diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 88cb0ca..99e5718 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -47,3 +47,10 @@ entry: pre_commit_hooks/php-stan.sh language: script files: \.php$ + +- id: php-psalm + name: Psalm + description: Run Psalm against all staged PHP files. + entry: pre_commit_hooks/php-psalm.sh + language: script + files: \.php$ diff --git a/README.md b/README.md index 1b554a0..5f74cf4 100644 --- a/README.md +++ b/README.md @@ -121,3 +121,19 @@ Adds the (PHPStan)[https://phpstan.org/] tool. ``` An `args` property in your hook declaration can be used for pass any valid PHPStan arguments. + +## php-psalm + +Adds the [PSalm](https://psalm.dev/) tool. + + + +```yaml +-- repo: https://github.com/digitalpulp/pre-commit-php.git + rev: 1.4.0 + hooks: + - id: php-psalm + files: \.(php)$ +``` + +An `args` property in your hook declaration can be used for passing any valid Psalm arguments. diff --git a/pre_commit_hooks/php-psalm.sh b/pre_commit_hooks/php-psalm.sh new file mode 100755 index 0000000..a8a8e35 --- /dev/null +++ b/pre_commit_hooks/php-psalm.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +################################################################################ +# +# Bash Psalm +# +# This script fails if Psalm output has the word "ERROR" in it. +# +# Exit 0 if no errors found +# Exit 1 if errors were found +# +# Requires +# - php +# +# Arguments +# See: https://psalm.dev/docs/running_psalm/command_line_usage/ +# +################################################################################ + +# Plugin title +title="Psalm" + +# Possible command names of this tool +local_command="psalm.phar" +vendor_command="vendor/bin/psalm" +global_command="psalm" + +# Print a welcome and locate the exec for this tool +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $DIR/helpers/colors.sh +source $DIR/helpers/formatters.sh +source $DIR/helpers/welcome.sh +source $DIR/helpers/locate.sh + +command_files_to_check="${@:2}" +command_args=$1 +command_to_run="${exec_command} ${command_args} ${command_files_to_check}" + +echo -e "${bldwht}Running command ${txtgrn} ${exec_command} ${command_args} ${txtrst}" +hr +command_result=`eval $command_to_run` +if [[ $command_result =~ ERROR ]] +then + hr + echo -en "${bldmag}Errors detected by ${title}... ${txtrst} \n" + hr + echo "$command_result" + exit 1 +fi + +exit 0