Skip to content

Commit 5a79024

Browse files
committed
ci: add GH workflow for PHPStan
1 parent 3cd2070 commit 5a79024

2 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: PHP Static Analysis
2+
3+
on:
4+
# PHPStan testing was introduced in @todo.
5+
push:
6+
branches:
7+
- trunk
8+
- '[6-9].[0-9]'
9+
tags:
10+
- '[6-9].[0-9]'
11+
- '[6-9]+.[0-9].[0-9]+'
12+
pull_request:
13+
branches:
14+
- trunk
15+
- '[6-9].[0-9]'
16+
paths:
17+
# This workflow only scans PHP files.
18+
- '**.php'
19+
# These files configure Composer. Changes could affect the outcome.
20+
- 'composer.*'
21+
# These files configure PHPStan. Changes could affect the outcome.
22+
- 'phpstan.neon.dist'
23+
- 'tests/phpstan/base.neon'
24+
# Confirm any changes to relevant workflow files.
25+
- '.github/workflows/php-static-analysis.yml'
26+
- '.github/workflows/reusable-php-static-analysis.yml'
27+
workflow_dispatch:
28+
29+
# Cancels all previous workflow runs for pull requests that have not completed.
30+
concurrency:
31+
# The concurrency group contains the workflow name and the branch name for pull requests
32+
# or the commit hash for any other events.
33+
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
34+
cancel-in-progress: true
35+
36+
# Disable permissions for all available scopes by default.
37+
# Any needed permissions should be configured at the job level.
38+
permissions: {}
39+
40+
jobs:
41+
# Runs PHPStan Static Analysis.
42+
phpstan:
43+
name: PHP coding standards
44+
uses: ./.github/workflows/reusable-php-static-analysis.yml
45+
permissions:
46+
contents: read
47+
if: ${{ github.repository == 'WordPress/wordpress-develop' || ( github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' ) }}
48+
49+
slack-notifications:
50+
name: Slack Notifications
51+
uses: ./.github/workflows/slack-notifications.yml
52+
permissions:
53+
actions: read
54+
contents: read
55+
needs: [ phpstan ]
56+
if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event_name != 'pull_request' && always() }}
57+
with:
58+
calling_status: ${{ contains( needs.*.result, 'cancelled' ) && 'cancelled' || contains( needs.*.result, 'failure' ) && 'failure' || 'success' }}
59+
secrets:
60+
SLACK_GHA_SUCCESS_WEBHOOK: ${{ secrets.SLACK_GHA_SUCCESS_WEBHOOK }}
61+
SLACK_GHA_CANCELLED_WEBHOOK: ${{ secrets.SLACK_GHA_CANCELLED_WEBHOOK }}
62+
SLACK_GHA_FIXED_WEBHOOK: ${{ secrets.SLACK_GHA_FIXED_WEBHOOK }}
63+
SLACK_GHA_FAILURE_WEBHOOK: ${{ secrets.SLACK_GHA_FAILURE_WEBHOOK }}
64+
65+
failed-workflow:
66+
name: Failed workflow tasks
67+
runs-on: ubuntu-24.04
68+
permissions:
69+
actions: write
70+
needs: [ slack-notifications ]
71+
if: |
72+
always() &&
73+
github.repository == 'WordPress/wordpress-develop' &&
74+
github.event_name != 'pull_request' &&
75+
github.run_attempt < 2 &&
76+
(
77+
contains( needs.*.result, 'cancelled' ) ||
78+
contains( needs.*.result, 'failure' )
79+
)
80+
81+
steps:
82+
- name: Dispatch workflow run
83+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
84+
with:
85+
retries: 2
86+
retry-exempt-status-codes: 418
87+
script: |
88+
github.rest.actions.createWorkflowDispatch({
89+
owner: context.repo.owner,
90+
repo: context.repo.repo,
91+
workflow_id: 'failed-workflow.yml',
92+
ref: 'trunk',
93+
inputs: {
94+
run_id: `${context.runId}`,
95+
}
96+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
##
2+
# A reusable workflow that runs PHP Static Analysis tests.
3+
##
4+
name: PHP Static Analysis
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
php-version:
10+
description: 'The PHP version to use.'
11+
required: false
12+
type: 'string'
13+
default: 'latest'
14+
15+
# Disable permissions for all available scopes by default.
16+
# Any needed permissions should be configured at the job level.
17+
permissions: {}
18+
19+
jobs:
20+
# Runs PHP static analysis tests.
21+
#
22+
# Violations are reported inline with annotations.
23+
#
24+
# Performs the following steps:
25+
# - Checks out the repository.
26+
# - Sets up PHP.
27+
# - Logs debug information.
28+
# - Installs Composer dependencies.
29+
# - Configures caching for PHP static analysis scans.
30+
# - Make Composer packages available globally.
31+
# - Runs PHPStan static analysis (with Pull Request annotations).
32+
# - Saves the PHPStan result cache.
33+
# - Ensures version-controlled files are not modified or deleted.
34+
phpstan:
35+
name: Run PHP static analysis
36+
runs-on: ubuntu-24.04
37+
permissions:
38+
contents: read
39+
timeout-minutes: 20
40+
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
44+
with:
45+
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
46+
persist-credentials: false
47+
48+
- name: Set up PHP
49+
uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0
50+
with:
51+
php-version: ${{ inputs.php-version }}
52+
coverage: none
53+
tools: cs2pr
54+
55+
- name: Log debug information
56+
run: |
57+
composer --version
58+
59+
# This date is used to ensure that the Composer cache is cleared at least once every week.
60+
# http://man7.org/linux/man-pages/man1/date.1.html
61+
- name: "Get last Monday's date"
62+
id: get-date
63+
run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> "$GITHUB_OUTPUT"
64+
65+
# Since Composer dependencies are installed using `composer update` and no lock file is in version control,
66+
# passing a custom cache suffix ensures that the cache is flushed at least once per week.
67+
- name: Install Composer dependencies
68+
uses: ramsey/composer-install@a2636af0004d1c0499ffca16ac0b4cc94df70565 # v3.1.0
69+
with:
70+
custom-cache-suffix: ${{ steps.get-date.outputs.date }}
71+
72+
- name: Make Composer packages available globally
73+
run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH"
74+
75+
- name: Cache PHP Static Analysis scan cache
76+
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
77+
with:
78+
path: .cache # This is defined in the base.neon file.
79+
key: "phpstan-result-cache-${{ github.run_id }}"
80+
restore-keys: |
81+
phpstan-result-cache-
82+
83+
- name: Run PHP static analysis tests
84+
id: phpstan
85+
run: phpstan analyse -vv --error-format=checkstyle | cs2pr
86+
87+
- name: "Save result cache"
88+
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
89+
if: ${{ !cancelled() }}
90+
with:
91+
path: .cache
92+
key: "phpstan-result-cache-${{ github.run_id }}"
93+
94+
- name: Ensure version-controlled files are not modified or deleted
95+
run: git diff --exit-code

0 commit comments

Comments
 (0)