Skip to content

Commit

Permalink
AttributesCheck - detect deprecated attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 1, 2023
1 parent cd55cb2 commit e46b0b4
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,8 @@ services:

-
class: PHPStan\Rules\AttributesCheck
arguments:
deprecationRulesInstalled: %deprecationRulesInstalled%

-
class: PHPStan\Rules\Arrays\NonexistentOffsetInArrayDimFetchCheck
Expand Down
10 changes: 10 additions & 0 deletions src/Rules/AttributesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
private ReflectionProvider $reflectionProvider,
private FunctionCallParametersCheck $functionCallParametersCheck,
private ClassCaseSensitivityCheck $classCaseSensitivityCheck,
private bool $deprecationRulesInstalled,
)
{
}
Expand Down Expand Up @@ -84,6 +85,15 @@ public function check(
$alreadyPresent[$loweredName] = true;
}

if ($this->deprecationRulesInstalled && $attributeClass->isDeprecated()) {
if ($attributeClass->getDeprecatedDescription() !== null) {
$deprecatedError = sprintf('Attribute class %s is deprecated: %s', $name, $attributeClass->getDeprecatedDescription());
} else {
$deprecatedError = sprintf('Attribute class %s is deprecated.', $name);
}
$errors[] = RuleErrorBuilder::message($deprecatedError)->line($attribute->getLine())->build();
}

if (!$attributeClass->hasConstructor()) {
if (count($attribute->args) > 0) {
$errors[] = RuleErrorBuilder::message(sprintf('Attribute class %s does not have a constructor and must be instantiated without any parameters.', $name))->line($attribute->getLine())->build();
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function getRule(): Rule
true,
),
new ClassCaseSensitivityCheck($reflectionProvider, false),
true,
),
);
}
Expand All @@ -51,4 +52,18 @@ public function testRule(): void
]);
}

public function testDeprecatedAttribute(): void
{
$this->analyse([__DIR__ . '/data/property-attributes-deprecated.php'], [
[
'Attribute class DeprecatedPropertyAttribute\DoSomethingTheOldWay is deprecated.',
16,
],
[
'Attribute class DeprecatedPropertyAttribute\DoSomethingTheOldWayWithDescription is deprecated: Use something else please',
19,
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DeprecatedPropertyAttribute;

/**
* @deprecated
*/
#[\Attribute]
final class DoSomethingTheOldWay
{
}


final class SomeDTO
{
#[DoSomethingTheOldWay]
public readonly string $property;

#[DoSomethingTheOldWayWithDescription]
public readonly string $property2;
}

/**
* @deprecated Use something else please
*/
#[\Attribute]
final class DoSomethingTheOldWayWithDescription
{
}

0 comments on commit e46b0b4

Please sign in to comment.