Skip to content

Commit 344ceb3

Browse files
authored
Merge branch 'trunk' into deprecated-abilities
2 parents 575c8f0 + e46ac23 commit 344ceb3

1,440 files changed

Lines changed: 160598 additions & 31745 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/phpstan-pull-request-comments.yml‎

Lines changed: 370 additions & 0 deletions
Large diffs are not rendered by default.

‎.github/workflows/reusable-phpstan-static-analysis-v1.yml‎

Lines changed: 147 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ permissions: {}
2121
jobs:
2222
# Runs PHP static analysis tests.
2323
#
24-
# Violations are reported inline with annotations.
24+
# Violations are reported inline with annotations. For pull requests, the results are also uploaded
25+
# as an artifact, which the PHPStan Pull Request Comments workflow summarizes in a comment.
2526
#
2627
# Performs the following steps:
2728
# - Checks out the repository.
@@ -31,8 +32,10 @@ jobs:
3132
# - Make Composer packages available globally.
3233
# - Builds WordPress.
3334
# - Configures caching for PHPStan static analysis scans.
34-
# - Runs PHPStan static analysis (with Pull Request annotations).
35-
# - Checks whether the baselines need regenerating.
35+
# - Runs PHPStan static analysis.
36+
# - Reports the results as annotations.
37+
# - Uploads the results as an artifact (pull requests only).
38+
# - Checks for errors that cannot be reported as warnings, such as out-of-date baselines.
3639
# - Saves the PHPStan result cache.
3740
# - Ensures version-controlled files are not modified or deleted.
3841
phpstan:
@@ -43,18 +46,20 @@ jobs:
4346
timeout-minutes: 20
4447

4548
steps:
49+
# For a pull request, the parents of the merge commit are fetched too, to find the files that the base branch
50+
# has changed since the pull request branched from it.
4651
- name: Checkout repository
4752
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
4853
with:
4954
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
5055
persist-credentials: false
56+
fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 1 }}
5157

5258
- name: Set up PHP
5359
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # 2.37.2
5460
with:
5561
php-version: ${{ inputs.php-version }}
5662
coverage: none
57-
tools: cs2pr
5863

5964
- name: General debug information
6065
run: |
@@ -98,35 +103,125 @@ jobs:
98103
- name: Run PHP static analysis tests
99104
id: phpstan
100105
run: |
101-
# The report is written to a file as well as piped to cs2pr, so that the step below
102-
# can look at it.
106+
# The report is written to a file for the steps below, which turn it into annotations,
107+
# the pull request comment, and a check for errors that cannot be reported as warnings.
108+
# Only the report goes to standard output; the debug output goes to standard error, so
109+
# it stays in the log without corrupting the JSON.
103110
#
104-
# cs2pr exits successfully so that reported errors annotate the pull request without
105-
# failing the run. A pipeline reports only the status of its last command, so that
106-
# also discards the status of the analysis itself. Recover it from PIPESTATUS.
107-
composer run phpstan -- -vvv --error-format=checkstyle | tee "${RUNNER_TEMP}/phpstan-report.xml" | cs2pr --errors-as-warnings --graceful-warnings
108-
status="${PIPESTATUS[0]}"
111+
# The step shell exits on the first failing command, which would also end the step on
112+
# the status of 1 that PHPStan uses for reporting errors. Capture the status instead.
113+
status=0
114+
composer run phpstan -- -vvv --error-format=json > "${RUNNER_TEMP}/phpstan-report.json" || status=$?
109115
110116
# PHPStan exits 1 when it has errors to report, which is the expected case here and
111-
# is what the annotations are for. Anything higher means it did not finish at all,
112-
# which would otherwise pass silently, since the discarded status was the only sign.
117+
# is what the annotations are for. Anything higher means it did not finish at all.
113118
if [ "${status}" -gt 1 ]; then
114119
echo "::error title=PHPStan did not complete::The analysis exited with status ${status}, so the code was not fully checked. This is a failure of the run itself rather than a problem found in the code."
115120
exit "${status}"
116121
fi
117122
118-
# An ignored error that no longer occurs, or occurs a different number of times, is
119-
# reported under an `ignore.*` identifier. That is not something to fix in the code: the
120-
# usual cause is that the error *was* fixed, leaving a baseline describing a state that
121-
# no longer exists. PHPStan does not allow those reports to be ignored or baselined.
123+
# Errors are annotated as warnings, so that they are visible on the pull request without
124+
# failing the run. The exception is an error that PHPStan does not allow to be ignored,
125+
# such as an out-of-date baseline entry, or one that is not specific to a file. These are
126+
# annotated as errors instead, and the run fails on them in the check further below.
122127
#
123-
# The analysis above is reported as warnings, so this would otherwise surface as a
124-
# passing run carrying an annotation that reads like a complaint about a fix. Call it
125-
# out on its own, and say what to do about it.
128+
# The results are also written out for the PHPStan Pull Request Comments workflow, which
129+
# runs with the permissions needed to comment on the pull request. Only the facts are
130+
# passed along: the comment is composed there, since a pull request can change anything
131+
# that runs here.
132+
- name: Report the analysis results
133+
id: report
134+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
135+
with:
136+
script: |
137+
const fs = require( 'fs' );
138+
139+
const report = JSON.parse( fs.readFileSync( `${ process.env.RUNNER_TEMP }/phpstan-report.json`, 'utf8' ) );
140+
const workspace = `${ process.env.GITHUB_WORKSPACE }/`;
141+
142+
const errors = [];
143+
for ( const [ absolutePath, file ] of Object.entries( report.files ) ) {
144+
const relativePath = absolutePath.startsWith( workspace ) ? absolutePath.slice( workspace.length ) : absolutePath;
145+
for ( const message of file.messages ) {
146+
errors.push( {
147+
file: relativePath,
148+
line: message.line ?? null,
149+
message: message.message,
150+
identifier: message.identifier ?? null,
151+
tip: message.tip ?? null,
152+
ignorable: message.ignorable,
153+
} );
154+
}
155+
}
156+
157+
// Errors that are not specific to a file, such as an `ignoreErrors` entry without a
158+
// path that no longer matches. These cannot be ignored either.
159+
for ( const message of report.errors ) {
160+
errors.push( { file: null, line: null, message, identifier: null, tip: null, ignorable: false } );
161+
}
162+
163+
for ( const error of errors ) {
164+
const properties = { title: error.identifier ? `PHPStan: ${ error.identifier }` : 'PHPStan' };
165+
if ( error.file ) {
166+
properties.file = error.file;
167+
}
168+
if ( error.line ) {
169+
properties.startLine = error.line;
170+
}
171+
const message = error.tip ? `${ error.message }\n${ error.tip }` : error.message;
172+
if ( error.ignorable ) {
173+
core.warning( message, properties );
174+
} else {
175+
core.error( message, properties );
176+
}
177+
}
178+
179+
// Tips are left out of the results, since they are only shown in the annotations, next to the code.
180+
//
181+
// The commit analyzed is the pull request merged into its base branch, whose second parent is the head
182+
// commit. The files with errors that differ between the two are those the base branch has changed, in
183+
// which the line numbers reported can be off in the head commit.
184+
if ( context.eventName === 'pull_request' ) {
185+
let changedInBase = [];
186+
try {
187+
const filesWithErrors = new Set( errors.map( ( error ) => error.file ) );
188+
changedInBase = require( 'child_process' )
189+
.execFileSync( 'git', [ 'diff', '--name-only', '--no-renames', 'HEAD^2', 'HEAD' ], { encoding: 'utf8' } )
190+
.split( '\n' )
191+
.filter( ( file ) => filesWithErrors.has( file ) );
192+
} catch ( error ) {
193+
core.warning( `The files changed in the base branch could not be found: ${ error.message }` );
194+
}
195+
fs.mkdirSync( `${ process.env.RUNNER_TEMP }/phpstan-results`, { recursive: true } );
196+
fs.writeFileSync( `${ process.env.RUNNER_TEMP }/phpstan-results/results.json`, JSON.stringify( {
197+
pull_request: context.payload.pull_request.number,
198+
changed_in_base: changedInBase,
199+
errors: errors.map( ( { tip, ...error } ) => error ),
200+
} ) );
201+
}
202+
203+
# Uploads the results for the PHPStan Pull Request Comments workflow, which keeps a comment on the
204+
# pull request up to date with them.
205+
- name: Upload the analysis results as an artifact
206+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
207+
if: ${{ github.event_name == 'pull_request' && steps.report.outcome == 'success' }}
208+
with:
209+
name: phpstan-results
210+
path: ${{ runner.temp }}/phpstan-results/
211+
212+
# Some errors cannot be ignored, so they cannot be reported as warnings either. Most often
213+
# this is an ignored error that no longer occurs, or occurs a different number of times,
214+
# which is reported under an `ignore.*` identifier. That is not something to fix in the
215+
# code: the usual cause is that the error *was* fixed, leaving a baseline describing a state
216+
# that no longer exists.
217+
#
218+
# Such an error is reported without a line number, so its annotation is not shown alongside
219+
# the changes in the pull request, and one that is not specific to a file has nowhere to be
220+
# shown at all. A passing run would let it go unnoticed, so fail the run on it instead, and
221+
# say what to do about it.
126222
#
127-
# Detection is on the identifier rather than the message, which is prose and may be
128-
# reworded in any release. The checkstyle format carries it in the `source` attribute.
129-
- name: Check whether the baselines need regenerating
223+
# Detection is on PHPStan's own `ignorable` flag rather than on the identifier or message.
224+
- name: Check for errors that cannot be reported as warnings
130225
if: ${{ !cancelled() }}
131226
env:
132227
BASELINES_URL: ${{ github.server_url }}/${{ github.repository }}/tree/${{ github.sha }}/tests/phpstan/baselines
@@ -135,28 +230,36 @@ jobs:
135230
# This step runs even when the analysis above it failed, in which case the report may
136231
# never have been written. That failure is reported there, so there is nothing to add
137232
# here beyond staying quiet about a file that was never going to exist.
138-
if [ ! -f "${RUNNER_TEMP}/phpstan-report.xml" ]; then
233+
report="${RUNNER_TEMP}/phpstan-report.json"
234+
if [ ! -f "${report}" ]; then
139235
exit 0
140236
fi
141237
142-
# Leave the run alone unless PHPStan reported an ignore error, because everything
143-
# below concerns an ignore configuration that no longer describes the code, and
144-
# nothing else. Those errors are `ignore.unmatched`, where a pattern matched nothing
145-
# at all, and `ignore.count`, where it matched a different number of times than the
146-
# entry records. The `ignore.` prefix is matched rather than those two names so that
147-
# any later addition to the group is caught as well.
148-
if ! grep -q 'source="ignore\.' "${RUNNER_TEMP}/phpstan-report.xml"; then
238+
# Errors that are not specific to a file are listed separately from the rest, and are
239+
# never ignorable.
240+
unignorable=$( jq '[ .files[].messages[] | select( .ignorable | not ) ] + .errors | length' "${report}" )
241+
if [ "${unignorable}" -eq 0 ]; then
149242
exit 0
150243
fi
151244
245+
# Whether the errors include ignore errors decides what to say about them, but not whether to
246+
# fail: every error that reaches this point fails the run. Ignore errors concern an ignore
247+
# configuration that no longer describes the code. They are `ignore.unmatched`, where a
248+
# pattern matched nothing at all, and `ignore.count`, where it matched a different number of
249+
# times than the entry records. The `ignore.` prefix is matched rather than those two names
250+
# so that any later addition to the group is caught as well. Any other error that cannot be
251+
# ignored is something regenerating the baselines would not help with, so it is reported
252+
# plainly instead.
253+
ignore_errors=$( jq '[ .files[].messages[] | select( .identifier // "" | startswith( "ignore." ) ) ] | length' "${report}" )
254+
152255
# The summary is Markdown, and its code spans and fences are written literally, so the
153256
# heredoc is quoted to keep the backticks out of the shell's hands. The links are
154257
# written in reference style for the same reason: the URLs are the only part needing
155258
# a variable, so defining them afterwards keeps the whole of the prose in here.
156259
#
157260
# A newline renders as a line break rather than a space, so each paragraph is one
158261
# line however long that makes it, and the rendered summary wraps to its own width.
159-
cat >> "${GITHUB_STEP_SUMMARY}" <<'SUMMARY'
262+
baselines_summary=$( cat <<'SUMMARY'
160263
## PHPStan baselines are out of date
161264
162265
An ignored error no longer occurs, or occurs a different number of times, so PHPStan reported it under an `ignore.unmatched` or `ignore.count` identifier.
@@ -182,13 +285,20 @@ jobs:
182285
See [`tests/phpstan/README.md`][readme] for details.
183286
184287
SUMMARY
288+
)
289+
290+
if [ "${ignore_errors}" -gt 0 ]; then
291+
{
292+
printf '%s\n\n' "${baselines_summary}"
293+
echo "[baselines]: ${BASELINES_URL}"
294+
echo "[readme]: ${README_URL}"
295+
} >> "${GITHUB_STEP_SUMMARY}"
185296
186-
{
187-
echo "[baselines]: ${BASELINES_URL}"
188-
echo "[readme]: ${README_URL}"
189-
} >> "${GITHUB_STEP_SUMMARY}"
297+
echo "::error title=PHPStan baselines are out of date::An ignored error no longer occurs, or occurs a different number of times. If you fixed it, that is expected: run \`npm run typecheck:php:baselines\` or \`composer phpstan:baselines\` and commit the updated baselines. See ${README_URL}"
298+
else
299+
echo "::error title=PHPStan reported errors that cannot be ignored::PHPStan reported ${unignorable} error(s) that cannot be ignored or reported as warnings. See the error annotations for details, and ${README_URL} for how to run the analysis locally."
300+
fi
190301
191-
echo "::error title=PHPStan baselines are out of date::An ignored error no longer occurs, or occurs a different number of times. If you fixed it, that is expected: run \`npm run typecheck:php:baselines\` or \`composer phpstan:baselines\` and commit the updated baselines. See ${README_URL}"
192302
exit 1
193303
194304
- name: "Save result cache"

‎.github/workflows/reusable-phpunit-tests-v3.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,4 @@ jobs:
435435
if: ${{ github.ref == 'refs/heads/trunk' && inputs.report }}
436436
env:
437437
WPT_REPORT_API_KEY: "${{ secrets.WPT_REPORT_API_KEY }}"
438-
run: docker compose run --rm -e WPT_REPORT_API_KEY -e WPT_PREPARE_DIR=/var/www -e WPT_TEST_DIR=/var/www php php test-runner/report.php
438+
run: docker compose run --rm -e WPT_REPORT_API_KEY -e WPT_PREPARE_DIR=/var/www -e WPT_TEST_DIR=/var/www -e WPT_DB_HOST=mysql -e WPT_DB_USER=root -e WPT_DB_PASSWORD=password -e WPT_DB_NAME=wordpress_develop_tests php php test-runner/report.php

0 commit comments

Comments
 (0)