Changeset 63019 for trunk/tests/phpstan/README.md
- Timestamp:
- 08/05/2026 07:16:57 AM (2 months ago)
- File:
-
- 1 edited
-
trunk/tests/phpstan/README.md (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpstan/README.md
r62939 r63019 39 39 composer run phpstan -- -vvv --debug 40 40 ``` 41 42 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. 41 43 42 44 For available flags, see https://phpstan.org/user-guide/command-line-usage. … … 92 94 - Adding the error pattern to the `ignoreErrors` section of the `phpstan.neon.dist` configuration file. This should be used to handle conflicts with WordPress Coding Standards or similar project decisions, or to allowlist legacy code that is not worth refactoring solely to satisfy the tests. 93 95 94 - Adding an error to the"tech debt" baseline. This should be used for code that needs to be addressed eventually - by fixing, refactoring, or ignoring via one of the above methods - but is not worth addressing right now.96 - Adding an error to a "tech debt" baseline. This should be used for code that needs to be addressed eventually - by fixing, refactoring, or ignoring via one of the above methods - but is not worth addressing right now. 95 97 96 98 Baselines are a useful triage tool for handling PHPStan errors in legacy code, as they allow us to enforce stricter code quality checks on new code, while gradually chipping away at the existing issues over time. **Avoid adding PHPStan errors from new code whenever possible, and use baselines as a last resort.** 97 99 98 The baseline file is located at `tests/phpstan/baseline.php` and generated by running PHPStan with the `--generate-baseline` flag: 100 ### How the baselines are organized 99 101 100 ```bash 101 npm run typecheck:php -- --generate-baseline=tests/phpstan/baseline.php 102 The baselines live in [`baselines/`](baselines), one file per error identifier, such as `variable.undefined.neon`. Splitting them this way keeps each kind of error visible as a single file that should shrink to nothing and then be deleted, rather than as part of one large file in which every kind is mixed together. 102 103 103 # or, with Composer directly: 104 composer run phpstan -- --generate-baseline=tests/phpstan/baseline.php 105 ``` 104 Every entry is scoped to the file the error occurs in and carries an exact occurrence count: 106 105 107 This will regenerate the baseline file with any new errors added to the existing ones. You can then commit the updated baseline file. 106 ```neon 107 - 108 message: '#^Variable \$wpdb might not be defined\.$#' 109 identifier: variable.undefined 110 count: 4 111 path: ../../../src/wp-trackback.php 112 ``` 113 114 Both the path and the count matter. A new occurrence of an already baselined error does not match the entry, even in a file that is already listed, and is reported as a new error. That is the point of recording them this way: the baselines describe exactly what exists today, so nothing new slips in behind them. 115 116 The consequence is that **fixing a baselined error means regenerating its baseline as part of the same change**, because the count no longer matches. A count that no longer matches is reported as an `ignore.count` error, which PHPStan does not allow to be ignored or baselined. 117 118 ### Regenerating the baselines 119 120 The baselines are generated, and should not be edited by hand. Regenerate them with: 121 122 ```bash 123 npm run typecheck:php:baselines 124 ``` 125 126 which will run the generator in the Docker container. 127 128 As with the analysis itself, flags are passed by adding `--` followed by the flags themselves: 129 130 ```bash 131 # a single identifier: 132 npm run typecheck:php:baselines -- --identifier=variable.undefined 133 134 # several, either comma separated or by repeating the option: 135 npm run typecheck:php:baselines -- --identifier=variable.undefined,isset.variable 136 npm run typecheck:php:baselines -- --identifier=isset.variable --identifier=empty.variable 137 138 # print every error as one baseline, writing nothing: 139 npm run typecheck:php:baselines -- --combined 140 141 # the remaining options: 142 npm run typecheck:php:baselines -- --help 143 ``` 144 145 If you are not using the Docker environment, you can run the generator via Composer directly: 146 147 ```bash 148 composer phpstan:baselines 149 150 composer phpstan:baselines -- --identifier=variable.undefined 151 composer phpstan:baselines -- --combined 152 composer phpstan:baselines -- --help 153 ``` 154 155 Note the `--` in each of those. Composer needs it in order to pass the flags on to the script rather than reading them as its own, and without it they are discarded silently, so `composer phpstan:baselines --identifier=variable.undefined` regenerates every baseline rather than that one. The npm script supplies it, which is why only one is needed there. 156 157 A run also deletes any baseline whose identifier no longer reports anything, and rewrites the list of them between the `# phpstan:baselines` markers in the `includes` of [`phpstan.neon.dist`](../../phpstan.neon.dist) to match. Adding a newly split out baseline, and retiring one that has reached zero, therefore need no edit of the configuration. 158 159 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. 108 160 109 161 ## Performance and troubleshooting
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)