Search by

konradmichalik / phpstan-exception-codes

konradmichalik

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

Statistics

Installs: 182

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.0 2026-11-24 19:56 UTC

This package is auto-updated.

Last update: 2026-11-24 19:59:10 UTC


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

Packagist Packagist Downloads

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).