drupal-starter
drupal-starter copied to clipboard
Run PHP syntax checked of modified files of current commit before PHPstan
It could be interesting to run a quick check if all the modified files of the current commit have valid syntax. This can be done before downloading any composer dependency.
git diff-tree --no-commit-id --name-only -r HEAD |grep -v yml$ | xargs -n1 php -l
Example using PHP 7:
$ php -v
PHP 7.4.33 (cli) (built: Nov 6 2022 12:08:36) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
$ git diff-tree --no-commit-id --name-only -r 726c12a | grep -v yml$ | xargs -n1 php -l
PHP Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in web/modules/custom/server_general/src/ElementWrap
Trait.php on line 193
Errors parsing web/modules/custom/server_general/src/ElementWrapTrait.php
xargs: php: exited with status 255; aborting
When running the same command with PHP 8:
$ ddev exec php -v
PHP 8.0.24 (cli) (built: Sep 29 2022 22:12:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.24, Copyright (c) Zend Technologies
with Zend OPcache v8.0.24, Copyright (c), by Zend Technologies
$ git diff-tree --no-commit-id --name-only -r 726c12a | grep -v yml$ | xargs -n1 ddev exec php -l
No syntax errors detected in web/modules/custom/server_general/src/ElementWrapTrait.php
There is only one caveat I need to figure out. When a commit deletes a file, php -l
fails to find it and fails...
@mariano-dagostino Is this for a pre-commit hook?
If yes, I use a simple one:
#!/bin/sh
#
# A hook that will verify code standards before committing.
ddev phpcs
if [ $? -ne 0 ]; then
exit 1 # exit with failure status
fi
ddev phpstan
if [ $? -ne 0 ]; then
exit 1 # exit with failure status
fi
This doesn't check the files in current commit though, just for whole codebase.
@dipakmdhrm Thanks... But that's does not consider commits made by github suggestions. This is why I'm proposing a quick sanity check before pulling any dependency.
The latest merged PR just got deployed successfully to Pantheon qa
environment