-
Notifications
You must be signed in to change notification settings - Fork 555
Fix phpstan/phpstan#14250: trait.duplicateMethod is not reported #5181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ondrejmirtes
merged 8 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-y7tlrzi
Mar 20, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f8b143
Fix phpstan/phpstan#14250: Report trait.duplicateMethod for duplicate…
phpstan-bot 084e3c8
Extract DuplicateDeclarationHelper to share logic between DuplicateDe…
phpstan-bot f08ea1b
Add comprehensive tests for duplicate declarations in traits (constan…
phpstan-bot be430eb
Fix lint
VincentLanglet 87758ef
Add test case for multiple conflicting traits (no false positive)
phpstan-bot f59c8fe
Fix lint: use import statement for IdentifierRuleError
phpstan-bot c717c8f
Convert DuplicateDeclarationHelper to AutowiredService instance
phpstan-bot 8804832
Add @param string literal type for $identifierType in DuplicateDeclar…
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Classes; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\ClassConst; | ||
| use PhpParser\Node\Stmt\ClassLike; | ||
| use PhpParser\Node\Stmt\EnumCase; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Rules\IdentifierRuleError; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\ShouldNotHappenException; | ||
| use function array_key_exists; | ||
| use function is_string; | ||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| #[AutowiredService] | ||
| final class DuplicateDeclarationHelper | ||
| { | ||
|
|
||
| /** | ||
| * @param 'class'|'interface'|'trait'|'enum' $identifierType | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| public function checkClassLike(ClassLike $classLike, string $displayName, string $identifierType): array | ||
| { | ||
| $errors = []; | ||
|
|
||
| $declaredClassConstantsOrEnumCases = []; | ||
| foreach ($classLike->stmts as $stmtNode) { | ||
| if ($stmtNode instanceof EnumCase) { | ||
| if (array_key_exists($stmtNode->name->name, $declaredClassConstantsOrEnumCases)) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Cannot redeclare enum case %s::%s.', | ||
| $displayName, | ||
| $stmtNode->name->name, | ||
| ))->identifier(sprintf('%s.duplicateEnumCase', $identifierType)) | ||
| ->line($stmtNode->getStartLine()) | ||
| ->nonIgnorable() | ||
| ->build(); | ||
| } else { | ||
| $declaredClassConstantsOrEnumCases[$stmtNode->name->name] = true; | ||
| } | ||
| } elseif ($stmtNode instanceof ClassConst) { | ||
| foreach ($stmtNode->consts as $classConstNode) { | ||
| if (array_key_exists($classConstNode->name->name, $declaredClassConstantsOrEnumCases)) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Cannot redeclare constant %s::%s.', | ||
| $displayName, | ||
| $classConstNode->name->name, | ||
| ))->identifier(sprintf('%s.duplicateConstant', $identifierType)) | ||
| ->line($classConstNode->getStartLine()) | ||
| ->nonIgnorable() | ||
| ->build(); | ||
| } else { | ||
| $declaredClassConstantsOrEnumCases[$classConstNode->name->name] = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $declaredProperties = []; | ||
| foreach ($classLike->getProperties() as $propertyDecl) { | ||
| foreach ($propertyDecl->props as $property) { | ||
| if (array_key_exists($property->name->name, $declaredProperties)) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Cannot redeclare property %s::$%s.', | ||
| $displayName, | ||
| $property->name->name, | ||
| ))->identifier(sprintf('%s.duplicateProperty', $identifierType)) | ||
| ->line($property->getStartLine()) | ||
| ->nonIgnorable() | ||
| ->build(); | ||
| } else { | ||
| $declaredProperties[$property->name->name] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $declaredFunctions = []; | ||
| foreach ($classLike->getMethods() as $method) { | ||
| if ($method->name->toLowerString() === '__construct') { | ||
| foreach ($method->params as $param) { | ||
| if ($param->flags === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!$param->var instanceof Node\Expr\Variable || !is_string($param->var->name)) { | ||
| throw new ShouldNotHappenException(); | ||
| } | ||
|
|
||
| $propertyName = $param->var->name; | ||
|
|
||
| if (array_key_exists($propertyName, $declaredProperties)) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Cannot redeclare property %s::$%s.', | ||
| $displayName, | ||
| $propertyName, | ||
| ))->identifier(sprintf('%s.duplicateProperty', $identifierType)) | ||
| ->line($param->getStartLine()) | ||
| ->nonIgnorable() | ||
| ->build(); | ||
| } else { | ||
| $declaredProperties[$propertyName] = true; | ||
| } | ||
| } | ||
| } | ||
| if (array_key_exists(strtolower($method->name->name), $declaredFunctions)) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Cannot redeclare method %s::%s().', | ||
| $displayName, | ||
| $method->name->name, | ||
| ))->identifier(sprintf('%s.duplicateMethod', $identifierType)) | ||
| ->line($method->getStartLine()) | ||
| ->nonIgnorable() | ||
| ->build(); | ||
| } else { | ||
| $declaredFunctions[strtolower($method->name->name)] = true; | ||
| } | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Classes; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\RegisteredRule; | ||
| use PHPStan\Node\InTraitNode; | ||
| use PHPStan\Rules\Rule; | ||
|
|
||
| /** | ||
| * @implements Rule<InTraitNode> | ||
| */ | ||
| #[RegisteredRule(level: 0)] | ||
| final class DuplicateTraitDeclarationRule implements Rule | ||
| { | ||
|
|
||
| public function __construct(private DuplicateDeclarationHelper $helper) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return InTraitNode::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| return $this->helper->checkClassLike( | ||
| $node->getOriginalNode(), | ||
| $node->getTraitReflection()->getDisplayName(), | ||
| 'trait', | ||
| ); | ||
| } | ||
|
|
||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method needs
@paramwith string literals as possible identifier parts so that later we're able to generate the identifiers documentation.Something like this:
@param 'class'|'trait' $identifierThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Here's what I changed:
src/Rules/Classes/DuplicateDeclarationHelper.php: Added@param 'class'|'interface'|'trait'|'enum' $identifierTypeto thecheckClassLike()method so identifier documentation can be generated from the string literals.src/PhpDoc/StubValidator.php: Fixed theDuplicateDeclarationRuleinstantiation to pass theDuplicateDeclarationHelperdependency (was missing after the refactor to use constructor injection).Both PHPStan and tests pass.