From c4f5d19bb1bc31c1660acea8c0859efbc0f6c3ad Mon Sep 17 00:00:00 2001 From: Richard van den Brand Date: Sun, 28 Nov 2021 12:13:58 +0100 Subject: [PATCH 1/2] Added support for Psalm --- .pre-commit-hooks.yaml | 7 +++++ README.md | 16 +++++++++++ pre_commit_hooks/php-psalm.sh | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100755 pre_commit_hooks/php-psalm.sh 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..5560d4e --- /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://phpstan.org/user-guide/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 From 4b943f459a6407a6a9ca68e7c4aaede652a4cfcc Mon Sep 17 00:00:00 2001 From: Richard van den Brand Date: Tue, 30 Nov 2021 19:22:29 +0100 Subject: [PATCH 2/2] Fixed link to Psalm documentation --- pre_commit_hooks/php-psalm.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit_hooks/php-psalm.sh b/pre_commit_hooks/php-psalm.sh index 5560d4e..a8a8e35 100755 --- a/pre_commit_hooks/php-psalm.sh +++ b/pre_commit_hooks/php-psalm.sh @@ -12,7 +12,7 @@ # - php # # Arguments -# See: https://phpstan.org/user-guide/command-line-usage +# See: https://psalm.dev/docs/running_psalm/command_line_usage/ # ################################################################################