Skip to content

Commit dcea715

Browse files
committed
Add PHPStan to QA checks
PHPStan is a good addition to our QA toolkit and with improvements PHPStan has made over the years is now a viable tool for us to use (previously it would give way too many false positives). This commit: * Adds a separate job to the `basics` workflow in GH Actions. Notes: - I've **not** added PHPStan to the Composer dependencies for two reasons: 1. It doesn't allow for installation on PHP < 7.2, which would break/block the `composer install` for our test runs. 2. It would add dependencies which could conflict/cause problems for our test runs due to those defining token constants too. - [Phive](https://phar.io/) could potentially be used to still have a setup which can be used locally, but just running locally from a PHPStan PHAR file should work just fine. - For CI, PHPStan will be installed as a PHAR file by `setup-php` now. This does carry a risk _if_ PHPStan would make breaking changes or if a new release adds rules for the levels being scanned as, in that case, builds could unexpectedly start failing. Fixing the version for the `setup-php` action installs to the current release `1.12.3` could prevent that, but that adds an additional maintenance burden of having to keep updating the version as PHPStan releases pretty often. So, for now, I've elected to run the risk of random failures. If and when those start happening, this should be re-evaluated. * Adds a configuration file for PHPStan. Notes: - PHPStan needs to know about the dependencies (PHPCS et al), so I'm (re-)using the bootstrap file for the tests to load the PHPCS autoloader and register the standard with the PHPCS autoloader as adding an `autoload` directive to the `composer.json` file would cause problems with the PHPCS autoloader. - PHPStan flags type checks on properties with a documented type, while without `strict_types` we cannot always be sure the properties set will be of the correct type. For that reason, I've set `treatPhpDocTypesAsCertain` to `false` (which silences those notices). * Adds the configuration file to `.gitattributes` and the typical overload file for the configuration file to `.gitignore`. Refs: * https://phpstan.org/ * https://phpstan.org/config-reference
1 parent 2dee3ea commit dcea715

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/.gitignore export-ignore
1010
/.github export-ignore
1111
/phpcs.xml.dist export-ignore
12+
/phpstan.neon.dist export-ignore
1213
/phpunit.xml.dist export-ignore
1314
/phpunit-bootstrap.php export-ignore
1415
/PHPCSDebug/Tests export-ignore

.github/workflows/cs.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,30 @@ jobs:
9797
- name: Show PHPCS results in PR
9898
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
9999
run: cs2pr ./phpcs-report.xml
100+
101+
phpstan:
102+
name: "PHPStan"
103+
runs-on: "ubuntu-latest"
104+
105+
steps:
106+
- name: Checkout code
107+
uses: actions/checkout@v4
108+
109+
- name: Install PHP
110+
uses: shivammathur/setup-php@v2
111+
with:
112+
php-version: 'latest'
113+
coverage: none
114+
tools: phpstan
115+
116+
# Install dependencies and handle caching in one go.
117+
# Dependencies need to be installed to make sure the PHPCS and PHPUnit classes are recognized.
118+
# @link https://github.com/marketplace/actions/install-composer-dependencies
119+
- name: Install Composer dependencies
120+
uses: "ramsey/composer-install@v3"
121+
with:
122+
# Bust the cache at least once a month - output format: YYYY-MM.
123+
custom-cache-suffix: $(date -u "+%Y-%m")
124+
125+
- name: Run PHPStan
126+
run: phpstan analyse

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ composer.lock
55
phpcs.xml
66
phpunit.xml
77
.phpunit.result.cache
8+
phpstan.neon

phpstan.neon.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
#phpVersion: 50400 # Needs to be 70100 or higher... sigh...
3+
level: 6
4+
paths:
5+
- bin
6+
- PHPCSDebug
7+
- Scripts
8+
- Tests
9+
bootstrapFiles:
10+
- phpunit-bootstrap.php
11+
treatPhpDocTypesAsCertain: false

0 commit comments

Comments
 (0)