git掛鉤:使用PHP和靜態評論自動化開發任務
對於經驗豐富的git用戶,git鉤可能很熟悉。 對於未經啟發的人,git掛鉤是由特定的git事件(提交,推動,接收等)觸發的腳本,它們在客戶端和服務器上都運行。 儘管傳統上以狂歡命令寫,但他們可以利用各種語言。 PHP雖然不是貝殼腳本的理想之選,但由於塞繆爾·帕金森(Samuel Parkinson)的靜態評論,因此變得更加合適。該庫使您可以在PHP中本地編寫git鉤,可選地利用核心類。
>
>本地php git鉤開發。
提高了PHP對創建git鉤的適用性。> >
>示例:一個預加入鉤composer require sjparkinson/static-review
>
>此掛鉤利用幾個評論課來對上演文件進行檢查。 每個
類都擴展了並實現
(確定審核是否適用)和#!/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) ... }
Review
創建自定義評論:檢查AbstractReview
ReviewInterface
canReview()
review()
>
調用。 在PSR-4兼容的目錄結構(例如,)中創建一個新類(例如,var_dump()
)。
檢查php文件的php文件。 如果發現,則報告了錯誤。 var_dump()
VarDumpReview.php
src/SitePoint/StaticReview/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); } } }
安裝鉤子:var_dump()
grep
>使用靜態評論的命令行工具安裝鉤子:
>
靜態評論使開發人員在PHP中創建複雜的GIT鉤子,從而增強開發工作流程和代碼質量。 它的可定制性和易用性使其成為任何PHP項目的寶貴資產。
以上是用靜態評論編寫php git掛鉤的詳細內容。更多資訊請關注PHP中文網其他相關文章!