Skip to content

Commit a7666b8

Browse files
committed
Added optional git pre-hook to validate with phpcs before every commit.
1 parent e79d865 commit a7666b8

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
All notable changes to `laravel-phpcs` will be documented in this file
44

5+
## 1.2.0 - 2022-01-27
6+
7+
- Added optional git pre-hook to validate with phpcs before every commit.
8+
59
## 1.1.0 - 2022-01-26
610

7-
- Excluded route files for Generic.CodeAnalysis.UnusedFunctionParameter rule
11+
- Excluded route files for Generic.CodeAnalysis.UnusedFunctionParameter rule.
812

913
## 1.0.0 - 2022-01-25
1014

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ composer require mreduar/laravel-phpcs --dev
2828

2929
> Only neccesary if you installed as a package using composer!
3030
31-
`php artisan vendor:publish --provider="Mreduar\LaravelPhpcs\LaravelPhpcsServiceProvider"`
31+
`php artisan vendor:publish --provider="Mreduar\LaravelPhpcs\LaravelPhpcsServiceProvider" --tag="ruleset"`
3232

3333
This will publish to your root directory the following file
3434

@@ -47,6 +47,23 @@ This will publish to your root directory the following file
4747
</ruleset>
4848
```
4949

50+
Optionally you can also publish a git hook that will help you to never overlook smelly code.
51+
52+
`php artisan vendor:publish --provider="Mreduar\LaravelPhpcs\LaravelPhpcsServiceProvider" --tag="hook"`
53+
54+
The file will be published in its root directory `.git/hooks/pre-commit`
55+
So every time you try to commit `phpcs` will first check that you have everything correct.
56+
57+
```bash
58+
$ git commit -m "test"
59+
[1/1] code sniffer OK!
60+
[master a6133d7] test
61+
1 file changed, 1 insertion(+), 1 deletion(-)
62+
```
63+
64+
if you have any errors the commit will be cancelled.
65+
66+
5067
### Sniffing code
5168
Use php CodeSniffer commands, pointed towards your xml file, to sniff the code
5269
using the new ruleset.
File renamed without changes.

resources/stubs/pre-commit-hook.stub

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
# get bash colors and styles here:
4+
# http://misc.flogisoft.com/bash/tip_colors_and_formatting
5+
C_RESET='\e[0m'
6+
C_RED='\e[31m'
7+
C_GREEN='\e[32m'
8+
C_YELLOW='\e[33m'
9+
10+
function __run() #(step, name, cmd)
11+
{
12+
local color output exitcode
13+
14+
printf "${C_YELLOW}[%s]${C_RESET} %-20s" "$1" "$2"
15+
output=$(eval "$3" 2>&1)
16+
exitcode=$?
17+
18+
if [[ 0 == $exitcode || 130 == $exitcode ]]; then
19+
echo -e "${C_GREEN}OK!${C_RESET}"
20+
else
21+
echo -e "${C_RED}NOK!${C_RESET}\n\n$output"
22+
exit 1
23+
fi
24+
}
25+
26+
modified="git diff --diff-filter=M --name-only --cached | grep \".php$\""
27+
ignore="resources/lang,resources/views,bootstrap/helpers,database/migrations,bin"
28+
phpcs="vendor/bin/phpcs --report=code --colors --report-width=80 --ignore=${ignore}"
29+
30+
__run "1/1" "code sniffer" "${modified} | xargs -r ${phpcs}"
31+
# __run "2/3" "php lint" "${modified} | xargs -r php -l"
32+
# __run "3/3" "phpstan" "${modified} | xargs -r vendor/bin/phpstan analyse"

src/LaravelPhpcsServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ public function boot()
1313
{
1414
if ($this->app->runningInConsole()) {
1515
$this->publishes([
16-
__DIR__.'/../phpcs.xml.stub' => base_path('phpcs.xml'),
16+
__DIR__.'/../resources/stubs/phpcs.xml.stub' => base_path('phpcs.xml'),
1717
], 'ruleset');
18+
19+
$this->publishes([
20+
__DIR__.'/../resources/stubs/pre-commit-hook.stub' => base_path('.git/hooks/pre-commit'),
21+
], 'hook');
1822
}
1923
}
2024
}

0 commit comments

Comments
 (0)