While developing the PHP projects and doing code reviews, I came to know that some of my time was wasted in suggesting the code formatting changes and indentation, there are tools available like PHPCS which take care of this stuff for you
As the developers were busy doing actual coding so sometimes they don’t even bother to check whether the code committed is adhering to the standards, or other reasons maybe they are lazy.
Git hooks are there to help to solve such situations, we can customize as per our need and execute some automated commands before committing the code
A few examples may be checking if any syntax errors, checking phpcs output for the committed files etc
We will look at some small examples that will accomplish this and will not take more than 10 mins to set up and work.
First of all, you will need a few packages which will help you in sorting your concerns in your projects.
Go ahead and install these
composer require --dev phpstan/phpstan
composer require --dev squizlabs/php_codesniffer
Make sure you have composer installed and working locally.
Your composer.json should look as follows
Make sure you have vendor directory added and it contains all the required files
The Next Step is to create a hook that will get executed before committing (pre-hook)
Open `.git/hooks/pre-commit file and add the following contents
#!/usr/bin/env bash
function __run() #(step, name, cmd)
{
local color output exitcode
printf "[%s] %-20s" "$1" "$2"
output=$(eval "$3" 2>&1)
exitcode=$?
if [[ 0 == $exitcode || 130 == $exitcode ]]; then
echo -e "OK!"
else
echo -e "Not OK!\n\n$output"
exit 1
fi
}
modified="git diff --diff-filter=M --name-only --cached | grep \".php$\""
ignore="resources/lang,resources/views,bootstrap/helpers,database/migrations,bin"
phpcs="vendor/bin/phpcs --report=code --colors --report-width=80 --standard=PSR2 --ignore=${ignore}"
__run "1/3" "php lint" "${modified} | xargs -r php -l"
__run "2/3" "code sniffer" "${modified} | xargs -r ${phpcs}"
__run "3/3" "phpstan" "${modified} | xargs -r vendor/bin/phpstan analyse"
It’s a simple bash script that will run specific commands before committing the code and based on output decides whether to continue or not
You may need to change the phpcs and another command path if you have a global installation
Go ahead and make some changes in the file and try to commit, I have added two files with some code standard errors, it will give you the following errors and prevent your commit.
bash-3.2$ git commit -m "pre commit test "
[1/3] php lint OK!
[2/3] code sniffer Not OK!
FILE: php/src/index.php
---------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
---------------------------------------------------------------------------
LINE 21: ERROR [x] A closing tag is not permitted at the end of a PHP file
---------------------------------------------------------------------------
19: ····echo·"Connected·to·MySQL·server·successfully!";
20: }
>> 21: ?>
---------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------
FILE: php/src/index2.php
--------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------
LINE 6: ERROR [x] A closing tag is not permitted at the end of a PHP file
--------------------------------------------------------------------------
4: //·in·the·php·file
5:
>> 6: ?>
--------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------
Time: 41ms; Memory: 6MB
bash-3.2$
You may get different errors based on your code
The good part is it will only check the files which we are committing and not the entire project folder
Once you fix all the errors then try committing again and if everything is fine, then only it will allow you to commit the changes.