Skip to content

Commit 629cfe3

Browse files
committed
Modernize rules to use RuleErrorBuilder and report error identifiers
1 parent 94d68d3 commit 629cfe3

18 files changed

+197
-145
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.2 || ^8.0",
10-
"phpstan/phpstan": "^1.10.3"
10+
"phpstan/phpstan": "^1.11"
1111
},
1212
"require-dev": {
1313
"php-parallel-lint/php-parallel-lint": "^1.2",

src/Rules/Deprecations/AccessDeprecatedPropertyRule.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use PHPStan\Reflection\MissingPropertyFromReflectionException;
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Rules\Rule;
13+
use PHPStan\Rules\RuleErrorBuilder;
1314
use function sprintf;
15+
use function strtolower;
1416

1517
/**
1618
* @implements Rule<PropertyFetch>
@@ -53,19 +55,25 @@ public function processNode(Node $node, Scope $scope): array
5355
if ($propertyReflection->isDeprecated()->yes()) {
5456
$description = $propertyReflection->getDeprecatedDescription();
5557
if ($description === null) {
56-
return [sprintf(
57-
'Access to deprecated property $%s of class %s.',
58-
$propertyName,
59-
$propertyReflection->getDeclaringClass()->getName()
60-
)];
58+
return [
59+
RuleErrorBuilder::message(sprintf(
60+
'Access to deprecated property $%s of %s %s.',
61+
$propertyName,
62+
strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()),
63+
$propertyReflection->getDeclaringClass()->getName()
64+
))->identifier('property.deprecated')->build(),
65+
];
6166
}
6267

63-
return [sprintf(
64-
"Access to deprecated property $%s of class %s:\n%s",
65-
$propertyName,
66-
$propertyReflection->getDeclaringClass()->getName(),
67-
$description
68-
)];
68+
return [
69+
RuleErrorBuilder::message(sprintf(
70+
"Access to deprecated property $%s of %s %s:\n%s",
71+
$propertyName,
72+
strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()),
73+
$propertyReflection->getDeclaringClass()->getName(),
74+
$description
75+
))->identifier('property.deprecated')->build(),
76+
];
6977
}
7078
} catch (ClassNotFoundException $e) {
7179
// Other rules will notify if the class is not found

src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use PHPStan\Reflection\MissingPropertyFromReflectionException;
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use PHPStan\Rules\Rule;
14+
use PHPStan\Rules\RuleErrorBuilder;
1415
use PHPStan\Rules\RuleLevelHelper;
1516
use PHPStan\Type\ErrorType;
1617
use PHPStan\Type\Type;
1718
use function sprintf;
19+
use function strtolower;
1820

1921
/**
2022
* @implements Rule<StaticPropertyFetch>
@@ -84,19 +86,25 @@ static function (Type $type) use ($propertyName): bool {
8486
if ($property->isDeprecated()->yes()) {
8587
$description = $property->getDeprecatedDescription();
8688
if ($description === null) {
87-
return [sprintf(
88-
'Access to deprecated static property $%s of class %s.',
89-
$propertyName,
90-
$property->getDeclaringClass()->getName()
91-
)];
89+
return [
90+
RuleErrorBuilder::message(sprintf(
91+
'Access to deprecated static property $%s of %s %s.',
92+
$propertyName,
93+
strtolower($property->getDeclaringClass()->getClassTypeDescription()),
94+
$property->getDeclaringClass()->getName()
95+
))->identifier('staticProperty.deprecated')->build(),
96+
];
9297
}
9398

94-
return [sprintf(
95-
"Access to deprecated static property $%s of class %s:\n%s",
96-
$propertyName,
97-
$property->getDeclaringClass()->getName(),
98-
$description
99-
)];
99+
return [
100+
RuleErrorBuilder::message(sprintf(
101+
"Access to deprecated static property $%s of %s %s:\n%s",
102+
$propertyName,
103+
strtolower($property->getDeclaringClass()->getClassTypeDescription()),
104+
$property->getDeclaringClass()->getName(),
105+
$description
106+
))->identifier('staticProperty.deprecated')->build(),
107+
];
100108
}
101109
}
102110

src/Rules/Deprecations/CallToDeprecatedFunctionRule.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Broker\FunctionNotFoundException;
1010
use PHPStan\Reflection\ReflectionProvider;
1111
use PHPStan\Rules\Rule;
12+
use PHPStan\Rules\RuleErrorBuilder;
1213
use function sprintf;
1314

1415
/**
@@ -50,17 +51,21 @@ public function processNode(Node $node, Scope $scope): array
5051
if ($function->isDeprecated()->yes()) {
5152
$description = $function->getDeprecatedDescription();
5253
if ($description === null) {
53-
return [sprintf(
54-
'Call to deprecated function %s().',
55-
$function->getName()
56-
)];
54+
return [
55+
RuleErrorBuilder::message(sprintf(
56+
'Call to deprecated function %s().',
57+
$function->getName()
58+
))->identifier('function.deprecated')->build(),
59+
];
5760
}
5861

59-
return [sprintf(
60-
"Call to deprecated function %s():\n%s",
61-
$function->getName(),
62-
$description
63-
)];
62+
return [
63+
RuleErrorBuilder::message(sprintf(
64+
"Call to deprecated function %s():\n%s",
65+
$function->getName(),
66+
$description
67+
))->identifier('function.deprecated')->build(),
68+
];
6469
}
6570

6671
return [];

src/Rules/Deprecations/CallToDeprecatedMethodRule.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use PHPStan\Reflection\MissingMethodFromReflectionException;
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Rules\Rule;
13+
use PHPStan\Rules\RuleErrorBuilder;
1314
use function sprintf;
15+
use function strtolower;
1416

1517
/**
1618
* @implements Rule<MethodCall>
@@ -56,19 +58,25 @@ public function processNode(Node $node, Scope $scope): array
5658

5759
$description = $methodReflection->getDeprecatedDescription();
5860
if ($description === null) {
59-
return [sprintf(
60-
'Call to deprecated method %s() of class %s.',
61-
$methodReflection->getName(),
62-
$methodReflection->getDeclaringClass()->getName()
63-
)];
61+
return [
62+
RuleErrorBuilder::message(sprintf(
63+
'Call to deprecated method %s() of %s %s.',
64+
$methodReflection->getName(),
65+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
66+
$methodReflection->getDeclaringClass()->getName()
67+
))->identifier('method.deprecated')->build(),
68+
];
6469
}
6570

66-
return [sprintf(
67-
"Call to deprecated method %s() of class %s:\n%s",
68-
$methodReflection->getName(),
69-
$methodReflection->getDeclaringClass()->getName(),
70-
$description
71-
)];
71+
return [
72+
RuleErrorBuilder::message(sprintf(
73+
"Call to deprecated method %s() of %s %s:\n%s",
74+
$methodReflection->getName(),
75+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
76+
$methodReflection->getDeclaringClass()->getName(),
77+
$description
78+
))->identifier('method.deprecated')->build(),
79+
];
7280
} catch (ClassNotFoundException $e) {
7381
// Other rules will notify if the class is not found
7482
} catch (MissingMethodFromReflectionException $e) {

src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use PHPStan\Reflection\MissingMethodFromReflectionException;
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use PHPStan\Rules\Rule;
14+
use PHPStan\Rules\RuleErrorBuilder;
1415
use PHPStan\Rules\RuleLevelHelper;
1516
use PHPStan\Type\ErrorType;
1617
use PHPStan\Type\Type;
1718
use function sprintf;
19+
use function strtolower;
1820

1921
/**
2022
* @implements Rule<StaticCall>
@@ -86,18 +88,20 @@ static function (Type $type) use ($methodName): bool {
8688
if ($class->isDeprecated()) {
8789
$classDescription = $class->getDeprecatedDescription();
8890
if ($classDescription === null) {
89-
$errors[] = sprintf(
90-
'Call to method %s() of deprecated class %s.',
91+
$errors[] = RuleErrorBuilder::message(sprintf(
92+
'Call to method %s() of deprecated %s %s.',
9193
$methodReflection->getName(),
94+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
9295
$methodReflection->getDeclaringClass()->getName()
93-
);
96+
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
9497
} else {
95-
$errors[] = sprintf(
96-
"Call to method %s() of deprecated class %s:\n%s",
98+
$errors[] = RuleErrorBuilder::message(sprintf(
99+
"Call to method %s() of deprecated %s %s:\n%s",
97100
$methodReflection->getName(),
101+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
98102
$methodReflection->getDeclaringClass()->getName(),
99103
$classDescription
100-
);
104+
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
101105
}
102106
}
103107

@@ -107,18 +111,20 @@ static function (Type $type) use ($methodName): bool {
107111

108112
$description = $methodReflection->getDeprecatedDescription();
109113
if ($description === null) {
110-
$errors[] = sprintf(
111-
'Call to deprecated method %s() of class %s.',
114+
$errors[] = RuleErrorBuilder::message(sprintf(
115+
'Call to deprecated method %s() of %s %s.',
112116
$methodReflection->getName(),
117+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
113118
$methodReflection->getDeclaringClass()->getName()
114-
);
119+
))->identifier('staticMethod.deprecated')->build();
115120
} else {
116-
$errors[] = sprintf(
117-
"Call to deprecated method %s() of class %s:\n%s",
121+
$errors[] = RuleErrorBuilder::message(sprintf(
122+
"Call to deprecated method %s() of %s %s:\n%s",
118123
$methodReflection->getName(),
124+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
119125
$methodReflection->getDeclaringClass()->getName(),
120126
$description
121-
);
127+
))->identifier('staticMethod.deprecated')->build();
122128
}
123129
}
124130

src/Rules/Deprecations/DeprecatedClassHelper.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ public function __construct(ReflectionProvider $reflectionProvider)
1818
$this->reflectionProvider = $reflectionProvider;
1919
}
2020

21-
public function getClassType(ClassReflection $class): string
22-
{
23-
if ($class->isInterface()) {
24-
return 'interface';
25-
}
26-
27-
return 'class';
28-
}
29-
3021
public function getClassDeprecationDescription(ClassReflection $class): string
3122
{
3223
$description = $class->getDeprecatedDescription();

src/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRule.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Broker\ClassNotFoundException;
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Rules\Rule;
13+
use PHPStan\Rules\RuleErrorBuilder;
1314
use PHPStan\Rules\RuleLevelHelper;
1415
use PHPStan\Type\ErrorType;
1516
use PHPStan\Type\Type;
@@ -83,18 +84,20 @@ static function (Type $type) use ($constantName): bool {
8384
if ($class->isDeprecated()) {
8485
$classDescription = $class->getDeprecatedDescription();
8586
if ($classDescription === null) {
86-
$errors[] = sprintf(
87-
'Fetching class constant %s of deprecated class %s.',
87+
$errors[] = RuleErrorBuilder::message(sprintf(
88+
'Fetching class constant %s of deprecated %s %s.',
8889
$constantName,
90+
strtolower($class->getClassTypeDescription()),
8991
$referencedClass
90-
);
92+
))->identifier(sprintf('classConstant.deprecated%s', $class->getClassTypeDescription()))->build();
9193
} else {
92-
$errors[] = sprintf(
93-
"Fetching class constant %s of deprecated class %s:\n%s",
94+
$errors[] = RuleErrorBuilder::message(sprintf(
95+
"Fetching class constant %s of deprecated %s %s:\n%s",
9496
$constantName,
97+
strtolower($class->getClassTypeDescription()),
9598
$referencedClass,
9699
$classDescription
97-
);
100+
))->identifier(sprintf('classConstant.deprecated%s', $class->getClassTypeDescription()))->build();
98101
}
99102
}
100103

@@ -114,18 +117,20 @@ static function (Type $type) use ($constantName): bool {
114117

115118
$description = $constantReflection->getDeprecatedDescription();
116119
if ($description === null) {
117-
$errors[] = sprintf(
118-
'Fetching deprecated class constant %s of class %s.',
120+
$errors[] = RuleErrorBuilder::message(sprintf(
121+
'Fetching deprecated class constant %s of %s %s.',
119122
$constantName,
123+
strtolower($class->getClassTypeDescription()),
120124
$referencedClass
121-
);
125+
))->identifier('classConstant.deprecated')->build();
122126
} else {
123-
$errors[] = sprintf(
124-
"Fetching deprecated class constant %s of class %s:\n%s",
127+
$errors[] = RuleErrorBuilder::message(sprintf(
128+
"Fetching deprecated class constant %s of %s %s:\n%s",
125129
$constantName,
130+
strtolower($class->getClassTypeDescription()),
126131
$referencedClass,
127132
$description
128-
);
133+
))->identifier('classConstant.deprecated')->build();
129134
}
130135
}
131136

src/Rules/Deprecations/FetchingDeprecatedConstRule.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\ReflectionProvider;
99
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
1011
use function sprintf;
1112
use const PHP_VERSION_ID;
1213

@@ -51,17 +52,21 @@ public function processNode(Node $node, Scope $scope): array
5152
$constantReflection = $this->reflectionProvider->getConstant($node->name, $scope);
5253

5354
if ($constantReflection->isDeprecated()->yes()) {
54-
return [sprintf(
55-
$constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.',
56-
$constantReflection->getName()
57-
)];
55+
return [
56+
RuleErrorBuilder::message(sprintf(
57+
$constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.',
58+
$constantReflection->getName()
59+
))->identifier('constant.deprecated')->build(),
60+
];
5861
}
5962

6063
if (isset($this->deprecatedConstants[$constantReflection->getName()])) {
61-
return [sprintf(
62-
$this->deprecatedConstants[$constantReflection->getName()],
63-
$constantReflection->getName()
64-
)];
64+
return [
65+
RuleErrorBuilder::message(sprintf(
66+
$this->deprecatedConstants[$constantReflection->getName()],
67+
$constantReflection->getName()
68+
))->identifier('constant.deprecated')->build(),
69+
];
6570
}
6671

6772
return [];

0 commit comments

Comments
 (0)