Skip to content

Commit e46b0b4

Browse files
committed
AttributesCheck - detect deprecated attributes
1 parent cd55cb2 commit e46b0b4

12 files changed

+64
-0
lines changed

conf/config.neon

+2
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,8 @@ services:
945945

946946
-
947947
class: PHPStan\Rules\AttributesCheck
948+
arguments:
949+
deprecationRulesInstalled: %deprecationRulesInstalled%
948950

949951
-
950952
class: PHPStan\Rules\Arrays\NonexistentOffsetInArrayDimFetchCheck

src/Rules/AttributesCheck.php

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
private ReflectionProvider $reflectionProvider,
2222
private FunctionCallParametersCheck $functionCallParametersCheck,
2323
private ClassCaseSensitivityCheck $classCaseSensitivityCheck,
24+
private bool $deprecationRulesInstalled,
2425
)
2526
{
2627
}
@@ -84,6 +85,15 @@ public function check(
8485
$alreadyPresent[$loweredName] = true;
8586
}
8687

88+
if ($this->deprecationRulesInstalled && $attributeClass->isDeprecated()) {
89+
if ($attributeClass->getDeprecatedDescription() !== null) {
90+
$deprecatedError = sprintf('Attribute class %s is deprecated: %s', $name, $attributeClass->getDeprecatedDescription());
91+
} else {
92+
$deprecatedError = sprintf('Attribute class %s is deprecated.', $name);
93+
}
94+
$errors[] = RuleErrorBuilder::message($deprecatedError)->line($attribute->getLine())->build();
95+
}
96+
8797
if (!$attributeClass->hasConstructor()) {
8898
if (count($attribute->args) > 0) {
8999
$errors[] = RuleErrorBuilder::message(sprintf('Attribute class %s does not have a constructor and must be instantiated without any parameters.', $name))->line($attribute->getLine())->build();

tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected function getRule(): Rule
3939
true,
4040
),
4141
new ClassCaseSensitivityCheck($reflectionProvider, false),
42+
true,
4243
),
4344
);
4445
}

tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getRule(): Rule
3838
true,
3939
),
4040
new ClassCaseSensitivityCheck($reflectionProvider, false),
41+
true,
4142
),
4243
);
4344
}

tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getRule(): Rule
3838
true,
3939
),
4040
new ClassCaseSensitivityCheck($reflectionProvider, false),
41+
true,
4142
),
4243
);
4344
}

tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getRule(): Rule
3838
true,
3939
),
4040
new ClassCaseSensitivityCheck($reflectionProvider, false),
41+
true,
4142
),
4243
);
4344
}

tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getRule(): Rule
3838
true,
3939
),
4040
new ClassCaseSensitivityCheck($reflectionProvider, false),
41+
true,
4142
),
4243
);
4344
}

tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getRule(): Rule
3838
true,
3939
),
4040
new ClassCaseSensitivityCheck($reflectionProvider, false),
41+
true,
4142
),
4243
);
4344
}

tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function getRule(): Rule
3838
true,
3939
),
4040
new ClassCaseSensitivityCheck($reflectionProvider, false),
41+
true,
4142
),
4243
);
4344
}

tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function getRule(): Rule
4040
true,
4141
),
4242
new ClassCaseSensitivityCheck($reflectionProvider, false),
43+
true,
4344
),
4445
);
4546
}

tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function getRule(): Rule
3737
true,
3838
),
3939
new ClassCaseSensitivityCheck($reflectionProvider, false),
40+
true,
4041
),
4142
);
4243
}
@@ -51,4 +52,18 @@ public function testRule(): void
5152
]);
5253
}
5354

55+
public function testDeprecatedAttribute(): void
56+
{
57+
$this->analyse([__DIR__ . '/data/property-attributes-deprecated.php'], [
58+
[
59+
'Attribute class DeprecatedPropertyAttribute\DoSomethingTheOldWay is deprecated.',
60+
16,
61+
],
62+
[
63+
'Attribute class DeprecatedPropertyAttribute\DoSomethingTheOldWayWithDescription is deprecated: Use something else please',
64+
19,
65+
],
66+
]);
67+
}
68+
5469
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace DeprecatedPropertyAttribute;
4+
5+
/**
6+
* @deprecated
7+
*/
8+
#[\Attribute]
9+
final class DoSomethingTheOldWay
10+
{
11+
}
12+
13+
14+
final class SomeDTO
15+
{
16+
#[DoSomethingTheOldWay]
17+
public readonly string $property;
18+
19+
#[DoSomethingTheOldWayWithDescription]
20+
public readonly string $property2;
21+
}
22+
23+
/**
24+
* @deprecated Use something else please
25+
*/
26+
#[\Attribute]
27+
final class DoSomethingTheOldWayWithDescription
28+
{
29+
}

0 commit comments

Comments
 (0)