Skip to content

Commit 908e232

Browse files
committed
Error identifiers
1 parent e468b76 commit 908e232

10 files changed

+64
-43
lines changed

src/Rules/PHPUnit/AnnotationHelper.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PhpParser\Comment\Doc;
6-
use PHPStan\Rules\RuleError;
6+
use PHPStan\Rules\IdentifierRuleError;
77
use PHPStan\Rules\RuleErrorBuilder;
88
use function array_key_exists;
99
use function in_array;
@@ -30,7 +30,7 @@ class AnnotationHelper
3030
];
3131

3232
/**
33-
* @return RuleError[] errors
33+
* @return list<IdentifierRuleError> errors
3434
*/
3535
public function processDocComment(Doc $docComment): array
3636
{
@@ -57,7 +57,7 @@ public function processDocComment(Doc $docComment): array
5757

5858
$errors[] = RuleErrorBuilder::message(
5959
'Annotation "' . $matches['annotation'] . '" is invalid, "@' . $matches['property'] . '" should be followed by a space and a value.'
60-
)->build();
60+
)->identifier('phpunit.invalidPhpDoc')->build();
6161
}
6262

6363
return $errors;

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function processNode(Node $node, Scope $scope): array
4141

4242
if ($expectedArgumentValue->name->toLowerString() === 'true') {
4343
return [
44-
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->build(),
44+
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),
4545
];
4646
}
4747

4848
if ($expectedArgumentValue->name->toLowerString() === 'false') {
4949
return [
50-
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->build(),
50+
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->identifier('phpunit.assertFalse')->build(),
5151
];
5252
}
5353

src/Rules/PHPUnit/AssertSameNullExpectedRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4141

4242
if ($expectedArgumentValue->name->toLowerString() === 'null') {
4343
return [
44-
RuleErrorBuilder::message('You should use assertNull() instead of assertSame(null, $actual).')->build(),
44+
RuleErrorBuilder::message('You should use assertNull() instead of assertSame(null, $actual).')->identifier('phpunit.assertNull')->build(),
4545
];
4646
}
4747

src/Rules/PHPUnit/AssertSameWithCountRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function processNode(Node $node, Scope $scope): array
4343
&& $right->name->toLowerString() === 'count'
4444
) {
4545
return [
46-
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')->build(),
46+
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')
47+
->identifier('phpunit.assertCount')
48+
->build(),
4749
];
4850
}
4951

@@ -57,7 +59,9 @@ public function processNode(Node $node, Scope $scope): array
5759

5860
if ((new ObjectType(Countable::class))->isSuperTypeOf($type)->yes()) {
5961
return [
60-
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')->build(),
62+
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
63+
->identifier('phpunit.assertCount')
64+
->build(),
6165
];
6266
}
6367
}

src/Rules/PHPUnit/ClassCoversExistsRule.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ public function processNode(Node $node, Scope $scope): array
5656
return [];
5757
}
5858

59-
$errors = [];
6059
$classPhpDoc = $classReflection->getResolvedPhpDoc();
6160
[$classCovers, $classCoversDefaultClasses] = $this->coversHelper->getCoverAnnotations($classPhpDoc);
6261

6362
if (count($classCoversDefaultClasses) >= 2) {
64-
$errors[] = RuleErrorBuilder::message(sprintf(
65-
'@coversDefaultClass is defined multiple times.'
66-
))->build();
67-
68-
return $errors;
63+
return [
64+
RuleErrorBuilder::message(sprintf(
65+
'@coversDefaultClass is defined multiple times.'
66+
))->identifier('phpunit.coversDuplicate')->build(),
67+
];
6968
}
7069

70+
$errors = [];
7171
$coversDefaultClass = array_shift($classCoversDefaultClasses);
7272

7373
if ($coversDefaultClass !== null) {
@@ -76,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
7676
$errors[] = RuleErrorBuilder::message(sprintf(
7777
'@coversDefaultClass references an invalid class %s.',
7878
$className
79-
))->build();
79+
))->identifier('phpunit.coversClass')->build();
8080
}
8181
}
8282

src/Rules/PHPUnit/ClassMethodCoversExistsRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ public function processNode(Node $node, Scope $scope): array
9494
$errors[] = RuleErrorBuilder::message(sprintf(
9595
'@coversDefaultClass defined on class method %s.',
9696
$node->name
97-
))->build();
97+
))->identifier('phpunit.covers')->build();
9898
}
9999

100100
foreach ($methodCovers as $covers) {
101101
if (in_array((string) $covers->value, $classCoversStrings, true)) {
102102
$errors[] = RuleErrorBuilder::message(sprintf(
103103
'Class already @covers %s so the method @covers is redundant.',
104104
$covers->value
105-
))->build();
105+
))->identifier('phpunit.coversDuplicate')->build();
106106
}
107107

108108
$errors = array_merge(

src/Rules/PHPUnit/CoversHelper.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
88
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
99
use PHPStan\Reflection\ReflectionProvider;
10-
use PHPStan\Rules\RuleError;
10+
use PHPStan\Rules\IdentifierRuleError;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use function array_merge;
1313
use function explode;
@@ -61,7 +61,7 @@ public function getCoverAnnotations(?ResolvedPhpDocBlock $phpDoc): array
6161
}
6262

6363
/**
64-
* @return RuleError[] errors
64+
* @return list<IdentifierRuleError> errors
6565
*/
6666
public function processCovers(
6767
Node $node,
@@ -73,7 +73,9 @@ public function processCovers(
7373
$covers = (string) $phpDocTag->value;
7474

7575
if ($covers === '') {
76-
$errors[] = RuleErrorBuilder::message('@covers value does not specify anything.')->build();
76+
$errors[] = RuleErrorBuilder::message('@covers value does not specify anything.')
77+
->identifier('phpunit.covers')
78+
->build();
7779

7880
return $errors;
7981
}
@@ -99,14 +101,14 @@ public function processCovers(
99101
$errors[] = RuleErrorBuilder::message(sprintf(
100102
'@covers value %s references an interface.',
101103
$fullName
102-
))->build();
104+
))->identifier('phpunit.coversInterface')->build();
103105
}
104106

105107
if (isset($method) && $method !== '' && !$class->hasMethod($method)) {
106108
$errors[] = RuleErrorBuilder::message(sprintf(
107109
'@covers value %s references an invalid method.',
108110
$fullName
109-
))->build();
111+
))->identifier('phpunit.coversMethod')->build();
110112
}
111113
} elseif (isset($method) && $this->reflectionProvider->hasFunction(new Name($method, []), null)) {
112114
return $errors;
@@ -117,7 +119,7 @@ public function processCovers(
117119
'@covers value %s references an invalid %s.',
118120
$fullName,
119121
$isMethod ? 'method' : 'class or function'
120-
));
122+
))->identifier(sprintf('phpunit.covers%s', $isMethod ? 'Method' : ''));
121123

122124
if (strpos($className, '\\') === false) {
123125
$error->tip('The @covers annotation requires a fully qualified name.');

src/Rules/PHPUnit/DataProviderHelper.php

+32-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPStan\Reflection\ClassReflection;
1414
use PHPStan\Reflection\MissingMethodFromReflectionException;
1515
use PHPStan\Reflection\ReflectionProvider;
16-
use PHPStan\Rules\RuleError;
16+
use PHPStan\Rules\IdentifierRuleError;
1717
use PHPStan\Rules\RuleErrorBuilder;
1818
use PHPStan\Type\FileTypeMapper;
1919
use function array_merge;
@@ -130,7 +130,7 @@ private function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array
130130
}
131131

132132
/**
133-
* @return RuleError[] errors
133+
* @return list<IdentifierRuleError> errors
134134
*/
135135
public function processDataProvider(
136136
string $dataProviderValue,
@@ -142,23 +142,29 @@ public function processDataProvider(
142142
): array
143143
{
144144
if ($classReflection === null) {
145-
$error = RuleErrorBuilder::message(sprintf(
146-
'@dataProvider %s related class not found.',
147-
$dataProviderValue
148-
))->line($lineNumber)->build();
149-
150-
return [$error];
145+
return [
146+
RuleErrorBuilder::message(sprintf(
147+
'@dataProvider %s related class not found.',
148+
$dataProviderValue
149+
))
150+
->line($lineNumber)
151+
->identifier('phpunit.dataProviderClass')
152+
->build(),
153+
];
151154
}
152155

153156
try {
154157
$dataProviderMethodReflection = $classReflection->getNativeMethod($methodName);
155158
} catch (MissingMethodFromReflectionException $missingMethodFromReflectionException) {
156-
$error = RuleErrorBuilder::message(sprintf(
157-
'@dataProvider %s related method not found.',
158-
$dataProviderValue
159-
))->line($lineNumber)->build();
160-
161-
return [$error];
159+
return [
160+
RuleErrorBuilder::message(sprintf(
161+
'@dataProvider %s related method not found.',
162+
$dataProviderValue
163+
))
164+
->line($lineNumber)
165+
->identifier('phpunit.dataProviderMethod')
166+
->build(),
167+
];
162168
}
163169

164170
$errors = [];
@@ -168,21 +174,30 @@ public function processDataProvider(
168174
'@dataProvider %s related method is used with incorrect case: %s.',
169175
$dataProviderValue,
170176
$dataProviderMethodReflection->getName()
171-
))->line($lineNumber)->build();
177+
))
178+
->line($lineNumber)
179+
->identifier('method.nameCase')
180+
->build();
172181
}
173182

174183
if (!$dataProviderMethodReflection->isPublic()) {
175184
$errors[] = RuleErrorBuilder::message(sprintf(
176185
'@dataProvider %s related method must be public.',
177186
$dataProviderValue
178-
))->line($lineNumber)->build();
187+
))
188+
->line($lineNumber)
189+
->identifier('phpunit.dataProviderPublic')
190+
->build();
179191
}
180192

181193
if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
182194
$errors[] = RuleErrorBuilder::message(sprintf(
183195
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
184196
$dataProviderValue
185-
))->line($lineNumber)->build();
197+
))
198+
->line($lineNumber)
199+
->identifier('phpunit.dataProviderStatic')
200+
->build();
186201
}
187202

188203
return $errors;

src/Rules/PHPUnit/MockMethodCallRule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
6565
'Trying to mock an undefined method %s() on class %s.',
6666
$method,
6767
implode('&', $mockClasses)
68-
))->build();
68+
))->identifier('phpunit.mockMethod')->build();
6969
continue;
7070
}
7171

@@ -83,7 +83,7 @@ public function processNode(Node $node, Scope $scope): array
8383
'Trying to mock an undefined method %s() on class %s.',
8484
$method,
8585
implode('|', $classNames)
86-
))->build();
86+
))->identifier('phpunit.mockMethod')->build();
8787
}
8888

8989
return $errors;

src/Rules/PHPUnit/ShouldCallParentMethodsRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function processNode(Node $node, Scope $scope): array
5757
return [
5858
RuleErrorBuilder::message(
5959
sprintf('Missing call to parent::%s() method.', $methodName)
60-
)->build(),
60+
)->identifier('phpunit.callParent')->build(),
6161
];
6262
}
6363

0 commit comments

Comments
 (0)