@@ -38,9 +38,12 @@ $ bin/static-review.php hook:install hooks/example-pre-commit.php ~/.../.git/hoo
3838
3939[ composer ] : https://getcomposer.org/
4040
41- ## Example Hook
41+ ## Example Hooks
4242
43- Below is a basic hook that you can extend upon.
43+ Static Review can be used for both files and commit message review. Below are
44+ basic hooks for each.
45+
46+ ### For Files
4447
4548``` php
4649#!/usr/bin/env php
@@ -50,6 +53,7 @@ include __DIR__ . '/../../../autoload.php';
5053
5154// Reference the required classes.
5255use StaticReview\StaticReview;
56+ use StaticReview\Review\General\LineEndingsReview;
5357[...]
5458
5559$reporter = new Reporter();
@@ -61,17 +65,47 @@ $review->addReview(new LineEndingsReview());
6165$git = new GitVersionControl();
6266
6367// Review the staged files.
64- $review->review ($git->getStagedFiles());
68+ $review->files ($git->getStagedFiles());
6569
6670// Check if any issues were found.
6771// Exit with a non-zero status to block the commit.
6872($reporter->hasIssues()) ? exit(1) : exit(0);
6973```
7074
71- ## Example Review
75+ ### For Commit Messages
7276
7377``` php
74- class NoCommitTagReview extends AbstractReview
78+ #!/usr/bin/env php
79+ <?php
80+
81+ include __DIR__ . '/../../../autoload.php';
82+
83+ // Reference the required classes.
84+ use StaticReview\StaticReview;
85+ use StaticReview\Review\Message\BodyLineLengthReview;
86+ [...]
87+
88+ $reporter = new Reporter();
89+ $review = new StaticReview($reporter);
90+
91+ // Add any reviews to the StaticReview instance, supports a fluent interface.
92+ $review->addReview(new BodyLineLengthReview());
93+
94+ $git = new GitVersionControl();
95+
96+ // Review the current commit message.
97+ // The hook is passed the file holding the commit message as the first argument.
98+ $review->message($git->getCommitMessage($argv[1]));
99+
100+ // Check if any issues were found.
101+ // Exit with a non-zero status to block the commit.
102+ ($reporter->hasIssues()) ? exit(1) : exit(0);
103+ ```
104+
105+ ## Example Review For Files
106+
107+ ``` php
108+ class NoCommitTagReview extends AbstractFileReview
75109{
76110 // Review any text based file.
77111 public function canReviewFile(FileInterface $file)
@@ -98,6 +132,24 @@ class NoCommitTagReview extends AbstractReview
98132}
99133```
100134
135+ ## Example Review For Messages
136+
137+ ``` php
138+ class WorkInProgressReview extends AbstractMessageReview
139+ {
140+ // Check if the commit message contains "wip"
141+ public function review(ReporterInterface $reporter, ReviewableInterface $commit)
142+ {
143+ $fulltext = $commit->getSubject() . PHP_EOL . $commit->getBody();
144+
145+ if (preg_match('/\bwip\b/i', $fulltext)) {
146+ $message = 'Do not commit WIP to shared branches';
147+ $reporter->error($message, $this, $commit);
148+ }
149+ }
150+ }
151+ ```
152+
101153## Unit Tests
102154
103155See [ vagrantup.com] [ vagrant ] and [ phpunit.de] [ phpunit ] .
0 commit comments