Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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$
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
50 changes: 50 additions & 0 deletions pre_commit_hooks/php-psalm.sh
Original file line number Diff line number Diff line change
@@ -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