Skip to content

Commit 2e08c9f

Browse files
RobertMeondrejmirtes
authored andcommitted
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
1 parent a81df66 commit 2e08c9f

4 files changed

Lines changed: 27 additions & 14 deletions

File tree

src/Rules/AttributesCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828

2929
/**
3030
* @param AttributeGroup[] $attrGroups
31-
* @param Attribute::TARGET_* $requiredTarget
31+
* @param int-mask-of<Attribute::TARGET_*> $requiredTarget
3232
* @return RuleError[]
3333
*/
3434
public function check(

src/Rules/Functions/ParamAttributesRule.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\AttributesCheck;
99
use PHPStan\Rules\Rule;
10-
use function count;
1110

1211
/**
1312
* @implements Rule<Node\Param>
@@ -27,25 +26,16 @@ public function getNodeType(): string
2726
public function processNode(Node $node, Scope $scope): array
2827
{
2928
$targetName = 'parameter';
29+
$targetType = Attribute::TARGET_PARAMETER;
3030
if ($node->flags !== 0) {
3131
$targetName = 'parameter or property';
32-
33-
$propertyTargetErrors = $this->attributesCheck->check(
34-
$scope,
35-
$node->attrGroups,
36-
Attribute::TARGET_PROPERTY,
37-
$targetName,
38-
);
39-
40-
if (count($propertyTargetErrors) === 0) {
41-
return $propertyTargetErrors;
42-
}
32+
$targetType |= Attribute::TARGET_PROPERTY;
4333
}
4434

4535
return $this->attributesCheck->check(
4636
$scope,
4737
$node->attrGroups,
48-
Attribute::TARGET_PARAMETER,
38+
$targetType,
4939
$targetName,
5040
);
5141
}

tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ public function testSensitiveParameterAttribute(): void
6666
$this->analyse([__DIR__ . '/data/sensitive-parameter.php'], []);
6767
}
6868

69+
public function testBug10298(): void
70+
{
71+
$this->analyse([__DIR__ . '/data/bug-10298.php'], []);
72+
}
73+
6974
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10298;
4+
5+
#[\Attribute(\Attribute::TARGET_PROPERTY)]
6+
class PropAttr {}
7+
8+
#[\Attribute(\Attribute::TARGET_PARAMETER)]
9+
class ParamAttr {}
10+
11+
class Test
12+
{
13+
public function __construct(
14+
#[PropAttr]
15+
#[ParamAttr]
16+
public string $test
17+
) {}
18+
}

0 commit comments

Comments
 (0)