Skip to content

Commit

Permalink
Bleeding edge - ParamAttributesRule - promoted property attribute nee…
Browse files Browse the repository at this point in the history
…ds to target both parameters and properties
  • Loading branch information
ondrejmirtes committed Dec 15, 2023
1 parent 154cc40 commit 25d1552
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parameters:
missingMagicSerializationRule: true
nullContextForVoidReturningFunctions: true
unescapeStrings: true
promotedPropertyAttribute: true
alwaysCheckTooWideReturnTypeFinalMethods: true
duplicateStubs: true
logicalXor: true
Expand Down
8 changes: 7 additions & 1 deletion conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ rules:
- PHPStan\Rules\Functions\FunctionAttributesRule
- PHPStan\Rules\Functions\InnerFunctionRule
- PHPStan\Rules\Functions\InvalidLexicalVariablesInClosureUseRule
- PHPStan\Rules\Functions\ParamAttributesRule
- PHPStan\Rules\Functions\PrintfParametersRule
- PHPStan\Rules\Functions\RedefinedParametersRule
- PHPStan\Rules\Functions\ReturnNullsafeByRefRule
Expand Down Expand Up @@ -152,6 +151,13 @@ services:
arguments:
checkFunctionNameCase: %checkFunctionNameCase%

-
class: PHPStan\Rules\Functions\ParamAttributesRule
tags:
- phpstan.rules.rule
arguments:
checkPromotedPropertyAttribute: %featureToggles.promotedPropertyAttribute%

-
class: PHPStan\Rules\Constants\OverridingConstantRule
arguments:
Expand Down
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ parameters:
missingMagicSerializationRule: false
nullContextForVoidReturningFunctions: false
unescapeStrings: false
promotedPropertyAttribute: false
alwaysCheckTooWideReturnTypeFinalMethods: false
duplicateStubs: false
logicalXor: false
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parametersSchema:
missingMagicSerializationRule: bool()
nullContextForVoidReturningFunctions: bool()
unescapeStrings: bool()
promotedPropertyAttribute: bool()
alwaysCheckTooWideReturnTypeFinalMethods: bool()
duplicateStubs: bool()
logicalXor: bool()
Expand Down
33 changes: 23 additions & 10 deletions src/Rules/Functions/ParamAttributesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class ParamAttributesRule implements Rule
{

public function __construct(private AttributesCheck $attributesCheck)
public function __construct(private AttributesCheck $attributesCheck, private bool $checkPromotedPropertyAttribute)
{
}

Expand All @@ -28,17 +28,30 @@ public function processNode(Node $node, Scope $scope): array
{
$targetName = 'parameter';
if ($node->flags !== 0) {
$targetName = 'parameter or property';
if ($this->checkPromotedPropertyAttribute) {
$propertyTargetErrors = $this->attributesCheck->check(
$scope,
$node->attrGroups,
Attribute::TARGET_PROPERTY,
'property',
);

$propertyTargetErrors = $this->attributesCheck->check(
$scope,
$node->attrGroups,
Attribute::TARGET_PROPERTY,
$targetName,
);
if (count($propertyTargetErrors) > 0) {
return $propertyTargetErrors;
}
} else {
$targetName = 'parameter or property';

if (count($propertyTargetErrors) === 0) {
return $propertyTargetErrors;
$propertyTargetErrors = $this->attributesCheck->check(
$scope,
$node->attrGroups,
Attribute::TARGET_PROPERTY,
$targetName,
);

if (count($propertyTargetErrors) === 0) {
return $propertyTargetErrors;
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class ParamAttributesRuleTest extends RuleTestCase
{

private bool $checkPromotedPropertyAttribute;

protected function getRule(): Rule
{
$reflectionProvider = $this->createReflectionProvider();
Expand All @@ -40,11 +42,13 @@ protected function getRule(): Rule
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
$this->checkPromotedPropertyAttribute,
);
}

public function testRule(): void
{
$this->checkPromotedPropertyAttribute = false;
$this->analyse([__DIR__ . '/data/param-attributes.php'], [
[
'Attribute class ParamAttributes\Foo does not have the parameter target.',
Expand All @@ -61,8 +65,44 @@ public function testRule(): void
]);
}

public function testRuleCheckPromotedPropertyAttribute(): void
{
$this->checkPromotedPropertyAttribute = true;
$this->analyse([__DIR__ . '/data/param-attributes.php'], [
[
'Attribute class ParamAttributes\Foo does not have the parameter target.',
33,
],
[
'Attribute class ParamAttributes\Foo does not have the property target.',
72,
],
[
'Attribute class ParamAttributes\Bar does not have the property target.',
74,
],
[
'Attribute class ParamAttributes\Qux does not have the parameter target.',
76,
],
[
'Attribute class ParamAttributes\Qux does not have the parameter target.',
78,
],
[
'Attribute class ParamAttributes\Qux does not have the parameter target.',
80,
],
[
'Attribute class ParamAttributes\Qux does not have the parameter target.',
82,
],
]);
}

public function testSensitiveParameterAttribute(): void
{
$this->checkPromotedPropertyAttribute = false;
$this->analyse([__DIR__ . '/data/sensitive-parameter.php'], []);
}

Expand Down

0 comments on commit 25d1552

Please sign in to comment.