From 2e08c9f92da4d7af9c3d738698cc722a97369d04 Mon Sep 17 00:00:00 2001 From: Robert Meijers Date: Wed, 13 Dec 2023 15:53:14 +0100 Subject: [PATCH] Fix mixing property and param attributes on promoted property Simplify the ParamAttributesRule by merging AttributeCheck::check calls and using an int mask to check for both TARGET_PARAMETER and TARGET_PROPERTY in one pass. Fixes #10298 --- src/Rules/AttributesCheck.php | 2 +- src/Rules/Functions/ParamAttributesRule.php | 16 +++------------- .../Functions/ParamAttributesRuleTest.php | 5 +++++ .../PHPStan/Rules/Functions/data/bug-10298.php | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-10298.php diff --git a/src/Rules/AttributesCheck.php b/src/Rules/AttributesCheck.php index e203dc4bcf..441eab62ee 100644 --- a/src/Rules/AttributesCheck.php +++ b/src/Rules/AttributesCheck.php @@ -28,7 +28,7 @@ public function __construct( /** * @param AttributeGroup[] $attrGroups - * @param Attribute::TARGET_* $requiredTarget + * @param int-mask-of $requiredTarget * @return RuleError[] */ public function check( diff --git a/src/Rules/Functions/ParamAttributesRule.php b/src/Rules/Functions/ParamAttributesRule.php index aeadb1cfb6..ee9e8f257c 100644 --- a/src/Rules/Functions/ParamAttributesRule.php +++ b/src/Rules/Functions/ParamAttributesRule.php @@ -7,7 +7,6 @@ use PHPStan\Analyser\Scope; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\Rule; -use function count; /** * @implements Rule @@ -27,25 +26,16 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { $targetName = 'parameter'; + $targetType = Attribute::TARGET_PARAMETER; if ($node->flags !== 0) { $targetName = 'parameter or property'; - - $propertyTargetErrors = $this->attributesCheck->check( - $scope, - $node->attrGroups, - Attribute::TARGET_PROPERTY, - $targetName, - ); - - if (count($propertyTargetErrors) === 0) { - return $propertyTargetErrors; - } + $targetType |= Attribute::TARGET_PROPERTY; } return $this->attributesCheck->check( $scope, $node->attrGroups, - Attribute::TARGET_PARAMETER, + $targetType, $targetName, ); } diff --git a/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php index ee517d6e74..73fa588489 100644 --- a/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php @@ -66,4 +66,9 @@ public function testSensitiveParameterAttribute(): void $this->analyse([__DIR__ . '/data/sensitive-parameter.php'], []); } + public function testBug10298(): void + { + $this->analyse([__DIR__ . '/data/bug-10298.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-10298.php b/tests/PHPStan/Rules/Functions/data/bug-10298.php new file mode 100644 index 0000000000..dfbfa7979e --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-10298.php @@ -0,0 +1,18 @@ +