Skip to content

Commit a6b8113

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 chosen **not** to add 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. - We could potentially use [Phive](https://phar.io/) 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. We could fix the version `setup-php` action installs to the current release `1.10.30`, 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, we can re-evaluate. - The PHP version for the CI run is set to PHP 7.4 to prevent PHPStan throwing some errors/notices related to the outdated PHPUnit version being used. * Adds a configuration file for PHPStan. Notes: - PHPStan needs to know about our dependencies (PHPCS et al), so I'm (re-)using the bootstrap file we have for our tests to load the PHPCS autoloader and register the standard with the PHPCS autoloader as we can't add an `autoload` directive to our `composer.json` file as it would cause problems with the PHPCS autoloader. - PHPStan flags type checks on properties with a documented type, while - especially for the `public` properties - 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 5f34bbe commit a6b8113

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/.gitattributes export-ignore
99
/.gitignore export-ignore
1010
/.phpcs.xml.dist export-ignore
11+
/phpstan.neon.dist export-ignore
1112
/phpunit.xml.dist export-ignore
1213
/.github export-ignore
1314
/bin export-ignore

.github/workflows/basics.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,31 @@ jobs:
7676
# At a later stage the documentation check can be activated.
7777
- name: Check sniff feature completeness
7878
run: composer feature-completeness
79+
80+
phpstan:
81+
name: "PHPStan"
82+
83+
runs-on: "ubuntu-latest"
84+
85+
steps:
86+
- name: Checkout code
87+
uses: actions/checkout@v3
88+
89+
- name: Install PHP
90+
uses: shivammathur/setup-php@v2
91+
with:
92+
php-version: '7.4'
93+
coverage: none
94+
tools: phpstan
95+
96+
# Install dependencies and handle caching in one go.
97+
# Dependencies need to be installed to make sure the PHPCS and PHPUnit classes are recognized.
98+
# @link https://github.com/marketplace/actions/install-composer-dependencies
99+
- name: Install Composer dependencies
100+
uses: "ramsey/composer-install@v2"
101+
with:
102+
# Bust the cache at least once a month - output format: YYYY-MM.
103+
custom-cache-suffix: $(date -u "+%Y-%m")
104+
105+
- name: Run PHPStan
106+
run: phpstan analyse

.gitignore

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

phpstan.neon.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
parameters:
2+
#phpVersion: 50400 # Needs to be 70100 or higher... sigh...
3+
level: 5
4+
paths:
5+
- WordPressVIPMinimum
6+
- tests
7+
bootstrapFiles:
8+
- tests/bootstrap.php
9+
scanDirectories:
10+
- vendor/wp-coding-standards/wpcs/WordPress
11+
treatPhpDocTypesAsCertain: false
12+
13+
ignoreErrors:

0 commit comments

Comments
 (0)