marc-mabe/php-enum-phpstan
PHPStan extension for marc-mabe/php-enum. Teaches PHPStan about enum accessor methods and allowed values so static analysis understands your enumerations and catches invalid usage. Install with Composer and include the provided extension.neon.
composer require --dev marc-mabe/php-enum-phpstan
phpstan.neon or phpstan.dist.neon):
includes:
- vendor/marc-mabe/php-enum-phpstan/extension.neon
php-enum enum (e.g., StringEnum or IntEnum) and writing code like MyEnum::STATUS_ACTIVE->getValue(). PHPStan will now correctly infer the return type (string/int) and detect invalid accesses (e.g., MyEnum::NON_EXISTENT) before running tests or production.MyEnum::from($input) with full confidence—PHPStan knows it returns MyEnum|null (or MyEnum if strict-null checks are off), and errors on invalid scalar types.MyEnum::ACTIVE, MyEnum::getValues(), MyEnum::getValuesAsArray(), etc., enabling reliable refactor-safe method calls.MyUnionEnum extends IntEnum|StringEnum, PHPStan infers correct union return types from getValues() → array<int|string>—critical for payload parsing in APIs.@deprecated (e.g., in MyEnum::OLD_STATUS), and PHPStan will emit warnings when used, aiding deprecation workflows.vendor/bin/phpstan analyse --memory-limit=2G to your pipeline to catch enum misuse early—especially valuable when evolving APIs or consolidating enum usage.marc-mabe/php-enum (≥4.0) and phpstan/phpstan (≥2.0) are installed. Mismatched versions silently disable inference. Run composer why marc-mabe/php-enum and composer show phpstan/phpstan to verify.$enumClass::getValues()), deeply dynamic constructs like call_user_func([$className, 'getValues']) won’t be resolved—fall back to @phpstan-var or @phpstan-ignore.phpstan/phpstan-deprecation-rules or consider adding stubs via stubFiles.php-enum methods are handled. For custom logic (e.g., MyEnum::isDefault()), either:@return in docblocks, orphp-enum’s reflection API.tests/ and database/ folders (you rarely need enum checks there) via ignoreErrors or skipAnalyze in config.How can I help you explore Laravel packages today?