Fix phpstan/phpstan#14252: Enum case outside of enum should be reported#5188
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14252: Enum case outside of enum should be reported#5188phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added new EnumCaseOutsideEnumRule that reports an error when `case` is used in classes or interfaces instead of enums - New regression test in tests/PHPStan/Rules/EnumCases/data/bug-14252.php - The rule is registered at level 0 and uses the identifier `enum.caseOutsideOfEnum` Closes phpstan/phpstan#14252
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PHPStan did not report an error when an enum
casedeclaration appeared inside a regular class or interface (instead of an enum). PHP itself produces a fatal error "Case can only be used in enums" for such code, but PHPStan silently accepted it.Changes
src/Rules/EnumCases/EnumCaseOutsideEnumRule.php— a new rule that checks ifNode\Stmt\EnumCaseappears inside a non-enum class and reports "Enum case can only be used in enums."tests/PHPStan/Rules/EnumCases/EnumCaseOutsideEnumRuleTest.php— test case for the new ruletests/PHPStan/Rules/EnumCases/data/bug-14252.php— test data with enum case in class, interface, and enum (enum case in enum should not error)Root cause
There was no rule checking whether
EnumCaseAST nodes appear in the correct context. The parser (nikic/php-parser) parsescase Active;asNode\Stmt\EnumCaseregardless of whether it's inside an enum or a class/interface. PHPStan had rules for validating enum case attributes and sanity checks within enums, but nothing to catch an enum case declaration outside of an enum.Test
The regression test verifies that:
case Active;inside a class reports an error on line 9case Active;inside an interface reports an error on line 14case Active;inside an enum does NOT report an error (line 19)Fixes phpstan/phpstan#14252