diff --git a/.github/workflows/phpstan-pull-request-comments.yml b/.github/workflows/phpstan-pull-request-comments.yml index d1c38cc83fd10..170d770cd880b 100644 --- a/.github/workflows/phpstan-pull-request-comments.yml +++ b/.github/workflows/phpstan-pull-request-comments.yml @@ -57,6 +57,20 @@ jobs: run-id: ${{ github.event.workflow_run.id }} github-token: ${{ github.token }} + # The results of the analysis holding selected paths to a stricter rule level. A pull request whose branch does + # not have that analysis yet uploads none, which is fine. Whether they were expected is decided from the results + # above. + - name: Download the results of the strict analysis + id: download-strict + if: ${{ steps.download.outcome == 'success' }} + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + continue-on-error: true + with: + name: phpstan-results-strict + path: ${{ runner.temp }}/phpstan-results-strict + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ github.token }} + - name: Create, update, or resolve the PHPStan comment if: ${{ steps.download.outcome == 'success' }} uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 @@ -74,7 +88,7 @@ jobs: // anything at all. They are therefore checked for shape, and only accepted for the pull request whose // current head is the commit that the run analyzed. That also discards the results of a run that was // overtaken by a newer push. - const results = JSON.parse( fs.readFileSync( `${ process.env.RUNNER_TEMP }/phpstan-results/results.json`, 'utf8' ) ); + const results = JSON.parse( fs.readFileSync( `${ process.env.RUNNER_TEMP }/phpstan-results/results.json`, { encoding: 'utf8' } ) ); const issue_number = results.pull_request; if ( ! Number.isSafeInteger( issue_number ) || issue_number <= 0 || ! Array.isArray( results.errors ) ) { core.setFailed( 'The analysis results are malformed.' ); @@ -93,12 +107,62 @@ jobs: const asString = ( value ) => ( typeof value === 'string' ? value : null ); const asSha = ( value ) => ( typeof value === 'string' && /^[0-9a-f]{40}$/.test( value ) ? value : null ); + // A rule level is a number, or `max`. Results from a branch that does not report the level have none. + const asLevel = ( value ) => ( ( Number.isSafeInteger( value ) && value >= 0 ) || value === 'max' ? value : null ); const normalize = ( error ) => ( { file: asString( error.file ), line: Number.isSafeInteger( error.line ) && error.line > 0 ? error.line : null, message: ( asString( error.message ) ?? '' ).slice( 0, 1000 ), identifier: /^[A-Za-z0-9.]+$/.test( error.identifier ) ? error.identifier : null, ignorable: error.ignorable === true, + // The analysis that reported the error, and the rule level it analyzed at. Neither is reported per error: + // they are those of the results the error was read from, and are kept in the history from there. + analysis: error.analysis === 'strict' ? 'strict' : 'default', + level: asLevel( error.level ), + } ); + + // The paths held to a stricter rule level are analyzed a second time, and that analysis uploads its results + // separately. The results above list the analyses the run made, and when the strict one is among them but + // its results are missing, the comment is left as it is: every error that only analysis reported would + // otherwise be shown as resolved. A run from a branch that does not have that analysis yet lists none, and + // has only these results. + const expectsStrict = Array.isArray( results.analyses ) && results.analyses.includes( 'strict' ); + let strictErrors = []; + let strictLevel = null; + let strictChangedInBase = []; + if ( expectsStrict ) { + let strictResults = null; + try { + strictResults = JSON.parse( fs.readFileSync( `${ process.env.RUNNER_TEMP }/phpstan-results-strict/results.json`, { encoding: 'utf8' } ) ); + } catch ( error ) { + core.info( `The results of the strict analysis are missing, so the comment is left as it is: ${ error.message }` ); + return; + } + if ( strictResults?.pull_request !== issue_number || ! Array.isArray( strictResults.errors ) ) { + core.setFailed( 'The results of the strict analysis are malformed.' ); + return; + } + strictLevel = asLevel( strictResults.level ); + strictErrors = strictResults.errors.map( ( error ) => normalize( { ...error, analysis: 'strict', level: strictLevel } ) ); + strictChangedInBase = Array.isArray( strictResults.changed_in_base ) ? strictResults.changed_in_base : []; + } + + // The strict analysis includes everything the default one does, so an error it reports at a lower level is + // reported by both. Such an error is listed once, as found by the analysis of the whole codebase, and the + // strict one only adds what is left over. Errors are matched as they are below, apart from their line. + const errorKey = ( error ) => JSON.stringify( [ error.file, error.identifier, error.message ] ); + const defaultCounts = new Map(); + const defaultErrors = results.errors.map( ( error ) => normalize( { ...error, analysis: 'default', level: results.level } ) ); + for ( const error of defaultErrors ) { + defaultCounts.set( errorKey( error ), ( defaultCounts.get( errorKey( error ) ) ?? 0 ) + 1 ); + } + const strictOnly = strictErrors.filter( ( error ) => { + const count = defaultCounts.get( errorKey( error ) ) ?? 0; + if ( count > 0 ) { + defaultCounts.set( errorKey( error ), count - 1 ); + return false; + } + return true; } ); // An error is recognized from one run to the next by its file, identifier and message, but not its line, @@ -107,10 +171,10 @@ jobs: // is fixed or added, those after it shift to the history of the one before or after, so their first reports // can be off, and the one shown as resolved or new is the last rather than the one that was. That is rarer // than lines being added or removed above them, which would throw off matching them by line. - const errors = results.errors.map( normalize ); + const errors = [ ...defaultErrors, ...strictOnly ]; const occurrences = new Map(); for ( const error of [ ...errors ].sort( ( a, b ) => ( a.line ?? 0 ) - ( b.line ?? 0 ) ) ) { - const key = JSON.stringify( [ error.file, error.identifier, error.message ] ); + const key = errorKey( error ); const occurrence = occurrences.get( key ) ?? 0; occurrences.set( key, occurrence + 1 ); error.key = `${ key }#${ occurrence }`; @@ -164,7 +228,11 @@ jobs: // Files the base branch has changed since the pull request branched from it, in which the line numbers // reported can be off in the head commit, since the analysis ran on the head commit merged into the base. - const changedInBase = new Set( Array.isArray( results.changed_in_base ) ? results.changed_in_base.filter( ( file ) => typeof file === 'string' ) : [] ); + // Each analysis only lists those among the files it reported errors in, so the lists of both are combined. + const changedInBase = new Set( + [ ...( Array.isArray( results.changed_in_base ) ? results.changed_in_base : [] ), ...strictChangedInBase ] + .filter( ( file ) => typeof file === 'string' ) + ); const lineMayBeOff = ( error ) => ! error.fixed && Boolean( error.line ) && changedInBase.has( error.file ); const filesWithLinesOff = new Set( active.filter( lineMayBeOff ).map( ( error ) => error.file ) ); @@ -206,7 +274,10 @@ jobs: // GitHub shows only the first few annotations of each kind from a step on the page of the workflow run, so // errors left out of a table are pointed to in the log of the step that reported them, which has them all. - const where = `the log of the [workflow run](${ run.html_url }), in the “Report the analysis results” step`; + // Each analysis runs in a job of its own, and an error found only by the strict one is only in that job's log. + const where = strictOnly.length > 0 + ? `the log of the [workflow run](${ run.html_url }), in the “Report the analysis results” step of each of its jobs (errors in strict paths are only in the “strict paths” job)` + : `the log of the [workflow run](${ run.html_url }), in the “Report the analysis results” step`; const table = ( rows, rowLimit, { withResolution = false } = {} ) => { const shown = rows.slice( 0, rowLimit ); if ( shown.length === 0 ) { @@ -215,10 +286,14 @@ jobs: : `The ${ rows.length } errors are not listed here, to keep this comment short. They are listed in ${ where }.`; } const more = `…and ${ rows.length - shown.length } more, which are listed in ${ where }.`; + // The rule level is only shown in a table listing an error from the strict analysis, so that one listing + // only errors from the analysis of the whole codebase looks as it did before there were two. A level + // that was not reported, as by a branch from before it was, is left blank. + const withLevel = shown.some( ( error ) => error.analysis === 'strict' ); const lines = [ - `| Location | Error | Identifier | First reported |${ withResolution ? ' Resolved |' : '' }`, - `| --- | --- | --- | --- |${ withResolution ? ' --- |' : '' }`, - ...shown.map( ( error ) => `| ${ location( error ) } | ${ code( error.message ) } | ${ identifier( error ) } | ${ commit( error.introduced ) } |${ withResolution ? ` ${ commit( error.fixed ) } |` : '' }` ), + `| Location | Error | Identifier |${ withLevel ? ' Level |' : '' } First reported |${ withResolution ? ' Resolved |' : '' }`, + `| --- | --- | --- |${ withLevel ? ' --- |' : '' } --- |${ withResolution ? ' --- |' : '' }`, + ...shown.map( ( error ) => `| ${ location( error ) } | ${ code( error.message ) } | ${ identifier( error ) } |${ withLevel ? ` ${ error.level ?? '' } |` : '' } ${ commit( error.introduced ) } |${ withResolution ? ` ${ commit( error.fixed ) } |` : '' }` ), ]; if ( shown.length < rows.length ) { lines.push( '', more ); @@ -248,6 +323,11 @@ jobs: `> ${ filesWithLinesOff.size === 1 ? 'A file with errors has' : `${ filesWithLinesOff.size } files with errors have` } changed in ${ code( pr.base.ref ) } since this branch was last updated, so the line numbers reported in ${ filesWithLinesOff.size === 1 ? 'it' : 'them' } may be off and are not linked. Merge the latest ${ code( pr.base.ref ) } into this branch for accurate links.`, ].join( '\n' ) ); } + if ( active.some( ( error ) => error.analysis === 'strict' ) ) { + const strictError = strictLevel === null ? 'An error found by the strict analysis' : `An error at level ${ strictLevel }`; + const stricterLevel = strictLevel === null ? 'a stricter rule level' : `rule level ${ strictLevel }`; + sections.push( `${ strictError } is in a path that ${ code( 'phpstan-strict.neon.dist' ) } holds to ${ stricterLevel }, which the rest of the codebase is not yet held to. See [Stricter analysis of selected paths](${ readmeUrl }#stricter-analysis-of-selected-paths).` ); + } } else { // Resolved by the last commit that resolved anything, so that later commits do not change the comment. const resolvedBy = resolved.at( -1 )?.fixed ?? run.head_sha; @@ -316,8 +396,8 @@ jobs: // The errors tracked are taken in the order they are listed, so that each one listed keeps its history. const tracked = [ ...unignorable, ...ignorable ].slice( 0, trackedLimit ).concat( resolved ).map( ( error ) => { - const { key, file, line, message, identifier, ignorable, introduced, seen, fixed } = error; - return { key, file, line: lineMayBeOff( error ) ? null : line, message, identifier, ignorable, introduced, seen, fixed }; + const { key, file, line, message, identifier, ignorable, analysis, level, introduced, seen, fixed } = error; + return { key, file, line: lineMayBeOff( error ) ? null : line, message, identifier, ignorable, analysis, level, introduced, seen, fixed }; } ); sections.push( `` ); diff --git a/.github/workflows/phpstan-static-analysis.yml b/.github/workflows/phpstan-static-analysis.yml index f0eaad6d015f3..3d855296e0e0a 100644 --- a/.github/workflows/phpstan-static-analysis.yml +++ b/.github/workflows/phpstan-static-analysis.yml @@ -17,8 +17,10 @@ on: - 'composer.*' # These files configure PHPStan. Changes could affect the outcome. - 'phpstan.neon.dist' + - 'phpstan-strict.neon.dist' - 'tests/phpstan/base.neon' - 'tests/phpstan/baselines/**' + - 'tests/phpstan/baselines-strict/**' # Confirm any changes to relevant workflow files. - '.github/workflows/phpstan-static-analysis.yml' - '.github/workflows/reusable-phpstan-static-analysis-v1.yml' diff --git a/.github/workflows/reusable-phpstan-static-analysis-v1.yml b/.github/workflows/reusable-phpstan-static-analysis-v1.yml index 5474a810164cd..aba7162bb2a05 100644 --- a/.github/workflows/reusable-phpstan-static-analysis-v1.yml +++ b/.github/workflows/reusable-phpstan-static-analysis-v1.yml @@ -21,8 +21,11 @@ permissions: {} jobs: # Runs PHP static analysis tests. # - # Violations are reported inline with annotations. For pull requests, the results are also uploaded - # as an artifact, which the PHPStan Pull Request Comments workflow summarizes in a comment. + # Core is analyzed twice, in parallel: the whole codebase with phpstan.neon.dist, and the paths held to a stricter rule + # level with phpstan-strict.neon.dist. See tests/phpstan/README.md. + # + # Violations are reported inline with annotations. For pull requests, the results of each analysis are also uploaded + # as an artifact, and the PHPStan Pull Request Comments workflow combines them in a comment. # # Performs the following steps: # - Checks out the repository. @@ -39,11 +42,33 @@ jobs: # - Saves the PHPStan result cache. # - Ensures version-controlled files are not modified or deleted. phpstan: - name: Run PHP static analysis + name: Run PHP static analysis (${{ matrix.label }}) runs-on: ubuntu-26.04 permissions: contents: read timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + include: + # The artifact of the analysis of the whole codebase keeps the name it had before there were two, which is + # the name the PHPStan Pull Request Comments workflow on trunk requires. That way the comment is kept up to + # date for a pull request whose branch does not have the second analysis yet. + # + # The analyses are named for their role rather than their rule level, which is read from the configuration + # when the results are reported, so that raising either level is an edit of its configuration alone. + - analysis: default + label: all paths + config: phpstan.neon.dist + baselines: tests/phpstan/baselines + script: phpstan:default + artifact: phpstan-results + - analysis: strict + label: strict paths + config: phpstan-strict.neon.dist + baselines: tests/phpstan/baselines-strict + script: phpstan:strict + artifact: phpstan-results-strict steps: # For a pull request, the parents of the merge commit are fetched too, to find the files that the base branch @@ -92,16 +117,21 @@ jobs: # merged this commit yet runs the workflow it has, and that workflow would prefix-match the # caches written here: it would analyse without the extensions against reflection derived with # them. A name of its own leaves those runs restoring only their own caches. + # + # Each analysis caches separately. Its result cache records the rule level, and is discarded + # by a run at another level, so restoring the other analysis's cache would gain nothing. - name: Cache PHP Static Analysis scan cache uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 with: path: .cache # This is defined in the base.neon file. - key: "phpstan-analysis-cache-${{ hashFiles('phpstan.neon.dist', 'tests/phpstan/*.neon', 'tests/phpstan/*.php') }}-${{ github.run_id }}" + key: "phpstan-analysis-cache-${{ matrix.analysis }}-${{ hashFiles('phpstan.neon.dist', 'phpstan-strict.neon.dist', 'tests/phpstan/*.neon', 'tests/phpstan/*.php') }}-${{ github.run_id }}" restore-keys: | - phpstan-analysis-cache-${{ hashFiles('phpstan.neon.dist', 'tests/phpstan/*.neon', 'tests/phpstan/*.php') }}- + phpstan-analysis-cache-${{ matrix.analysis }}-${{ hashFiles('phpstan.neon.dist', 'phpstan-strict.neon.dist', 'tests/phpstan/*.neon', 'tests/phpstan/*.php') }}- - name: Run PHP static analysis tests id: phpstan + env: + SCRIPT: ${{ matrix.script }} run: | # The report is written to a file for the steps below, which turn it into annotations, # the pull request comment, and a check for errors that cannot be reported as warnings. @@ -111,7 +141,7 @@ jobs: # The step shell exits on the first failing command, which would also end the step on # the status of 1 that PHPStan uses for reporting errors. Capture the status instead. status=0 - composer run phpstan -- -vvv --error-format=json > "${RUNNER_TEMP}/phpstan-report.json" || status=$? + composer run "${SCRIPT}" -- -vvv --error-format=json > "${RUNNER_TEMP}/phpstan-report.json" || status=$? # PHPStan exits 1 when it has errors to report, which is the expected case here and # is what the annotations are for. Anything higher means it did not finish at all. @@ -132,11 +162,36 @@ jobs: - name: Report the analysis results id: report uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + ANALYSIS: ${{ matrix.analysis }} + CONFIG: ${{ matrix.config }} with: script: | const fs = require( 'fs' ); - const report = JSON.parse( fs.readFileSync( `${ process.env.RUNNER_TEMP }/phpstan-report.json`, 'utf8' ) ); + // The rule level the configuration analyzes at, as a number, or `max`. Only the line of the parameter is + // matched, rather than decoding the whole dump, because the JSON PHPStan prints can be invalid: it writes + // the `\<` in a message pattern as is, which is not a valid JSON escape. + let level = null; + try { + const parameters = require( 'child_process' ).execFileSync( + `${ process.env.GITHUB_WORKSPACE }/vendor/bin/phpstan`, + [ 'dump-parameters', `--configuration=${ process.env.CONFIG }`, '--json' ], + { encoding: 'utf8' } + ); + const match = parameters.match( /^\s*"level":\s*"?(max|\d+)"?,?$/m ); + if ( match ) { + level = match[ 1 ] === 'max' ? 'max' : Number( match[ 1 ] ); + } + } catch ( error ) { + core.warning( `The rule level of ${ process.env.CONFIG } could not be read: ${ error.message }` ); + } + + // Annotations from the stricter analysis say so, since the same code can pass the other one. + const analysis = process.env.ANALYSIS; + const titlePrefix = analysis === 'strict' ? `PHPStan (strict${ level === null ? '' : `, level ${ level }` })` : 'PHPStan'; + + const report = JSON.parse( fs.readFileSync( `${ process.env.RUNNER_TEMP }/phpstan-report.json`, { encoding: 'utf8' } ) ); const workspace = `${ process.env.GITHUB_WORKSPACE }/`; const errors = []; @@ -165,7 +220,7 @@ jobs: const fixHint = 'This can be fixed automatically by running `composer phpstan:fix` (or `npm run typecheck:php:fix`).'; for ( const error of errors ) { - const properties = { title: error.identifier ? `PHPStan: ${ error.identifier }` : 'PHPStan' }; + const properties = { title: error.identifier ? `${ titlePrefix }: ${ error.identifier }` : titlePrefix }; if ( error.file ) { properties.file = error.file; } @@ -186,6 +241,10 @@ jobs: // The commit analyzed is the pull request merged into its base branch, whose second parent is the head // commit. The files with errors that differ between the two are those the base branch has changed, in // which the line numbers reported can be off in the head commit. + // + // Each analysis writes its own results. Those of the analysis of the whole codebase also list every + // analysis the run makes, so that the comment is not updated from only some of them: an error missing + // because its analysis did not upload results would otherwise be shown as resolved. if ( context.eventName === 'pull_request' ) { let changedInBase = []; try { @@ -200,6 +259,10 @@ jobs: fs.mkdirSync( `${ process.env.RUNNER_TEMP }/phpstan-results`, { recursive: true } ); fs.writeFileSync( `${ process.env.RUNNER_TEMP }/phpstan-results/results.json`, JSON.stringify( { pull_request: context.payload.pull_request.number, + analysis, + level, + // Keep this list in step with the `analysis` values of the job's matrix. + ...( analysis === 'default' ? { analyses: [ 'default', 'strict' ] } : {} ), changed_in_base: changedInBase, errors: errors.map( ( { tip, fixable, ...error } ) => error ), } ) ); @@ -211,7 +274,7 @@ jobs: uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 if: ${{ github.event_name == 'pull_request' && steps.report.outcome == 'success' }} with: - name: phpstan-results + name: ${{ matrix.artifact }} path: ${{ runner.temp }}/phpstan-results/ # Some errors cannot be ignored, so they cannot be reported as warnings either. Most often @@ -229,7 +292,8 @@ jobs: - name: Check for errors that cannot be reported as warnings if: ${{ !cancelled() }} env: - BASELINES_URL: ${{ github.server_url }}/${{ github.repository }}/tree/${{ github.sha }}/tests/phpstan/baselines + BASELINES_URL: ${{ github.server_url }}/${{ github.repository }}/tree/${{ github.sha }}/${{ matrix.baselines }} + LABEL: ${{ matrix.label }} README_URL: ${{ github.server_url }}/${{ github.repository }}/blob/${{ github.sha }}/tests/phpstan/README.md run: | # This step runs even when the analysis above it failed, in which case the report may @@ -264,8 +328,11 @@ jobs: # # A newline renders as a line break rather than a space, so each paragraph is one # line however long that makes it, and the rendered summary wraps to its own width. + # + # The heading names the analysis whose baselines are out of date, which is filled in for + # the LABEL placeholder when the summary is written, since the heredoc expands nothing. baselines_summary=$( cat <<'SUMMARY' - ## PHPStan baselines are out of date + ## PHPStan baselines are out of date (LABEL) 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. @@ -283,7 +350,7 @@ jobs: composer phpstan:fix ``` - That runs PHPStan with `--fix`, which lowers the counts in the files under [`tests/phpstan/baselines`][baselines], removes the entries that no longer match, and removes any `@phpstan-ignore` annotation in the code that no longer ignores anything. It then deletes any baseline left with no entries. + That runs PHPStan with `--fix`, which lowers the counts in [the baselines][baselines], removes the entries that no longer match, and removes any `@phpstan-ignore` annotation in the code that no longer ignores anything. It then deletes any baseline left with no entries. Alternatively, regenerate the baselines with `npm run typecheck:php:baselines` or `composer phpstan:baselines`. That also deletes any baseline whose errors are now all fixed, but it does not clear an `@phpstan-ignore` annotation. @@ -294,14 +361,14 @@ jobs: if [ "${ignore_errors}" -gt 0 ]; then { - printf '%s\n\n' "${baselines_summary}" + printf '%s\n\n' "${baselines_summary/LABEL/${LABEL}}" echo "[baselines]: ${BASELINES_URL}" echo "[readme]: ${README_URL}" } >> "${GITHUB_STEP_SUMMARY}" - 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:fix\` or \`composer phpstan:fix\` and commit the updated baselines. See ${README_URL}" + echo "::error title=PHPStan baselines are out of date (${LABEL})::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:fix\` or \`composer phpstan:fix\` and commit the updated baselines. See ${README_URL}" else - 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." + echo "::error title=PHPStan reported errors that cannot be ignored (${LABEL})::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." fi exit 1 @@ -311,7 +378,7 @@ jobs: if: ${{ !cancelled() }} with: path: .cache - key: "phpstan-analysis-cache-${{ hashFiles('phpstan.neon.dist', 'tests/phpstan/*.neon', 'tests/phpstan/*.php') }}-${{ github.run_id }}" + key: "phpstan-analysis-cache-${{ matrix.analysis }}-${{ hashFiles('phpstan.neon.dist', 'phpstan-strict.neon.dist', 'tests/phpstan/*.neon', 'tests/phpstan/*.php') }}-${{ github.run_id }}" - name: Ensure version-controlled files are not modified or deleted run: git diff --exit-code diff --git a/.gitignore b/.gitignore index 2d0178f495120..24b4282326304 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ wp-tests-config.php /tests/phpunit/build /wp-cli.local.yml /phpstan.neon +/phpstan-strict.neon /*.tsbuildinfo /jsdoc /composer.lock diff --git a/composer.json b/composer.json index dd7f57d5b6313..f17e0922e00d1 100644 --- a/composer.json +++ b/composer.json @@ -75,9 +75,11 @@ } }, "scripts": { - "phpstan": "@php ./vendor/bin/phpstan analyse --memory-limit=2G", + "phpstan": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/analyse.php --memory-limit=2G" ], + "phpstan:default": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/bin/phpstan analyse --configuration=phpstan.neon.dist --memory-limit=2G" ], + "phpstan:strict": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/bin/phpstan analyse --configuration=phpstan-strict.neon.dist --memory-limit=2G" ], "phpstan:baselines": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/generate-baselines.php" ], - "phpstan:fix": [ "Composer\\Config::disableProcessTimeout", "@phpstan --configuration=phpstan.neon.dist --fix", "@phpstan:baselines --prune", "@phpstan --configuration=phpstan.neon.dist" ], + "phpstan:fix": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/analyse.php --memory-limit=2G --fix" ], "compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source", "format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source", "lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source", diff --git a/phpstan-strict.neon.dist b/phpstan-strict.neon.dist new file mode 100644 index 0000000000000..ffa58a226d576 --- /dev/null +++ b/phpstan-strict.neon.dist @@ -0,0 +1,91 @@ +# PHPStan configuration holding selected parts of WordPress Core to a stricter rule level. +# +# phpstan.neon.dist analyzes the whole codebase at a lower rule level. This analyzes only the paths listed under +# `paths!` below, at the `level` set below, so that code which already meets that stricter level, mostly code added in +# recent releases, keeps meeting it. `composer phpstan` runs both analyses. +# +# The rest of core is scanned rather than analyzed, so that what the strict paths use is known. A scanned file is read +# with the same WordPress-specific parser node visitors as an analyzed one (see the redefinition of +# `currentPhpVersionSimpleParser` in tests/phpstan/base.neon), so a function outside the strict paths has the same +# types here, such as the array shapes from hash notation, as in the analysis of the whole codebase. +# +# To override this configuration, create a phpstan-strict.neon which includes this file, in the same way as +# phpstan.neon overrides phpstan.neon.dist. +# +# https://phpstan.org/config-reference + +includes: + # Everything phpstan.neon.dist enforces: the base configuration, the strict rules, the errors ignored as not + # actionable, and its baselines. An error that both analyses report is baselined once, by phpstan.neon.dist. The + # entries for files this does not analyze are not reported as unmatched, since PHPStan only reports an entry when + # what it ignores is within the analysis. + - phpstan.neon.dist + + # Preexisting errors at this level in the strict paths, beyond those baselined by phpstan.neon.dist. They are + # organized and regenerated the same way, by `composer phpstan:baselines`, and included the same way, through the + # loader in their directory. + - tests/phpstan/baselines-strict/loader.php + +parameters: + # The level is set explicitly rather than as `max`, so that a PHPStan update that adds a level does not raise it. + # https://phpstan.org/user-guide/rule-levels + level: 10 + + # The two analyses share the cache of what PHPStan reads from each file, since both read every file the same way, + # but each needs its own result cache. Its metadata includes the rule level and the paths analyzed, so a shared one + # would be discarded by every run of the other analysis. + resultCachePath: %tmpDir%/resultCache-strict.php + + # The paths held to this rule level, replacing those analyzed by phpstan.neon.dist. A directory covers every file + # below it. + # + # Before adding a path, fix the errors this analysis reports in it, or baseline those that cannot be fixed yet. + paths!: + - src/wp-admin/includes/class-wp-filesystem-base.php + - src/wp-admin/includes/class-wp-filesystem-direct.php + - src/wp-admin/includes/class-wp-filesystem-ssh2.php + - src/wp-includes/abilities-api + - src/wp-includes/abilities-api.php + - src/wp-includes/ai-client + - src/wp-includes/ai-client.php + - src/wp-includes/class-wp-block-styles-registry.php + - src/wp-includes/class-wp-block-template.php + - src/wp-includes/class-wp-comment.php + - src/wp-includes/class-wp-connector-registry.php + - src/wp-includes/class-wp-error.php + - src/wp-includes/class-wp-post.php + - src/wp-includes/class-wp-url-pattern-prefixer.php + - src/wp-includes/html-api/class-wp-html-attribute-token.php + - src/wp-includes/html-api/class-wp-html-decoder.php + - src/wp-includes/html-api/class-wp-html-doctype-info.php + - src/wp-includes/html-api/class-wp-html-processor-state.php + - src/wp-includes/html-api/class-wp-html-span.php + - src/wp-includes/html-api/class-wp-html-stack-event.php + - src/wp-includes/html-api/class-wp-html-text-replacement.php + - src/wp-includes/html-api/class-wp-html-token.php + - src/wp-includes/html-api/class-wp-html-unsupported-exception.php + - src/wp-includes/l10n/class-wp-translations.php + - src/wp-includes/sitemaps.php + - src/wp-includes/speculative-loading.php + - src/wp-includes/style-engine + - tests/phpstan + + # The code outside the strict paths that they can use. The bundled themes are left out, since nothing in core uses + # them. The files in the root are scanned for the constants they define, such as WPINC in wp-settings.php. + scanDirectories: + - src/wp-admin + - src/wp-includes + scanFiles: + - src/index.php + - src/wp-activate.php + - src/wp-blog-header.php + - src/wp-comments-post.php + - src/wp-cron.php + - src/wp-links-opml.php + - src/wp-load.php + - src/wp-login.php + - src/wp-mail.php + - src/wp-settings.php + - src/wp-signup.php + - src/wp-trackback.php + - src/xmlrpc.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 260afa934953f..796646255ada6 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,8 @@ # PHPStan configuration for WordPress Core. # +# This analyzes the whole codebase. phpstan-strict.neon.dist includes it, and holds selected +# paths to a stricter rule level. `composer phpstan` runs both. +# # To override this configuration, create a phpstan.neon which includes this file and adjusts # its parameters as needed, rather than copying it, so that the copy does not go stale. See # tests/phpstan/README.md for examples. diff --git a/tests/phpstan/README.md b/tests/phpstan/README.md index 7dd1df431d4a9..056c4acdfb077 100644 --- a/tests/phpstan/README.md +++ b/tests/phpstan/README.md @@ -16,6 +16,8 @@ npm run typecheck:php which will run PHPStan in the Docker container. +This runs two analyses, one after the other: the whole codebase with [`phpstan.neon.dist`](../../phpstan.neon.dist), and then the paths held to a stricter rule level with [`phpstan-strict.neon.dist`](../../phpstan-strict.neon.dist), as described in [Stricter analysis of selected paths](#stricter-analysis-of-selected-paths). The command fails when either of them reports an error. + Additional flags supported by PHPStan can be passed by passing `--` followed by the flags themselves. For example, ```bash @@ -41,14 +43,16 @@ composer run phpstan -- -vvv --debug Note the `--` in each of those. Composer needs it in order to pass the flags on to PHPStan rather than reading them as its own, and without it they are discarded silently. The npm script supplies it, which is why only one is needed there. +The flags are passed to both analyses, except that paths, `--configuration`, `--level`, `--error-format`, `--generate-baseline`, and `--fix-ignored` run only the first, since each only makes sense for one. To run only one of them, as CI does, use `composer phpstan:default` or `composer phpstan:strict`. Those always use the `.dist` configurations, ignoring a local override. + For available flags, see https://phpstan.org/user-guide/command-line-usage. ## Results on pull requests -The analysis also runs on every pull request that changes PHP files or the PHPStan configuration. The errors it reports are shown in two places: +Both analyses also run on every pull request that changes PHP files or the PHPStan configuration, as two jobs. The errors they report are shown in two places: - As annotations on the changed files. Most errors are annotated as warnings, which do not fail the check. -- In a comment on the pull request, which lists every error in a table along with the commit it was first reported for. The comment is updated as new commits are pushed, moving each error that a commit resolves into a collapsed list of resolved errors, which also names that commit. Once all of the errors are resolved, the comment is marked as resolved and collapsed. +- In a comment on the pull request, which lists every error in a table along with the commit it was first reported for. The comment is updated as new commits are pushed, moving each error that a commit resolves into a collapsed list of resolved errors, which also names that commit. Once all of the errors are resolved, the comment is marked as resolved and collapsed. The comment combines both analyses: an error reported by both is listed once, and when an error is only reported by the analysis of the strict paths, the table gains a Level column giving the rule level of each error. Some errors cannot be ignored, and so cannot be reported as warnings either. These fail the check. The most common is an [out-of-date baseline](#removing-stale-entries-with---fix), which is reported when an error recorded in a baseline was fixed without the baseline being updated. Such an error is reported against a file rather than a line in it, so its annotation does not appear alongside the changes, and it would easily go unnoticed if the check passed. @@ -104,6 +108,25 @@ parameters: ignoreErrors!: [] ``` +### Stricter analysis of selected paths + +`phpstan.neon.dist` holds the whole codebase to a low rule level, since most of it does not yet meet a higher one. Some of it already does, mostly code added in recent releases, such as the AI client and the Abilities API. [`phpstan-strict.neon.dist`](../../phpstan-strict.neon.dist) holds those paths to a stricter rule level, currently 10, so that they keep meeting it. The level is set as a number rather than `max`, so that it is only raised deliberately, not by a PHPStan update adding a level. + +`phpstan-strict.neon.dist` includes `phpstan.neon.dist`, so everything `phpstan.neon.dist` enforces still applies, and replaces the paths analyzed with the ones it lists under `paths!`. The rest of core is scanned instead, so that what those paths use is known. A scanned file gets the same [WordPress-specific extensions](#wordpress-specific-extensions) as an analyzed one, because `base.neon` has every parse run them, so the types of the code outside the strict paths are the same as in the analysis of the whole codebase. + +Errors are baselined in two layers. An error that the analysis of the whole codebase reports is baselined in [`baselines/`](baselines), as usual, and stays baselined there for the strict analysis. What the stricter level adds in the strict paths is baselined in [`baselines-strict/`](baselines-strict), in the same way, and included through its own [`loader.php`](baselines-strict/loader.php). `composer phpstan:baselines` and `composer phpstan:fix` maintain both. + +To hold another path to the stricter level: + +1. Make sure no entry in [`baselines/`](baselines) is for a file in the path. Both analyses enforce those entries, but each may report a different number of the errors one ignores, and then no count satisfies both. Fix those errors first. +2. Add the path to `paths!` in `phpstan-strict.neon.dist`. +3. Run `composer phpstan:strict`, and fix the errors it reports. +4. Baseline any that cannot be fixed yet with `composer phpstan:baselines`. + +In the strict paths, **do not add an `@phpstan-ignore` comment for an error that only the stricter level reports**. The analysis of the whole codebase does not report that error, so it reports the comment as ignoring nothing, and `composer phpstan:fix` would remove it. Fix the error, or baseline it instead. + +A local `phpstan-strict.neon` overrides `phpstan-strict.neon.dist`, the same way `phpstan.neon` overrides `phpstan.neon.dist`. When a local `phpstan.neon` analyzes at the strict level or above, `composer phpstan` skips the second analysis, since it could only repeat part of the first. Only the levels are compared, so if the local `phpstan.neon` also narrows the paths analyzed, leaving out some of the strict paths, run `composer phpstan:strict` as well. + For more information about configuring PHPStan, see the [PHPStan documentation's Config reference](https://phpstan.org/config-reference). ## WordPress-specific extensions @@ -234,16 +257,15 @@ or, outside the Docker environment: composer phpstan:fix ``` -This runs three steps, each only when the one before it succeeds: +This runs three steps: -1. PHPStan with its `--fix` option, which lowers the count of the entry, or removes the entry, leaving the rest of the baseline as it was. It is usually much faster than regenerating, since it reuses the result cache of the run that reported the errors. Unlike regenerating, it also removes an `@phpstan-ignore` comment that no longer matches. +1. PHPStan with its `--fix` option, once with `phpstan.neon.dist` and once with `phpstan-strict.neon.dist`. It lowers the count of the entry, or removes the entry, leaving the rest of the baseline as it was. It is usually much faster than regenerating, since it reuses the result cache of the run that reported the errors. Unlike regenerating, it also removes an `@phpstan-ignore` comment that no longer matches. PHPStan says `No fixable errors found` for a configuration with nothing to fix, which does not stop the other steps. 2. `composer phpstan:baselines -- --prune`, which deletes any baseline left with no entries. Removing the last entry of a baseline leaves the file behind, as `ignoreErrors: []`. Pruning analyzes nothing, so it can also be run on its own. -3. The analysis once more, which reuses the result cache and so is quick. PHPStan's `--fix` reports only what it fixed, and succeeds even when other errors remain, so this is what shows the errors that are left. +3. Both analyses once more, which reuse the result cache and so are quick. PHPStan's `--fix` reports only what it fixed, and succeeds even when other errors remain, so this is what shows the errors that are left. Keep in mind: -- **It fails when nothing is fixable.** PHPStan then says `No fixable errors found` and stops the script there, so run `composer phpstan` to see whether any errors remain. -- **It always analyzes the whole tree with `phpstan.neon.dist`**, whatever a local `phpstan.neon` says. PHPStan only reports stale ignores when it analyzes every path, and a configuration at another rule level can report a baselined error with a different message, in which case `--fix` would remove an entry that the default configuration still needs. +- **It always analyzes with the two `.dist` configurations**, whatever a local `phpstan.neon` or `phpstan-strict.neon` says. A configuration at another rule level can report a baselined error with a different message, in which case `--fix` would remove an entry that the default configuration still needs. - **It fixes every fixable error the run reports**, not only stale ignores. A few other errors can also be fixed automatically, such as a [useless cast](https://phpstan.org/error-identifiers/cast.useless), so check the diff before committing it. - PHPStan's `--fix-ignored`, run as `composer phpstan -- --fix-ignored`, goes further and also fixes the fixable errors which a baseline currently ignores. That is a way to work through a baseline, but expect larger diffs, and follow it with `composer phpstan:baselines -- --prune`. @@ -294,6 +316,8 @@ Note the `--` in each of those. Composer needs it in order to pass the flags on A run also deletes any baseline whose identifier no longer reports anything. Since the baselines are included through [`baselines/loader.php`](baselines/loader.php), adding a newly split out baseline, and retiring one that has reached zero, need no edit of the configuration. +Each run regenerates both sets of baselines: those of `phpstan.neon.dist` in [`baselines/`](baselines), and then those of `phpstan-strict.neon.dist` in [`baselines-strict/`](baselines-strict). The order matters, since the strict configuration includes the other one along with its baselines, so its own are only what is left over. The options apply to both, except `--combined`, which only prints the baseline of `phpstan.neon.dist`. To regenerate one set, name its configuration with `--config`, such as `--config=phpstan-strict.neon.dist`. + PHPStan's own `--generate-baseline` is deliberately not used directly. It captures every error a run reports, with no way to restrict it to one identifier, so it cannot refresh a single baseline without sweeping every other kind of error into it. ## Performance and troubleshooting diff --git a/tests/phpstan/analyse.php b/tests/phpstan/analyse.php new file mode 100644 index 0000000000000..816198c483114 --- /dev/null +++ b/tests/phpstan/analyse.php @@ -0,0 +1,276 @@ + 1 ) { + exit( $status ); + } + } + + $status = run( 'Deleting the baselines left without entries', __DIR__ . '/generate-baselines.php', array( '--prune' ) ); + if ( 0 !== $status ) { + exit( $status ); + } + + $exit_code = 0; + foreach ( array( PROJECT_CONFIG, STRICT_CONFIG ) as $config ) { + $exit_code = max( $exit_code, analyse( "Analyzing with $config", $phpstan, array_merge( array( "--configuration=$config" ), $args ) ) ); + } + exit( $exit_code ); +} + +$local_config = $repo_root . '/phpstan.neon'; +$strict_config = is_file( $repo_root . '/phpstan-strict.neon' ) ? 'phpstan-strict.neon' : STRICT_CONFIG; + +// PHPStan reads phpstan.neon in place of phpstan.neon.dist by itself, so the first analysis names neither. +$exit_code = analyse( 'Analyzing with ' . ( is_file( $local_config ) ? 'phpstan.neon' : PROJECT_CONFIG ), $phpstan, $args ); + +if ( is_file( $local_config ) ) { + $local_level = get_level( $phpstan, $local_config ); + $strict_level = get_level( $phpstan, $repo_root . '/' . $strict_config ); + + if ( null !== $local_level && null !== $strict_level && is_level_at_least( $local_level, $strict_level ) ) { + fwrite( STDERR, "\nSkipping $strict_config: phpstan.neon analyzes at level $local_level, and the strict paths are held to $strict_level. If phpstan.neon leaves any of them out, run `composer phpstan:strict` too.\n" ); + exit( $exit_code ); + } +} + +exit( max( $exit_code, analyse( "Analyzing the strict paths with $strict_config", $phpstan, array_merge( array( "--configuration=$strict_config" ), $args ) ) ) ); + +/** + * Runs a command with a heading, passing its output through. + * + * The heading goes to stderr, so that what the command writes to stdout, such as + * a report in a machine-readable format, is left as it is. + * + * The script runs on the PHP running this one, which Composer's `@php` chose, + * rather than on whichever `php` the script's shebang finds first. + * + * @param string $heading Heading describing the run. + * @param string $script Path to the PHP script to run. + * @param string[] $args Arguments, which are escaped for the shell here. + * @return int Exit status of the script. + */ +function run( string $heading, string $script, array $args ): int { + fwrite( STDERR, "\n== $heading ==\n\n" ); + + $parts = array( escapeshellarg( PHP_BINARY ), escapeshellarg( $script ) ); + foreach ( $args as $arg ) { + $parts[] = escapeshellarg( $arg ); + } + + $exit_code = 0; + passthru( implode( ' ', $parts ), $exit_code ); + + return $exit_code; +} + +/** + * Runs PHPStan's `analyse` command with a heading, passing its output through. + * + * @param string $heading Heading describing the run. + * @param string $phpstan Path to the PHPStan executable. + * @param list $args Arguments to the command, which are escaped for the shell. + * @return int Exit status of PHPStan. + */ +function analyse( string $heading, string $phpstan, array $args ): int { + return run( $heading, $phpstan, array( 'analyse', ...$args ) ); +} + +/** + * Gets the rule level a configuration analyzes at. + * + * Only the line of the parameter is matched, rather than decoding the whole dump, + * because the JSON PHPStan prints can be invalid: it writes the `\<` in a message + * pattern as is, which is not a valid JSON escape. + * + * @param string $phpstan Path to the PHPStan executable. + * @param string $config Absolute path to the configuration file. + * @return string|null The rule level, as digits or `max`, or null if it could not be read. + */ +function get_level( string $phpstan, string $config ): ?string { + $output = array(); + $exit_code = 0; + exec( sprintf( '%s %s dump-parameters --configuration=%s --json 2>/dev/null', escapeshellarg( PHP_BINARY ), escapeshellarg( $phpstan ), escapeshellarg( $config ) ), $output, $exit_code ); + + // A configuration PHPStan cannot load, for one, leaves no parameters to read. + if ( 0 !== $exit_code ) { + return null; + } + + foreach ( $output as $line ) { + if ( 1 === preg_match( '/^\s*"level":\s*("max"|"?\d+"?),?$/', $line, $matches ) ) { + return trim( $matches[1], '"' ); + } + } + + return null; +} + +/** + * Determines whether a rule level is at least as strict as another. + * + * `max` is the highest level of whichever PHPStan version is installed, so it is + * at least any other. A numbered level is not taken to be at least `max`, since + * that would only hold until PHPStan adds a level above it. + * + * @param string $level Rule level, as digits or `max`. + * @param string $minimum Rule level to compare it with, as digits or `max`. + * @return bool Whether the level is at least the minimum. + */ +function is_level_at_least( string $level, string $minimum ): bool { + if ( 'max' === $level ) { + return true; + } + if ( 'max' === $minimum ) { + return false; + } + return (int) $level >= (int) $minimum; +} + +/** + * Finds an argument that only makes sense for a single analysis. + * + * Those are a configuration, a level, a report format, a baseline to generate, + * `--fix-ignored`, and the paths to analyze. An option's value can follow it as the + * next argument, so that is skipped rather than taken for a path. + * + * @param list $args Arguments. + * @return string|null Description of the first such argument, or null if there is none. + */ +function get_single_analysis_reason( array $args ): ?string { + // The options whose value can be given as the next argument rather than after `=`. + $takes_value = array( '-c', '--configuration', '-l', '--level', '-a', '--autoload-file', '--error-format', '--memory-limit', '--tmp-file', '--instead-of' ); + $single = array( '-c', '--configuration', '-l', '--level', '--error-format', '--generate-baseline', '-b', '--fix-ignored' ); + + $is_value = false; + foreach ( $args as $i => $arg ) { + // The value of the option before, given as an argument of its own. + if ( $is_value ) { + $is_value = false; + continue; + } + + if ( '--' === $arg ) { + return $i + 1 < count( $args ) ? 'the paths given' : null; + } + + if ( '' === $arg || '-' !== $arg[0] ) { + return 'the paths given'; + } + + // A long option's value follows `=`, and a short option's follows its letter directly, as in `-l6`. + if ( 0 === strpos( $arg, '--' ) ) { + $name = explode( '=', $arg, 2 )[0]; + $has_value = false !== strpos( $arg, '=' ); + } else { + $name = substr( $arg, 0, 2 ); + $has_value = strlen( $arg ) > 2; + } + + if ( in_array( $name, $single, true ) ) { + return $name; + } + + if ( ! $has_value && in_array( $name, $takes_value, true ) ) { + $is_value = true; + } + } + + return null; +} diff --git a/tests/phpstan/baselines-strict/argument.type.neon b/tests/phpstan/baselines-strict/argument.type.neon new file mode 100644 index 0000000000000..dd83b950de430 --- /dev/null +++ b/tests/phpstan/baselines-strict/argument.type.neon @@ -0,0 +1,60 @@ +# PHPStan baseline for the `argument.type` errors in WordPress core. +# +# https://phpstan.org/error-identifiers/argument.type +# +# Each entry is scoped to a single file and carries an exact occurrence count, +# so that a new instance is reported as a new error rather than being absorbed +# silently. Fixing an occurrence therefore means decrementing or removing its +# entry here as part of the same change. +# +# The goal is to empty this file and delete it. +# +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: +# +# composer phpstan:baselines -- --identifier=argument.type --config=phpstan-strict.neon.dist +# +# This reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. + +parameters: + ignoreErrors: + - + message: '#^Parameter \#1 \$object_or_string of function is_a expects object\|string, mixed given\.$#' + identifier: argument.type + count: 1 + path: ../../../src/wp-includes/abilities-api/class-wp-abilities-registry.php + - + message: '#^Parameter \#1 \$args of function wp_parse_args expects array\\|object\|string, mixed given\.$#' + identifier: argument.type + count: 2 + path: ../../../src/wp-includes/abilities-api/class-wp-ability.php + - + message: '#^Parameter \#1 \$name of method WordPress\\AiClientDependencies\\Psr\\Http\\Message\\MessageInterface\:\:withHeader\(\) expects string, mixed given\.$#' + identifier: argument.type + count: 1 + path: ../../../src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php + - + message: '#^Parameter \#1 \$wp_response of method WP_AI_Client_HTTP_Client\:\:create_psr_response\(\) expects array\, array given\.$#' + identifier: argument.type + count: 2 + path: ../../../src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php + - + message: '#^Parameter \#2 \$value of method WordPress\\AiClientDependencies\\Psr\\Http\\Message\\MessageInterface\:\:withHeader\(\) expects array\\|string, mixed given\.$#' + identifier: argument.type + count: 1 + path: ../../../src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php + - + message: '#^Parameter \#4 \$options of static method WP_Style_Engine_CSS_Declarations\:\:filter_declaration\(\) expects array\{important\?\: bool, \.\.\.\}, mixed given\.$#' + identifier: argument.type + count: 1 + path: ../../../src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php + - + message: '#^Parameter \#3 \$options of method WP_Style_Engine_CSS_Declarations\:\:add_declaration\(\) expects array\{important\?\: bool, \.\.\.\}, mixed given\.$#' + identifier: argument.type + count: 1 + path: ../../../src/wp-includes/style-engine/class-wp-style-engine-css-rule.php + - + message: '#^Parameter \#1 \$should_prettify of method WP_Style_Engine_CSS_Rule\:\:get_css\(\) expects bool, mixed given\.$#' + identifier: argument.type + count: 1 + path: ../../../src/wp-includes/style-engine/class-wp-style-engine-processor.php diff --git a/tests/phpstan/baselines-strict/cast.int.neon b/tests/phpstan/baselines-strict/cast.int.neon new file mode 100644 index 0000000000000..afa3e42a72b97 --- /dev/null +++ b/tests/phpstan/baselines-strict/cast.int.neon @@ -0,0 +1,25 @@ +# PHPStan baseline for the `cast.int` errors in WordPress core. +# +# https://phpstan.org/error-identifiers/cast.int +# +# Each entry is scoped to a single file and carries an exact occurrence count, +# so that a new instance is reported as a new error rather than being absorbed +# silently. Fixing an occurrence therefore means decrementing or removing its +# entry here as part of the same change. +# +# The goal is to empty this file and delete it. +# +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: +# +# composer phpstan:baselines -- --identifier=cast.int --config=phpstan-strict.neon.dist +# +# This reruns the analysis with the baselines in this directory suppressed, so +# that their errors surface again, and rewrites only this file. + +parameters: + ignoreErrors: + - + message: '#^Cannot cast mixed to int\.$#' + identifier: cast.int + count: 1 + path: ../../../src/wp-includes/class-wp-post.php diff --git a/tests/phpstan/baselines-strict/loader.php b/tests/phpstan/baselines-strict/loader.php new file mode 100644 index 0000000000000..829e4ac4880d4 --- /dev/null +++ b/tests/phpstan/baselines-strict/loader.php @@ -0,0 +1,16 @@ + but returns array\\.$#' + identifier: return.type + count: 1 + path: ../../../src/wp-includes/abilities-api/class-wp-ability-categories-registry.php diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index dd184d5a12924..bd6828d096b11 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=argument.type +# composer phpstan:baselines -- --identifier=argument.type --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/argument.unresolvableType.neon b/tests/phpstan/baselines/argument.unresolvableType.neon index 8bee3995f6ac5..f6ca492a525ce 100644 --- a/tests/phpstan/baselines/argument.unresolvableType.neon +++ b/tests/phpstan/baselines/argument.unresolvableType.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=argument.unresolvableType +# composer phpstan:baselines -- --identifier=argument.unresolvableType --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/assign.propertyType.neon b/tests/phpstan/baselines/assign.propertyType.neon index 4afc274001850..a9a89163f9df1 100644 --- a/tests/phpstan/baselines/assign.propertyType.neon +++ b/tests/phpstan/baselines/assign.propertyType.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=assign.propertyType +# composer phpstan:baselines -- --identifier=assign.propertyType --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/binaryOp.invalid.neon b/tests/phpstan/baselines/binaryOp.invalid.neon index 57b8bf343662e..bfcd43d16d874 100644 --- a/tests/phpstan/baselines/binaryOp.invalid.neon +++ b/tests/phpstan/baselines/binaryOp.invalid.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=binaryOp.invalid +# composer phpstan:baselines -- --identifier=binaryOp.invalid --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon index 23523d7b66739..1f55c01be4124 100644 --- a/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon +++ b/tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=booleanAnd.leftAlwaysTrue +# composer phpstan:baselines -- --identifier=booleanAnd.leftAlwaysTrue --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon index 066ee954ba210..7db8291d66e7c 100644 --- a/tests/phpstan/baselines/booleanNot.alwaysTrue.neon +++ b/tests/phpstan/baselines/booleanNot.alwaysTrue.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=booleanNot.alwaysTrue +# composer phpstan:baselines -- --identifier=booleanNot.alwaysTrue --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/class.notFound.neon b/tests/phpstan/baselines/class.notFound.neon index 423a42dd3aca5..97a6745e87a0d 100644 --- a/tests/phpstan/baselines/class.notFound.neon +++ b/tests/phpstan/baselines/class.notFound.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=class.notFound +# composer phpstan:baselines -- --identifier=class.notFound --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/deadCode.unreachable.neon b/tests/phpstan/baselines/deadCode.unreachable.neon index 3dccb140aacc0..1a688f51c78a1 100644 --- a/tests/phpstan/baselines/deadCode.unreachable.neon +++ b/tests/phpstan/baselines/deadCode.unreachable.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=deadCode.unreachable +# composer phpstan:baselines -- --identifier=deadCode.unreachable --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/empty.property.neon b/tests/phpstan/baselines/empty.property.neon index 869cadbf2fea7..29c45b3128ea4 100644 --- a/tests/phpstan/baselines/empty.property.neon +++ b/tests/phpstan/baselines/empty.property.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=empty.property +# composer phpstan:baselines -- --identifier=empty.property --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/empty.variable.neon b/tests/phpstan/baselines/empty.variable.neon index fe2adb8dec11e..47f6187fd5310 100644 --- a/tests/phpstan/baselines/empty.variable.neon +++ b/tests/phpstan/baselines/empty.variable.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=empty.variable +# composer phpstan:baselines -- --identifier=empty.variable --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/function.alreadyNarrowedType.neon b/tests/phpstan/baselines/function.alreadyNarrowedType.neon index 467991720b5a7..3c1a44fe8f871 100644 --- a/tests/phpstan/baselines/function.alreadyNarrowedType.neon +++ b/tests/phpstan/baselines/function.alreadyNarrowedType.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=function.alreadyNarrowedType +# composer phpstan:baselines -- --identifier=function.alreadyNarrowedType --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/function.resultUnused.neon b/tests/phpstan/baselines/function.resultUnused.neon index 442b207fe196d..045ad4b731459 100644 --- a/tests/phpstan/baselines/function.resultUnused.neon +++ b/tests/phpstan/baselines/function.resultUnused.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=function.resultUnused +# composer phpstan:baselines -- --identifier=function.resultUnused --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/identical.alwaysFalse.neon b/tests/phpstan/baselines/identical.alwaysFalse.neon index 5bd777f130a40..0d305bff88893 100644 --- a/tests/phpstan/baselines/identical.alwaysFalse.neon +++ b/tests/phpstan/baselines/identical.alwaysFalse.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=identical.alwaysFalse +# composer phpstan:baselines -- --identifier=identical.alwaysFalse --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/if.alwaysFalse.neon b/tests/phpstan/baselines/if.alwaysFalse.neon index 30fb317dec4c4..ed8b9f60e0c1c 100644 --- a/tests/phpstan/baselines/if.alwaysFalse.neon +++ b/tests/phpstan/baselines/if.alwaysFalse.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=if.alwaysFalse +# composer phpstan:baselines -- --identifier=if.alwaysFalse --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/if.alwaysTrue.neon b/tests/phpstan/baselines/if.alwaysTrue.neon index 1d3af11e446fb..a86c1b801ab98 100644 --- a/tests/phpstan/baselines/if.alwaysTrue.neon +++ b/tests/phpstan/baselines/if.alwaysTrue.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=if.alwaysTrue +# composer phpstan:baselines -- --identifier=if.alwaysTrue --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/isset.property.neon b/tests/phpstan/baselines/isset.property.neon index 327cd1c76aa43..4cf68462ad846 100644 --- a/tests/phpstan/baselines/isset.property.neon +++ b/tests/phpstan/baselines/isset.property.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=isset.property +# composer phpstan:baselines -- --identifier=isset.property --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/method.childParameterType.neon b/tests/phpstan/baselines/method.childParameterType.neon index 4c68bab982fab..5faf12a73aa86 100644 --- a/tests/phpstan/baselines/method.childParameterType.neon +++ b/tests/phpstan/baselines/method.childParameterType.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=method.childParameterType +# composer phpstan:baselines -- --identifier=method.childParameterType --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/method.unused.neon b/tests/phpstan/baselines/method.unused.neon index 054dc94542241..ced0e2372a23f 100644 --- a/tests/phpstan/baselines/method.unused.neon +++ b/tests/phpstan/baselines/method.unused.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=method.unused +# composer phpstan:baselines -- --identifier=method.unused --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/notIdentical.alwaysTrue.neon b/tests/phpstan/baselines/notIdentical.alwaysTrue.neon index 1a6284b361ec2..642d685217d02 100644 --- a/tests/phpstan/baselines/notIdentical.alwaysTrue.neon +++ b/tests/phpstan/baselines/notIdentical.alwaysTrue.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=notIdentical.alwaysTrue +# composer phpstan:baselines -- --identifier=notIdentical.alwaysTrue --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon b/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon index 6ea0acc46ac2b..ed021ec794760 100644 --- a/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon +++ b/tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=offsetAccess.nonOffsetAccessible +# composer phpstan:baselines -- --identifier=offsetAccess.nonOffsetAccessible --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/offsetAccess.notFound.neon b/tests/phpstan/baselines/offsetAccess.notFound.neon index ed127f9ed48cd..9f61db04aba25 100644 --- a/tests/phpstan/baselines/offsetAccess.notFound.neon +++ b/tests/phpstan/baselines/offsetAccess.notFound.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=offsetAccess.notFound +# composer phpstan:baselines -- --identifier=offsetAccess.notFound --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/offsetAssign.valueType.neon b/tests/phpstan/baselines/offsetAssign.valueType.neon index 6764804f6d4f1..8077ca9892ff4 100644 --- a/tests/phpstan/baselines/offsetAssign.valueType.neon +++ b/tests/phpstan/baselines/offsetAssign.valueType.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=offsetAssign.valueType +# composer phpstan:baselines -- --identifier=offsetAssign.valueType --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/parameterByRef.type.neon b/tests/phpstan/baselines/parameterByRef.type.neon index 40c8748b45974..0a99ae46d6d27 100644 --- a/tests/phpstan/baselines/parameterByRef.type.neon +++ b/tests/phpstan/baselines/parameterByRef.type.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=parameterByRef.type +# composer phpstan:baselines -- --identifier=parameterByRef.type --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/parameterByRef.unusedType.neon b/tests/phpstan/baselines/parameterByRef.unusedType.neon index 0704b43af2e54..4738b39636023 100644 --- a/tests/phpstan/baselines/parameterByRef.unusedType.neon +++ b/tests/phpstan/baselines/parameterByRef.unusedType.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=parameterByRef.unusedType +# composer phpstan:baselines -- --identifier=parameterByRef.unusedType --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/property.defaultValue.neon b/tests/phpstan/baselines/property.defaultValue.neon index 6cda21d8d6b20..5c7ebe5274920 100644 --- a/tests/phpstan/baselines/property.defaultValue.neon +++ b/tests/phpstan/baselines/property.defaultValue.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=property.defaultValue +# composer phpstan:baselines -- --identifier=property.defaultValue --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/property.nonObject.neon b/tests/phpstan/baselines/property.nonObject.neon index e9f1ae8ec3d98..2acecd3a68838 100644 --- a/tests/phpstan/baselines/property.nonObject.neon +++ b/tests/phpstan/baselines/property.nonObject.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=property.nonObject +# composer phpstan:baselines -- --identifier=property.nonObject --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/property.notFound.neon b/tests/phpstan/baselines/property.notFound.neon index a3f3247ffce3e..6686c06ba9431 100644 --- a/tests/phpstan/baselines/property.notFound.neon +++ b/tests/phpstan/baselines/property.notFound.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=property.notFound +# composer phpstan:baselines -- --identifier=property.notFound --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/property.private.neon b/tests/phpstan/baselines/property.private.neon index 20fdc0268cef3..6d08652de08d5 100644 --- a/tests/phpstan/baselines/property.private.neon +++ b/tests/phpstan/baselines/property.private.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=property.private +# composer phpstan:baselines -- --identifier=property.private --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/return.type.neon b/tests/phpstan/baselines/return.type.neon index f0f524e3f0dbd..9143cbd2bbad7 100644 --- a/tests/phpstan/baselines/return.type.neon +++ b/tests/phpstan/baselines/return.type.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=return.type +# composer phpstan:baselines -- --identifier=return.type --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/varTag.noVariable.neon b/tests/phpstan/baselines/varTag.noVariable.neon index 2593db2cafcd1..5078f4df8beb9 100644 --- a/tests/phpstan/baselines/varTag.noVariable.neon +++ b/tests/phpstan/baselines/varTag.noVariable.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=varTag.noVariable +# composer phpstan:baselines -- --identifier=varTag.noVariable --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/baselines/variable.undefined.neon b/tests/phpstan/baselines/variable.undefined.neon index b1eec349cd4d0..a46e0e5fa2283 100644 --- a/tests/phpstan/baselines/variable.undefined.neon +++ b/tests/phpstan/baselines/variable.undefined.neon @@ -9,11 +9,11 @@ # # The goal is to empty this file and delete it. # -# Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with +# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # -# composer phpstan:baselines -- --identifier=variable.undefined +# composer phpstan:baselines -- --identifier=variable.undefined --config=phpstan.neon.dist # -# which reruns the analysis with the baselines in this directory suppressed, so +# This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. parameters: diff --git a/tests/phpstan/generate-baselines.php b/tests/phpstan/generate-baselines.php index 5a4ee33d9d5f9..3611c958627cd 100644 --- a/tests/phpstan/generate-baselines.php +++ b/tests/phpstan/generate-baselines.php @@ -17,6 +17,11 @@ * includes every baseline in that directory, so adding a newly split out baseline, * and retiring one that has reached zero, need no edit of the configuration. * + * Unless a configuration is named, this is done for each of DEFAULT_CONFIGS in + * turn: phpstan.neon.dist, which analyzes the whole codebase, and then + * phpstan-strict.neon.dist, which holds selected paths to a stricter rule level + * and includes the former along with its baselines. + * * With `--prune`, nothing is analyzed: the baselines that no longer list any * entries are deleted. That completes running PHPStan with `--fix`, which removes * the entries of fixed errors in place but leaves a baseline it has emptied @@ -57,6 +62,21 @@ */ const DEFAULT_CONFIG = 'phpstan.neon.dist'; +/** + * The configurations processed when neither `--config` nor `--output-dir` is given, + * each with the directory its baselines are written to. + * + * They are processed in this order, which matters: phpstan-strict.neon.dist + * includes phpstan.neon.dist along with its baselines, so its own are only what is + * left over once those are up to date. + * + * Relative to the repository root. + */ +const DEFAULT_CONFIGS = array( + DEFAULT_CONFIG => DEFAULT_OUTPUT_DIR, + 'phpstan-strict.neon.dist' => 'tests/phpstan/baselines-strict', +); + if ( 'cli' !== PHP_SAPI ) { fwrite( STDERR, "This script must be run from the command line.\n" ); exit( 1 ); @@ -74,7 +94,8 @@ array_shift( $args ); $config_option = DEFAULT_CONFIG; -$output_option = DEFAULT_OUTPUT_DIR; +$output_option = null; +$config_given = false; $memory_limit = '2G'; $only_identifiers = array(); $combined = false; @@ -121,11 +142,13 @@ if ( 1 === preg_match( '/^--config=(.+)$/', $arg, $matches ) ) { $config_option = $matches[1]; + $config_given = true; continue; } if ( 1 === preg_match( '/^--output-dir=(.+)$/', $arg, $matches ) ) { $output_option = $matches[1]; + $config_given = true; continue; } @@ -138,6 +161,52 @@ exit( 1 ); } +/* + * Without a configuration named, every default configuration is processed in turn, + * each by a run of this script naming it, so that each run is the same as one asked + * for directly. The first failure stops the rest, since each configuration after it + * depends on the baselines of those before it. + * + * `--combined` prints a single baseline, so it keeps to the first configuration. + */ +if ( ! $config_given && ! $combined ) { + foreach ( DEFAULT_CONFIGS as $config => $output ) { + $command = implode( + ' ', + array_map( + 'escapeshellarg', + array_merge( array( PHP_BINARY, __FILE__ ), $args, array( "--config=$config", "--output-dir=$output" ) ) + ) + ); + + fwrite( STDERR, "\n== $config ==\n\n" ); + + $exit_code = 0; + passthru( $command, $exit_code ); + + if ( 0 !== $exit_code ) { + exit( $exit_code ); + } + } + exit( 0 ); +} + +/* + * A default configuration named without an output directory writes to its own, + * so that naming phpstan-strict.neon.dist alone cannot write its baselines over + * those of phpstan.neon.dist. So does a local override of one, such as + * phpstan-strict.neon, named as the `.dist` file is but without the suffix. + * Written to the default directory instead, the stricter analysis of only the + * strict paths, with the baselines there suppressed, would retire every baseline of + * the whole codebase it does not report again. + */ +$config_relative = trim( normalize_path( $config_option ), '/' ); +$config_output_dir = DEFAULT_CONFIGS[ $config_relative ] ?? DEFAULT_CONFIGS[ $config_relative . '.dist' ] ?? DEFAULT_OUTPUT_DIR; + +if ( null === $output_option ) { + $output_option = $config_output_dir; +} + $config_path = $repo_root . '/' . ltrim( $config_option, '/' ); $output_dir = $repo_root . '/' . trim( $output_option, '/' ); @@ -258,8 +327,6 @@ static function () use ( $temp_config, $temp_baseline ): void { * readings of everything it does not cover. Keying the directory on the * configuration keeps each one's reflection to itself. */ -$config_relative = trim( normalize_path( $config_option ), '/' ); - if ( DEFAULT_CONFIG !== $config_relative ) { $analysis_tmp_dir .= '-' . (string) preg_replace( '/[^A-Za-z0-9._]+/', '-', $config_relative ); } @@ -301,14 +368,55 @@ static function () use ( $temp_config, $temp_baseline ): void { escapeshellarg( $memory_limit ) ); +/* + * The baselines only take effect through a loader in their own directory. It is + * written on every run, so that the committed loader of the default directory, and + * that of any other, stay what build_loader() makes rather than drifting from it + * unnoticed. A directory given with `--output-dir` that had none is not included + * by anything yet, so say so. + * + * It is written before the analysis rather than after, since a configuration may + * already include it, and PHPStan refuses to run with an include that is missing. + * That is how the baselines of a new configuration are first generated: add the + * loader of its directory to its `includes`, then run this with `--config` and + * `--output-dir` naming them. With the directory suppressed, the loader includes + * nothing during the analysis either way. + * + * `--combined` writes nothing to disk, so a missing loader stays missing then. + */ +if ( ! $combined ) { + if ( ! is_dir( $output_dir ) && ! mkdir( $output_dir, 0755, true ) ) { + fwrite( STDERR, "Could not create $output_option\n" ); + exit( 1 ); + } + + $loader_path = $output_dir . '/loader.php'; + $loader = build_loader( $output_dir ); + $loader_existed = is_file( $loader_path ); + $loader_contents = $loader_existed ? file_get_contents( $loader_path ) : false; + + if ( $loader !== $loader_contents ) { + if ( false === file_put_contents( $loader_path, $loader ) ) { + fwrite( STDERR, "Could not write $output_option/loader.php\n" ); + exit( 1 ); + } + + printf( + $loader_existed ? "%s: rewritten.\n" : "%s: written. The configuration has to include it for these baselines to take effect.\n", + $output_option . '/loader.php' + ); + } +} + fwrite( STDERR, "Analyzing with $config_option, baselines in $output_option suppressed...\n" ); /* * PHPStan inherits it, as do the worker processes it starts. It is the real path, - * since that is what the loader's __DIR__ is. An output directory not created yet - * holds no loader to match, so it is not set at all then. It is cleared first - * either way, so that a value exported in the shell, perhaps naming another - * directory, cannot switch off baselines this run means to keep. + * since that is what the loader's __DIR__ is. An output directory that does not + * exist, which only `--combined` leaves uncreated, holds no loader to match, so it + * is not set at all then. It is cleared first either way, so that a value + * exported in the shell, perhaps naming another directory, cannot switch off + * baselines this run means to keep. */ putenv( GENERATING_BASELINES_ENV ); $resolved_output_dir = realpath( $output_dir ); @@ -363,46 +471,17 @@ static function () use ( $temp_config, $temp_baseline ): void { exit( 0 ); } -if ( ! is_dir( $output_dir ) && ! mkdir( $output_dir, 0755, true ) ) { - fwrite( STDERR, "Could not create $output_option\n" ); - exit( 1 ); -} - -/* - * The baselines only take effect through a loader in their own directory. It is - * written on every run, so that the committed loader of the default directory, and - * that of any other, stay what build_loader() makes rather than drifting from it - * unnoticed. A directory given with `--output-dir` that had none is not included - * by anything yet, so say so. - */ -$loader_path = $output_dir . '/loader.php'; -$loader = build_loader( $output_dir ); -$loader_existed = is_file( $loader_path ); -$loader_contents = $loader_existed ? file_get_contents( $loader_path ) : false; - -if ( $loader !== $loader_contents ) { - if ( false === file_put_contents( $loader_path, $loader ) ) { - fwrite( STDERR, "Could not write $output_option/loader.php\n" ); - exit( 1 ); - } - - printf( - $loader_existed ? "%s: rewritten.\n" : "%s: written. Include it in the configuration for these baselines to take effect.\n", - $output_option . '/loader.php' - ); -} - /* * The command each header gives for regenerating its baseline has to name the - * configuration and the directory this run used, where they are not the - * defaults. Without them, it would regenerate the default directory instead. + * configuration this run used, even the default one, since without a + * configuration named, every one in DEFAULT_CONFIGS is processed, and the + * baselines of the same identifier in each of their directories are rewritten. + * The directory is only named where it is not the configuration's own, since + * naming the configuration alone writes there. */ -$regenerate_options = ''; -if ( DEFAULT_CONFIG !== $config_relative ) { - $regenerate_options .= ' --config=' . $config_option; -} -if ( ! $is_default_output_dir ) { - $regenerate_options .= ' --output-dir=' . $output_option; +$regenerate_options = ' --config=' . quote_shell_value( $config_option ); +if ( $config_output_dir !== trim( normalize_path( $output_option ), '/' ) ) { + $regenerate_options .= ' --output-dir=' . quote_shell_value( $output_option ); } foreach ( $grouped as $identifier => $entries ) { @@ -468,6 +547,10 @@ function get_usage(): string { tests/phpstan/baselines/loader.php, so neither addition nor removal needs an edit of it. + Without --config or --output-dir, this is done for phpstan.neon.dist into + tests/phpstan/baselines, and then for phpstan-strict.neon.dist into + tests/phpstan/baselines-strict. The other options apply to both. + Usage: composer phpstan:baselines [-- ] @@ -478,16 +561,21 @@ function get_usage(): string { rather than left behind empty. Default: every identifier reported. --config= Configuration to analyze with, relative to the repository - root. Default: phpstan.neon.dist + root. Naming one processes only that configuration. + Default: both of the above. --output-dir= Where the per-identifier baselines are written, relative to the repository root. Paths inside them are written relative to this directory. A loader is written there too, which the configuration has to include. Baselines in other directories stay in force, so only the errors they leave are written. - Default: tests/phpstan/baselines + Default: the directory of the configuration named + above, or of the one it overrides locally, such as + phpstan-strict.neon, or else tests/phpstan/baselines --combined Print one combined baseline to stdout instead of writing - per-identifier files. Nothing is written to disk. + per-identifier files. Nothing is written to disk. Only + phpstan.neon.dist is analyzed, unless --config names + another configuration. --prune Without analyzing anything, delete the baselines that no longer list any entries, such as one emptied by running PHPStan with --fix. With --identifier, only those @@ -800,6 +888,22 @@ function quote_neon_value( string $value ): string { return "'" . str_replace( "'", "''", $value ) . "'"; } +/** + * Quotes a value for a command shown to be run in a shell, where it needs it. + * + * The command is only written into a baseline's header, to be copied from there, + * so a value is left as it is when the shell would read it that way, as the paths + * of the default configurations and their directories are. Any other value, such + * as a path with a space in it, is quoted, so the copied command still passes it + * as one argument. + * + * @param string $value Value to quote. + * @return string Value, quoted if the shell would otherwise split or expand it. + */ +function quote_shell_value( string $value ): string { + return 1 === preg_match( '#^[\w./-]+$#', $value ) ? $value : escapeshellarg( $value ); +} + /** * Builds the header comment for a per-identifier baseline. * @@ -822,11 +926,11 @@ function build_baseline_header( string $identifier, string $options = '' ): stri # # The goal is to empty this file and delete it. # - # Generated by `composer phpstan:baselines`. Do not edit by hand; regenerate with + # Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: # # composer phpstan:baselines -- --identifier=$identifier$options # - # which reruns the analysis with the baselines in this directory suppressed, so + # This reruns the analysis with the baselines in this directory suppressed, so # that their errors surface again, and rewrites only this file. TEXT;