-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Tests / Build Scripts: Configure PHPStan static analysis #7619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
48a9312
8e9c0e0
cdd934c
8da1a4a
9c8f096
7386db1
0949e37
4687567
7b44654
370551f
8bceab7
2ca6044
03e64f8
ac2ea4b
8f79e7e
46488de
88cc77b
4c8fcf4
37a379d
0646dce
64661b3
1375c99
788f5f1
c921307
bf2a9ae
f844811
7c6c2c0
f94f26b
6da7fe9
588a98c
3a13aed
6e191ca
ff32c3e
8fe43f3
b099803
0f1ee0b
49dcda3
89b4c20
fe6ece2
c12ed00
92941c5
2df0536
92bdb86
ebae3fa
12675ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
name: PHPStan Static Analysis | ||
|
||
on: | ||
# PHPStan testing was introduced in @todo. | ||
push: | ||
branches: | ||
- trunk | ||
- '6.9' | ||
- '[7-9].[0-9]' | ||
tags: | ||
- '6.9' | ||
- '6.9.[0-9]+' | ||
- '[7-9].[0-9]' | ||
- '[7-9]+.[0-9].[0-9]+' | ||
pull_request: | ||
branches: | ||
- trunk | ||
- '6.9' | ||
- '[7-9].[0-9]' | ||
paths: | ||
# This workflow only scans PHP files. | ||
- '**.php' | ||
# These files configure Composer. Changes could affect the outcome. | ||
- 'composer.*' | ||
# These files configure PHPStan. Changes could affect the outcome. | ||
- 'phpstan.neon.dist' | ||
- 'tests/phpstan/base.neon' | ||
# Confirm any changes to relevant workflow files. | ||
- '.github/workflows/php-static-analysis.yml' | ||
- '.github/workflows/reusable-php-static-analysis.yml' | ||
workflow_dispatch: | ||
|
||
# Cancels all previous workflow runs for pull requests that have not completed. | ||
concurrency: | ||
# The concurrency group contains the workflow name and the branch name for pull requests | ||
# or the commit hash for any other events. | ||
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} | ||
cancel-in-progress: true | ||
|
||
# Disable permissions for all available scopes by default. | ||
# Any needed permissions should be configured at the job level. | ||
permissions: {} | ||
|
||
jobs: | ||
# Runs PHPStan Static Analysis. | ||
phpstan: | ||
name: PHP coding standards | ||
uses: ./.github/workflows/reusable-php-static-analysis.yml | ||
permissions: | ||
contents: read | ||
if: ${{ github.repository == 'WordPress/wordpress-develop' || ( github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' ) }} | ||
|
||
slack-notifications: | ||
name: Slack Notifications | ||
uses: ./.github/workflows/slack-notifications.yml | ||
permissions: | ||
actions: read | ||
contents: read | ||
needs: [ phpstan ] | ||
if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event_name != 'pull_request' && always() }} | ||
with: | ||
calling_status: ${{ contains( needs.*.result, 'cancelled' ) && 'cancelled' || contains( needs.*.result, 'failure' ) && 'failure' || 'success' }} | ||
secrets: | ||
SLACK_GHA_SUCCESS_WEBHOOK: ${{ secrets.SLACK_GHA_SUCCESS_WEBHOOK }} | ||
SLACK_GHA_CANCELLED_WEBHOOK: ${{ secrets.SLACK_GHA_CANCELLED_WEBHOOK }} | ||
SLACK_GHA_FIXED_WEBHOOK: ${{ secrets.SLACK_GHA_FIXED_WEBHOOK }} | ||
SLACK_GHA_FAILURE_WEBHOOK: ${{ secrets.SLACK_GHA_FAILURE_WEBHOOK }} | ||
|
||
failed-workflow: | ||
name: Failed workflow tasks | ||
runs-on: ubuntu-24.04 | ||
permissions: | ||
actions: write | ||
needs: [ slack-notifications ] | ||
if: | | ||
always() && | ||
github.repository == 'WordPress/wordpress-develop' && | ||
github.event_name != 'pull_request' && | ||
github.run_attempt < 2 && | ||
( | ||
contains( needs.*.result, 'cancelled' ) || | ||
contains( needs.*.result, 'failure' ) | ||
) | ||
|
||
steps: | ||
- name: Dispatch workflow run | ||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
with: | ||
retries: 2 | ||
retry-exempt-status-codes: 418 | ||
script: | | ||
github.rest.actions.createWorkflowDispatch({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
workflow_id: 'failed-workflow.yml', | ||
ref: 'trunk', | ||
inputs: { | ||
run_id: `${context.runId}`, | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
## | ||
# A reusable workflow that runs PHP Static Analysis tests. | ||
## | ||
name: PHP Static Analysis | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
php-version: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The php tests just uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something to be addressed in this PR, and if so in which direction? (regular PHPCS also uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's something that should be addressed before this PR and then this PR may need to be updated. I'll raise a slack discussion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussion for this: https://wordpress.slack.com/archives/C08D0Q6BHNY/p1752520451251869 |
||
description: 'The PHP version to use.' | ||
required: false | ||
type: 'string' | ||
default: 'latest' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking this is why CI fails despite passing locally - I've baselined on v8.3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to leave out a default since this could cause issues in the future. For example, once 9.0 is the latest PHP, versions of WP that don't support it shouldn't have that as the default. Better to be explicit about the version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, but what version do we use:
|
||
|
||
# Disable permissions for all available scopes by default. | ||
# Any needed permissions should be configured at the job level. | ||
permissions: {} | ||
|
||
jobs: | ||
# Runs PHP static analysis tests. | ||
# | ||
# Violations are reported inline with annotations. | ||
# | ||
# Performs the following steps: | ||
# - Checks out the repository. | ||
# - Sets up PHP. | ||
# - Logs debug information. | ||
# - Installs Composer dependencies. | ||
# - Configures caching for PHP static analysis scans. | ||
# - Make Composer packages available globally. | ||
# - Runs PHPStan static analysis (with Pull Request annotations). | ||
# - Saves the PHPStan result cache. | ||
# - Ensures version-controlled files are not modified or deleted. | ||
phpstan: | ||
name: Run PHP static analysis | ||
runs-on: ubuntu-24.04 | ||
permissions: | ||
contents: read | ||
timeout-minutes: 20 | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
with: | ||
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} | ||
persist-credentials: false | ||
|
||
- name: Set up PHP | ||
uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0 | ||
with: | ||
php-version: ${{ inputs.php-version }} | ||
coverage: none | ||
tools: cs2pr | ||
|
||
- name: Log debug information | ||
run: | | ||
composer --version | ||
# This date is used to ensure that the Composer cache is cleared at least once every week. | ||
# http://man7.org/linux/man-pages/man1/date.1.html | ||
- name: "Get last Monday's date" | ||
id: get-date | ||
run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> "$GITHUB_OUTPUT" | ||
|
||
# Since Composer dependencies are installed using `composer update` and no lock file is in version control, | ||
# passing a custom cache suffix ensures that the cache is flushed at least once per week. | ||
- name: Install Composer dependencies | ||
uses: ramsey/composer-install@a2636af0004d1c0499ffca16ac0b4cc94df70565 # v3.1.0 | ||
with: | ||
custom-cache-suffix: ${{ steps.get-date.outputs.date }} | ||
|
||
- name: Make Composer packages available globally | ||
run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH" | ||
|
||
- name: Cache PHP Static Analysis scan cache | ||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 | ||
with: | ||
path: .cache # This is defined in the base.neon file. | ||
key: "phpstan-result-cache-${{ github.run_id }}" | ||
restore-keys: | | ||
phpstan-result-cache- | ||
- name: Run PHP static analysis tests | ||
id: phpstan | ||
run: phpstan analyse -vvv --error-format=checkstyle | cs2pr | ||
|
||
- name: "Save result cache" | ||
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 | ||
if: ${{ !cancelled() }} | ||
with: | ||
path: .cache | ||
key: "phpstan-result-cache-${{ github.run_id }}" | ||
|
||
- name: Ensure version-controlled files are not modified or deleted | ||
run: git diff --exit-code |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
{ | ||
"6-9": [ | ||
"7.2", | ||
"7.3", | ||
"7.4", | ||
"8.0", | ||
"8.1", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# PHPStan configuration for WordPress Core. | ||
# | ||
# To overload this configuration, copy this file to phpstan.neon and adjust as needed. | ||
# | ||
# https://phpstan.org/config-reference | ||
|
||
includes: | ||
# The WordPress Core configuration file includes the base configuration for the WordPress codebase. | ||
- tests/phpstan/base.neon | ||
# The baseline file includes preexisting errors in the codebase that should be ignored. | ||
# https://phpstan.org/user-guide/baseline | ||
- tests/phpstan/baseline/level-0.php | ||
- tests/phpstan/baseline/level-1.php | ||
- tests/phpstan/baseline/level-2.php | ||
- tests/phpstan/baseline/level-3.php | ||
- tests/phpstan/baseline/level-4.php | ||
- tests/phpstan/baseline/level-5.php | ||
- tests/phpstan/baseline/level-6.php | ||
|
||
parameters: | ||
level: 6 | ||
reportUnmatchedIgnoredErrors: true | ||
|
||
ignoreErrors: | ||
# Level 0: | ||
- # Inner functions arent supported by PHPstan. | ||
message: '#Function wxr_[a-z_]+ not found#' | ||
path: src/wp-admin/includes/export.php | ||
|
||
# Level 1: | ||
- # These are too noisy at the moment. | ||
message: '#Variable \$[a-zA-Z0-9_]+ might not be defined\.#' | ||
|
||
# Level 2: | ||
- # Callable-strings are used as callables in WordPress. | ||
message: '#Default value of the parameter .* is incompatible with type callable.*#' | ||
|
||
# Level 6: | ||
- # WPCS syntax for iterable types is not supported. | ||
identifier: missingType.iterableValue | ||
- # Too noisy until `void` return types are allowed. | ||
identifier: missingType.return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be dropping 7.2 and 7.3 testing which should be separate from phpstan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @johnbillion (I believe this was just to suppress the failing CI notifications until #9181 is merged)