Skip to content

Commit

Permalink
Add PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
FatherShawn committed Apr 20, 2020
1 parent 45b0f40 commit c864643
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
15 changes: 11 additions & 4 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

- id: php-lint
name: PHP Syntax Check (Quick)
description: Runs php -l on all staged files. Exits when it hits the first errored file
description: Runs php -l on all staged PHP files. Exits when it hits the first errored file
entry: php -l
language: system
files: \.php$
Expand All @@ -22,21 +22,28 @@

- id: php-cs
name: PHP Codesniffer
description: Run php codesniffer against all staged files.
description: Run php codesniffer against all staged PHP files.
entry: pre_commit_hooks/php-cs.sh
language: script
files: \.php$

- id: php-cbf
name: PHP Codesniffer (Code Beutifier and Formatter)
description: Run php codesniffer against all staged files.
description: Run php codesniffer against all staged PHP files.
entry: pre_commit_hooks/php-cbf.sh
language: script
files: \.php$

- id: php-cs-fixer
name: PHP Coding Standards Fixer
description: Run php coding standards fixer against all staged files.
description: Run php coding standards fixer against all staged PHP files.
entry: pre_commit_hooks/php-cs-fixer.sh
language: script
files: \.php$

- id: php-stan
name: PHPStan
description: Run PHPStan against all staged PHP files.
entry: pre_commit_hooks/php-stan.sh
language: script
files: \.php$
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Just add to your `.pre-commit-config.yaml` file with the following

```yaml
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-lint
- id: php-unit
Expand All @@ -25,9 +25,8 @@ Just add to your `.pre-commit-config.yaml` file with the following
## php-lint
```yaml
<<<<<<< HEAD
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-lint
```
Expand All @@ -38,7 +37,7 @@ A bash script that runs `php -l` against stage files that are php. Assumes `php`

```yaml
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-lint-all
```
Expand All @@ -50,7 +49,7 @@ A systems hook that just runs `php -l` against stage files that have the `.php`

```yaml
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-unit
```
Expand All @@ -66,7 +65,7 @@ Note in its current state, it will run the whole PHPUnit test as along as `.php`

```yaml
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-cs
files: \.(php)$
Expand All @@ -79,13 +78,11 @@ It will assume that there is a valid PHP Code Sniffer executable at these locati

The `args` property in your hook declaration can be used for pass any valid PHP Code Sniffer arguments. In the example above, it will run PHP Code Sniffer against only the staged php files with the `PSR-1` and progress enabled.

If you have multiple standards or a comma in your `args` property, escape the comma character like so

## php-cbf

```yaml
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-cs
files: \.(php)$
Expand All @@ -101,7 +98,7 @@ If you have multiple standards or a comma in your `args` property, escape the co

```yaml
- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-cs
files: \.(php)$
Expand All @@ -113,7 +110,7 @@ To install PHP Codesniffer (phpcs & phpcbf), follow the [recommended steps here]
## php-cs-fixer
```yaml
-- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.3.0
sha: 1.4.0
hooks:
- id: php-cs-fixer
files: \.(php)$
Expand All @@ -123,3 +120,18 @@ Similar pattern as the php-cs hook. A bash script that will run the appropriate

The tool will fail a build when it has made changes to the staged files. This allows a developer to do a `git diff` and examine the changes that it has made. Remember that you may omit this if needed with a `SKIP=php-cs-fixer git commit`.

## php-stan

Adds the (PHPStan)[https://phpstan.org/] tool.



```yaml
-- repo: https://github.com/digitalpulp/pre-commit-php.git
sha: 1.4.0
hooks:
- id: php-stan
files: \.(php)$
```

An `args` property in your hook declaration can be used for pass any valid PHPStan arguments.
50 changes: 50 additions & 0 deletions pre_commit_hooks/php-stan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
################################################################################
#
# Bash PHPStan
#
# This script fails if the PHPStan 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="PHPStan"

# Possible command names of this tool
local_command="phpstan.phar"
vendor_command="vendor/bin/phpstan"
global_command="phpstan"

# 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} analyse --no-progress ${command_args} ${command_files_to_check}"

echo -e "${bldwht}Running command ${txtgrn} ${exec_command} analyse ${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

0 comments on commit c864643

Please sign in to comment.