konradmichalik / phpstan-exception-codes
PHPStan rules for unique, plausible exception codes, as TYPO3 expects them
Package info
github.com/konradmichalik/phpstan-exception-codes
Type:phpstan-extension
pkg:composer/konradmichalik/phpstan-exception-codes
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0
- phpstan/phpstan: ^2.1
Requires (Dev)
- armin/editorconfig-cli: ^2.0
- ergebnis/composer-normalize: ^2.44
- phpunit/phpunit: ^11.5 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PHPStan Exception Codes
This package provides PHPStan rules for exception codes, as TYPO3 expects them: every throw carries a unique, plausible code.
Important
This package is intended for use in my personal projects only. It is not designed for general use.
🔥 Installation
composer require konradmichalik/phpstan-exception-codes --dev
⚡ Usage
If you have the phpstan/extension-installer
package installed, there's nothing more to do. The rules are automatically included.
Manual Setup
# phpstan.neon includes: - %rootDir%/../../konradmichalik/phpstan-exception-codes/rules.neon
🧰 Included Rules
| Identifier | Reported when |
|---|---|
exceptionCode.missing |
throw new X(...) passes no code, or 0 |
exceptionCode.outOfRange |
The code is outside the configured range |
exceptionCode.duplicate |
The same code is thrown in more than one place, reported at every place |
Only throw new X(...) is checked, and only when the second constructor parameter of X is an int $code. Exceptions such as new HttpProblemException(404, ...) are skipped. The parameter has to be named code and typed int, so ?int $code is skipped. If the code is not passed, the default of the parameter counts. Rethrows (throw $e), spread arguments (new X(...$arguments)) and codes that are not constant ($previous->getCode()) are skipped as well.
Duplicates are collected across all analysed files, so they work with parallel analysis and the result cache. A code can be reported as out of range and as a duplicate at the same time. A constant or enum value shared by several throws counts as a duplicate as well, because the code no longer tells which place threw. Use an inline @phpstan-ignore exceptionCode.duplicate (reason) if sharing is intended.
Range
The range is fixed, not derived from the current time, so a run gives the same result every day. The defaults match TYPO3 timestamp codes:
parameters: exceptionCodes: minimum: 1000000000 maximum: 2147483647
🛠️ Typical Overrides
Existing codes that cannot be changed yet belong in the baseline. Ignore a single throw per identifier and path, and keep the reason next to it:
parameters: ignoreErrors: # Code is part of the public API and cannot change before the next major release - identifier: exceptionCode.duplicate path: Classes/Legacy/LegacyService.php
Widen the range if a project uses random ten-digit codes instead of timestamps:
parameters: exceptionCodes: maximum: 9999999999
🧑💻 Development
composer test runs the PHPUnit rule tests in tests/Rule. The analysed fixtures live in tests/Rule/data.
⭐ License
This project is licensed under GNU General Public License 3.0 (or later).