Skip to content

Commit

Permalink
Fix mixing property and param attributes on promoted property
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RobertMe authored and ondrejmirtes committed Jan 6, 2024
1 parent a81df66 commit 2e08c9f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Rules/AttributesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

/**
* @param AttributeGroup[] $attrGroups
* @param Attribute::TARGET_* $requiredTarget
* @param int-mask-of<Attribute::TARGET_*> $requiredTarget
* @return RuleError[]
*/
public function check(
Expand Down
16 changes: 3 additions & 13 deletions src/Rules/Functions/ParamAttributesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\Rule;
use function count;

/**
* @implements Rule<Node\Param>
Expand All @@ -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,
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10298.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug10298;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class PropAttr {}

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class ParamAttr {}

class Test
{
public function __construct(
#[PropAttr]
#[ParamAttr]
public string $test
) {}
}

0 comments on commit 2e08c9f

Please sign in to comment.