Skip to content
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

Fix mixing property and param attributes on promoted property #2825

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from this issue phpstan/phpstan#10385 maybe you could improve the test suite by adding another test case to cover when an attribute has both property and parameter targets like:

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER)]
class PropAndParamAttr {}

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there is already such a test. Feel free to check that and send a PR in case it isn't 😊

) {}
}