Skip to content

Add PHPStan checks#382

Merged
chuckadams merged 16 commits intofairpm:release_1.4.0from
kasparsd:add-phpstan-min
Apr 6, 2026
Merged

Add PHPStan checks#382
chuckadams merged 16 commits intofairpm:release_1.4.0from
kasparsd:add-phpstan-min

Conversation

@kasparsd
Copy link
Copy Markdown

Fixes #376.

  1. Add szepeviktor/phpstan-wordpress (includes both phpstan and the WP core stubs) at v2.0 which supports the lowest supported PHP version 7.4 by the plugin.

  2. Update the existing coding standard CI checks to run the PHPStan checks inside the same predictable wp-env environment. This is part of larger strategy of running all CI tasks using the same tooling as used for local development (wp-env in this case instead of a soup of bash scripts that rely on global mysql installs, etc.)

  3. Update contributor documentation with all of the available helper scripts and their usage. Explain how to configure specific WP and PHP versions.

@github-actions
Copy link
Copy Markdown
Contributor

with:
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
key: ${{ runner.os }}-${{ hashFiles('composer.json') }} # Note that lock file will change between runs.
path: .cache
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where both phpcs and phpstan store their caches.

Comment thread .github/workflows/coding-standards.yml Outdated
- name: Make Composer packages available globally
run: echo "${PWD}/vendor/bin" >> $GITHUB_PATH
- name: Start the development environment
run: npm run env start
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run the checks inside the same development environment as used locally. Ensures consistent results across local dev environments and the CI runs.

I've tested this with updated PHPUnit runs and they take the same ~1 min. as the runs currently.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm rather 👎 on wp-env in general: it's running Node to run Docker to run PHP, when PHP directly will do fine in a controlled environment like a github runner. This is a wordpress plugin, so PHP is going to be a dev requirement regardless, and phpstan's results are going to be identical across OS distributions.

It looks like we're already using wp-env anyway, so if there's no impact on runner time, I don't have a problem either way. It just seems far more convoluted than it has to be.

Comment thread .github/workflows/coding-standards.yml Outdated
run: phpcs . -n --report-full
- name: Lint PHPCS
# TODO: Remove the flag once all warnings are fixed.
run: npm run lint:php:phpcs -- -- -- --warning-severity=0
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several existing warnings and errors reported (not sure why previously they didn't come up). I chose to not fix them as part of this pull request to make merging this easier.

FILE: inc/packages/admin/info.php
------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------
 498 | WARNING | Strings should not be wrapped in HTML (WordPress.WP.I18n.NoHtmlWrappedStrings)
------------------------------------------------------------------------------------------------


FILE: inc/packages/admin/namespace.php
------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------
 502 | WARNING | strip_tags() is discouraged. Use the more comprehensive wp_strip_all_tags() instead.
     |         | (WordPress.WP.AlternativeFunctions.strip_tags_strip_tags)
------------------------------------------------------------------------------------------------------------------------------------


FILE: inc/packages/namespace.php
------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------
 627 | WARNING | strip_tags() is discouraged. Use the more comprehensive wp_strip_all_tags() instead.
     |         | (WordPress.WP.AlternativeFunctions.strip_tags_strip_tags)
------------------------------------------------------------------------------------------------------------------------------------


FILE: inc/site-health/namespace.php
------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------
 1 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it
   |         | should execute logic with side effects, but should not do both. The first symbol is defined on line 13 and the first
   |         | side effect is on line 110. (PSR1.Files.SideEffects.FoundWithSymbols)
------------------------------------------------------------------------------------------------------------------------------------

Time: 241ms; Memory: 16MB

@@ -0,0 +1,109 @@
parameters:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid making changes outside of adding the phpstan tooling to this pull request, we generate a baseline of "known issues" which can be fixed in follow-up pull requests.

Comment thread composer.json
"format": "@php ./vendor/bin/phpcbf .",
"lint:phpcs": "phpcs .",
"lint:phpstan": "phpstan analyse --verbose --memory-limit=2G",
"lint": [
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We keep the existing composer lint as the combined task and introduce individual scripts for just phpcs and phpstan for convenience.

Comment thread CONTRIBUTING.md
# Stop the development server
$ npm run env stop
```
- `npm run lint:php:phpcs` to run PHPCS (configured in [`phpcs.xml.dist`](phpcs.xml.dist)).
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented a bunch of existing scripts along with pointers to their configuration.

Added instructions for overriding PHP and WP versions locally for running the tooling with any kind of setup. Also useful when debugging version-specific bugs and issues.

Comment thread phpcs.xml.dist
</rule>

<!-- Cache for PHPStan and PHPCS -->
<exclude-pattern>.cache</exclude-pattern>
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were being caught by the phpcs.

Comment thread phpstan.dist.neon
@@ -0,0 +1,10 @@
includes:
- tests/phpstan-baseline.neon # TODO: Fix all these issues.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed once we've fixed all the reported issues.

Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
@kasparsd
Copy link
Copy Markdown
Author

kasparsd commented Jan 4, 2026

@toderash Noticed this was moved to icebox. I'm not familiar with the triage/planning lingo for the project. Could you please confirm if there is any interest in merging this (or solving the associated #376) in near term? Or if there is anything required to get this merged?

I have a other follow-up pull requests ready that would fix #383, for example, and it relies on the cleanup and documentation updates in this pull request. Wanted to get a sense of when and how that could happen, if at all. Thanks!

@kasparsd
Copy link
Copy Markdown
Author

kasparsd commented Jan 4, 2026

Noting that #394 would have been caught by phpstan if I'm not mistaken.

@toderash toderash moved this from Icebox to Review in FAIR Connect Jan 5, 2026
@Ipstenu Ipstenu requested a review from rmccue February 9, 2026 17:48
@cdils cdils added this to the 1.4.0 milestone Feb 9, 2026
@Ipstenu Ipstenu changed the base branch from main to release_1.4.0 February 12, 2026 17:18
@cdils cdils requested review from chuckadams April 6, 2026 16:58
@cdils cdils removed this from the 1.4.0 milestone Apr 6, 2026
@cdils cdils modified the milestones: 1.5.0, 1.4.0 Apr 6, 2026
Comment thread .github/workflows/coding-standards.yml Outdated
- name: Make Composer packages available globally
run: echo "${PWD}/vendor/bin" >> $GITHUB_PATH
- name: Start the development environment
run: npm run env start
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm rather 👎 on wp-env in general: it's running Node to run Docker to run PHP, when PHP directly will do fine in a controlled environment like a github runner. This is a wordpress plugin, so PHP is going to be a dev requirement regardless, and phpstan's results are going to be identical across OS distributions.

It looks like we're already using wp-env anyway, so if there's no impact on runner time, I don't have a problem either way. It just seems far more convoluted than it has to be.

Comment thread .github/workflows/coding-standards.yml Outdated
run: phpcs . -n --report-full
- name: Lint PHPCS
# TODO: Remove the flag once all warnings are fixed.
run: npm run lint:php:phpcs -- -- -- --warning-severity=0
Copy link
Copy Markdown
Contributor

@chuckadams chuckadams Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    run: npm run lint:php:phpcs -- -- -- --warning-severity=0

Are there really supposed to be three occurrences of -- here? Mainly though, if we have a baseline file, we shouldn't need to bring down the severity, no? (edit: nvm I see this was for phpcs, not phpstan)

@chuckadams
Copy link
Copy Markdown
Contributor

I'm all for phpstan, but I refuse to deal with wp-env anymore:

❯ npm run lint:php:phpcs

> lint:php:phpcs
> wp-env run cli --env-cwd=wp-content/plugins/plugin composer run lint:phpcs

ℹ Starting 'composer run lint:phpcs' on the cli container. 

open /Users/chuck/.wp-env/ea95b44cf8f0dbec465691910de921ef/docker-compose.yml: no such file or directory
✖ Command failed with exit code 1
Command failed with exit code 1

composer run lint:phpstan does work fine though.

@chuckadams
Copy link
Copy Markdown
Contributor

In the interests of getting this merged before 1.4 release (even though it's a dev feature and the release doesn't matter) I'm going to patch this PR up myself so that the GH action does not use wp-env, then we can merge the rest to add the phpstan checks. While an individual CI run might not have added much overhead, we have a large test matrix that magnifies the extra overhead of Node+Docker significantly.

Signed-off-by: Chuck Adams <chaz@chaz.works>
Signed-off-by: Chuck Adams <chaz@chaz.works>
chuckadams
chuckadams previously approved these changes Apr 6, 2026
Copy link
Copy Markdown
Contributor

@chuckadams chuckadams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated coding-standards.yml to more directly invoke the linting CI scripts, but leaving the wp-env stuff as-is in CONTRIBUTING.md, assuming it works for others. Approved the workflow and it passes now.

cdils
cdils previously approved these changes Apr 6, 2026
Signed-off-by: Chuck Adams <chaz@chaz.works>
@chuckadams chuckadams dismissed stale reviews from cdils and themself via 2c21b15 April 6, 2026 19:10
@chuckadams chuckadams merged commit 0966d2a into fairpm:release_1.4.0 Apr 6, 2026
2 checks passed
@github-project-automation github-project-automation Bot moved this from Review to Done in FAIR Connect Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🚀 Feature Request: Add static analysis for PHP

4 participants