git挂钩:使用PHP和静态评论自动化开发任务
对于经验丰富的git用户,git钩可能很熟悉。 对于未经启发的人,git挂钩是由特定的git事件(提交,推动,接收等)触发的脚本,它们在客户端和服务器上都运行。 尽管传统上以狂欢命令写,但他们可以利用各种语言。 PHP虽然不是贝壳脚本的理想之选,但由于塞缪尔·帕金森(Samuel Parkinson)的静态评论,因此变得更加合适。该库使您可以在PHP中本地编写git钩,可选地利用核心类。
>
>本地php git钩开发。
提高了PHP对创建git钩的适用性。> >
>示例:一个预加入钩composer require sjparkinson/static-review
>
>此挂钩利用几个评论课来对上演文件进行检查。 每个
类都扩展了并实现
(确定审核是否适用)和<code class="language-php">#!/usr/bin/env php <?php // ... (Autoloader inclusion and error handling – as in original example) ... // ... (Class imports – as in original example) ... $reporter = new Reporter(); $climate = new CLImate(); $Git = new GitVersionControl(); $review = new StaticReview($reporter); // Add reviews (LineEndingsReview, PhpLeadingLineReview, NoCommitTagReview, PhpLintReview, ComposerLintReview – as in original example) // Review staged files $review->review($Git->getStagedFiles()); // Report results if ($reporter->hasIssues()) { // ... (Error reporting – as in original example) ... } else { // ... (Success message – as in original example) ... }</code>>(执行评论)方法。
Review
创建自定义评论:检查AbstractReview
ReviewInterface
canReview()
review()
>
调用。 在PSR-4兼容的目录结构(例如,)中创建一个新类(例如,var_dump()
)。
检查php文件的php文件。 如果发现,则报告了错误。var_dump()
VarDumpReview.php
src/SitePoint/StaticReview/PHP
<code class="language-php"><?php namespace SitePoint\StaticReview\PHP; // ... (Imports – as in original example) ... class VarDumpReview extends AbstractReview { public function canReview(FileInterface $file) { return in_array($file->getExtension(), ['php', 'phtml']); } public function review(ReporterInterface $reporter, FileInterface $file) { $cmd = sprintf('grep --fixed-strings --ignore-case --quiet "var_dump" %s', $file->getFullPath()); $process = $this->getProcess($cmd); $process->run(); if (! $process->isSuccessful()) { // Note: Changed to !isSuccessful() $reporter->error('A call to `var_dump()` was found', $this, $file); } } }</code>
安装钩子:var_dump()
grep
>使用静态评论的命令行工具安装钩子:
>
静态评论使开发人员在PHP中创建复杂的GIT钩子,从而增强开发工作流程和代码质量。 它的可定制性和易用性使其成为任何PHP项目的宝贵资产。
以上是用静态评论编写php git挂钩的详细内容。更多信息请关注PHP中文网其他相关文章!