Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2313a63

Browse files
Modernize rules to use RuleErrorBuilder and report error identifiers
1 parent ca9e4fd commit 2313a63

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>
@@ -57,19 +59,25 @@ public function processNode(Node $node, Scope $scope): array
5759
if ($propertyReflection->isDeprecated()->yes()) {
5860
$description = $propertyReflection->getDeprecatedDescription();
5961
if ($description === null) {
60-
return [sprintf(
61-
'Access to deprecated property $%s of class %s.',
62-
$propertyName,
63-
$propertyReflection->getDeclaringClass()->getName()
64-
)];
62+
return [
63+
RuleErrorBuilder::message(sprintf(
64+
'Access to deprecated property $%s of %s %s.',
65+
$propertyName,
66+
strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()),
67+
$propertyReflection->getDeclaringClass()->getName()
68+
))->identifier('property.deprecated')->build(),
69+
];
6570
}
6671

67-
return [sprintf(
68-
"Access to deprecated property $%s of class %s:\n%s",
69-
$propertyName,
70-
$propertyReflection->getDeclaringClass()->getName(),
71-
$description
72-
)];
72+
return [
73+
RuleErrorBuilder::message(sprintf(
74+
"Access to deprecated property $%s of %s %s:\n%s",
75+
$propertyName,
76+
strtolower($propertyReflection->getDeclaringClass()->getClassTypeDescription()),
77+
$propertyReflection->getDeclaringClass()->getName(),
78+
$description
79+
))->identifier('property.deprecated')->build(),
80+
];
7381
}
7482
} catch (ClassNotFoundException $e) {
7583
// 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>
@@ -88,19 +90,25 @@ static function (Type $type) use ($propertyName): bool {
8890
if ($property->isDeprecated()->yes()) {
8991
$description = $property->getDeprecatedDescription();
9092
if ($description === null) {
91-
return [sprintf(
92-
'Access to deprecated static property $%s of class %s.',
93-
$propertyName,
94-
$property->getDeclaringClass()->getName()
95-
)];
93+
return [
94+
RuleErrorBuilder::message(sprintf(
95+
'Access to deprecated static property $%s of %s %s.',
96+
$propertyName,
97+
strtolower($property->getDeclaringClass()->getClassTypeDescription()),
98+
$property->getDeclaringClass()->getName()
99+
))->identifier('staticProperty.deprecated')->build(),
100+
];
96101
}
97102

98-
return [sprintf(
99-
"Access to deprecated static property $%s of class %s:\n%s",
100-
$propertyName,
101-
$property->getDeclaringClass()->getName(),
102-
$description
103-
)];
103+
return [
104+
RuleErrorBuilder::message(sprintf(
105+
"Access to deprecated static property $%s of %s %s:\n%s",
106+
$propertyName,
107+
strtolower($property->getDeclaringClass()->getClassTypeDescription()),
108+
$property->getDeclaringClass()->getName(),
109+
$description
110+
))->identifier('staticProperty.deprecated')->build(),
111+
];
104112
}
105113
}
106114

‎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
/**
@@ -54,17 +55,21 @@ public function processNode(Node $node, Scope $scope): array
5455
if ($function->isDeprecated()->yes()) {
5556
$description = $function->getDeprecatedDescription();
5657
if ($description === null) {
57-
return [sprintf(
58-
'Call to deprecated function %s().',
59-
$function->getName()
60-
)];
58+
return [
59+
RuleErrorBuilder::message(sprintf(
60+
'Call to deprecated function %s().',
61+
$function->getName()
62+
))->identifier('function.deprecated')->build(),
63+
];
6164
}
6265

63-
return [sprintf(
64-
"Call to deprecated function %s():\n%s",
65-
$function->getName(),
66-
$description
67-
)];
66+
return [
67+
RuleErrorBuilder::message(sprintf(
68+
"Call to deprecated function %s():\n%s",
69+
$function->getName(),
70+
$description
71+
))->identifier('function.deprecated')->build(),
72+
];
6873
}
6974

7075
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>
@@ -60,19 +62,25 @@ public function processNode(Node $node, Scope $scope): array
6062

6163
$description = $methodReflection->getDeprecatedDescription();
6264
if ($description === null) {
63-
return [sprintf(
64-
'Call to deprecated method %s() of class %s.',
65-
$methodReflection->getName(),
66-
$methodReflection->getDeclaringClass()->getName()
67-
)];
65+
return [
66+
RuleErrorBuilder::message(sprintf(
67+
'Call to deprecated method %s() of %s %s.',
68+
$methodReflection->getName(),
69+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
70+
$methodReflection->getDeclaringClass()->getName()
71+
))->identifier('method.deprecated')->build(),
72+
];
6873
}
6974

70-
return [sprintf(
71-
"Call to deprecated method %s() of class %s:\n%s",
72-
$methodReflection->getName(),
73-
$methodReflection->getDeclaringClass()->getName(),
74-
$description
75-
)];
75+
return [
76+
RuleErrorBuilder::message(sprintf(
77+
"Call to deprecated method %s() of %s %s:\n%s",
78+
$methodReflection->getName(),
79+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
80+
$methodReflection->getDeclaringClass()->getName(),
81+
$description
82+
))->identifier('method.deprecated')->build(),
83+
];
7684
} catch (ClassNotFoundException $e) {
7785
// Other rules will notify if the class is not found
7886
} 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>
@@ -90,18 +92,20 @@ static function (Type $type) use ($methodName): bool {
9092
if ($class->isDeprecated()) {
9193
$classDescription = $class->getDeprecatedDescription();
9294
if ($classDescription === null) {
93-
$errors[] = sprintf(
94-
'Call to method %s() of deprecated class %s.',
95+
$errors[] = RuleErrorBuilder::message(sprintf(
96+
'Call to method %s() of deprecated %s %s.',
9597
$methodReflection->getName(),
98+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
9699
$methodReflection->getDeclaringClass()->getName()
97-
);
100+
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
98101
} else {
99-
$errors[] = sprintf(
100-
"Call to method %s() of deprecated class %s:\n%s",
102+
$errors[] = RuleErrorBuilder::message(sprintf(
103+
"Call to method %s() of deprecated %s %s:\n%s",
101104
$methodReflection->getName(),
105+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
102106
$methodReflection->getDeclaringClass()->getName(),
103107
$classDescription
104-
);
108+
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
105109
}
106110
}
107111

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

112116
$description = $methodReflection->getDeprecatedDescription();
113117
if ($description === null) {
114-
$errors[] = sprintf(
115-
'Call to deprecated method %s() of class %s.',
118+
$errors[] = RuleErrorBuilder::message(sprintf(
119+
'Call to deprecated method %s() of %s %s.',
116120
$methodReflection->getName(),
121+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
117122
$methodReflection->getDeclaringClass()->getName()
118-
);
123+
))->identifier('staticMethod.deprecated')->build();
119124
} else {
120-
$errors[] = sprintf(
121-
"Call to deprecated method %s() of class %s:\n%s",
125+
$errors[] = RuleErrorBuilder::message(sprintf(
126+
"Call to deprecated method %s() of %s %s:\n%s",
122127
$methodReflection->getName(),
128+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
123129
$methodReflection->getDeclaringClass()->getName(),
124130
$description
125-
);
131+
))->identifier('staticMethod.deprecated')->build();
126132
}
127133
}
128134

‎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;
@@ -87,18 +88,20 @@ static function (Type $type) use ($constantName): bool {
8788
if ($class->isDeprecated()) {
8889
$classDescription = $class->getDeprecatedDescription();
8990
if ($classDescription === null) {
90-
$errors[] = sprintf(
91-
'Fetching class constant %s of deprecated class %s.',
91+
$errors[] = RuleErrorBuilder::message(sprintf(
92+
'Fetching class constant %s of deprecated %s %s.',
9293
$constantName,
94+
strtolower($class->getClassTypeDescription()),
9395
$referencedClass
94-
);
96+
))->identifier(sprintf('classConstant.deprecated%s', $class->getClassTypeDescription()))->build();
9597
} else {
96-
$errors[] = sprintf(
97-
"Fetching class constant %s of deprecated class %s:\n%s",
98+
$errors[] = RuleErrorBuilder::message(sprintf(
99+
"Fetching class constant %s of deprecated %s %s:\n%s",
98100
$constantName,
101+
strtolower($class->getClassTypeDescription()),
99102
$referencedClass,
100103
$classDescription
101-
);
104+
))->identifier(sprintf('classConstant.deprecated%s', $class->getClassTypeDescription()))->build();
102105
}
103106
}
104107

@@ -118,18 +121,20 @@ static function (Type $type) use ($constantName): bool {
118121

119122
$description = $constantReflection->getDeprecatedDescription();
120123
if ($description === null) {
121-
$errors[] = sprintf(
122-
'Fetching deprecated class constant %s of class %s.',
124+
$errors[] = RuleErrorBuilder::message(sprintf(
125+
'Fetching deprecated class constant %s of %s %s.',
123126
$constantName,
127+
strtolower($class->getClassTypeDescription()),
124128
$referencedClass
125-
);
129+
))->identifier('classConstant.deprecated')->build();
126130
} else {
127-
$errors[] = sprintf(
128-
"Fetching deprecated class constant %s of class %s:\n%s",
131+
$errors[] = RuleErrorBuilder::message(sprintf(
132+
"Fetching deprecated class constant %s of %s %s:\n%s",
129133
$constantName,
134+
strtolower($class->getClassTypeDescription()),
130135
$referencedClass,
131136
$description
132-
);
137+
))->identifier('classConstant.deprecated')->build();
133138
}
134139
}
135140

‎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

@@ -55,17 +56,21 @@ public function processNode(Node $node, Scope $scope): array
5556
$constantReflection = $this->reflectionProvider->getConstant($node->name, $scope);
5657

5758
if ($constantReflection->isDeprecated()->yes()) {
58-
return [sprintf(
59-
$constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.',
60-
$constantReflection->getName()
61-
)];
59+
return [
60+
RuleErrorBuilder::message(sprintf(
61+
$constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.',
62+
$constantReflection->getName()
63+
))->identifier('constant.deprecated')->build(),
64+
];
6265
}
6366

6467
if (isset($this->deprecatedConstants[$constantReflection->getName()])) {
65-
return [sprintf(
66-
$this->deprecatedConstants[$constantReflection->getName()],
67-
$constantReflection->getName()
68-
)];
68+
return [
69+
RuleErrorBuilder::message(sprintf(
70+
$this->deprecatedConstants[$constantReflection->getName()],
71+
$constantReflection->getName()
72+
))->identifier('constant.deprecated')->build(),
73+
];
6974
}
7075

7176
return [];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /