Skip to content

Commit 804902a

Browse files
committed
Modernize rules with RuleErrorBuilder
1 parent 3d9b1e2 commit 804902a

8 files changed

+57
-32
lines changed

src/Rules/Doctrine/ORM/DqlRule.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
1112
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1213
use PHPStan\Type\ObjectType;
1314
use PHPStan\Type\TypeUtils;
@@ -76,7 +77,8 @@ public function processNode(Node $node, Scope $scope): array
7677
try {
7778
$query->getAST();
7879
} catch (QueryException $e) {
79-
$messages[] = sprintf('DQL: %s', $e->getMessage());
80+
$messages[] = RuleErrorBuilder::message(sprintf('DQL: %s', $e->getMessage()))
81+
->build();
8082
} catch (AssertionError $e) {
8183
continue;
8284
}

src/Rules/Doctrine/ORM/EntityColumnRule.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Node\ClassPropertyNode;
88
use PHPStan\Reflection\ReflectionProvider;
99
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
1011
use PHPStan\Type\ArrayType;
1112
use PHPStan\Type\Doctrine\DescriptorNotRegisteredException;
1213
use PHPStan\Type\Doctrine\DescriptorRegistry;
@@ -100,12 +101,14 @@ public function processNode(Node $node, Scope $scope): array
100101
try {
101102
$descriptor = $this->descriptorRegistry->get($fieldMapping['type']);
102103
} catch (DescriptorNotRegisteredException $e) {
103-
return $this->reportUnknownTypes ? [sprintf(
104-
'Property %s::$%s: Doctrine type "%s" does not have any registered descriptor.',
105-
$className,
106-
$propertyName,
107-
$fieldMapping['type']
108-
)] : [];
104+
return $this->reportUnknownTypes ? [
105+
RuleErrorBuilder::message(sprintf(
106+
'Property %s::$%s: Doctrine type "%s" does not have any registered descriptor.',
107+
$className,
108+
$propertyName,
109+
$fieldMapping['type']
110+
))->build(),
111+
] : [];
109112
}
110113

111114
$writableToPropertyType = $descriptor->getWritableToPropertyType();
@@ -118,14 +121,14 @@ public function processNode(Node $node, Scope $scope): array
118121
$backedEnumType = $enumReflection->getBackedEnumType();
119122
if ($backedEnumType !== null) {
120123
if (!$backedEnumType->equals($writableToDatabaseType) || !$backedEnumType->equals($writableToPropertyType)) {
121-
$errors[] = sprintf(
124+
$errors[] = RuleErrorBuilder::message(sprintf(
122125
'Property %s::$%s type mapping mismatch: backing type %s of enum %s does not match database type %s.',
123126
$className,
124127
$propertyName,
125128
$backedEnumType->describe(VerbosityLevel::typeOnly()),
126129
$enumReflection->getDisplayName(),
127130
$writableToDatabaseType->describe(VerbosityLevel::typeOnly())
128-
);
131+
))->build();
129132
}
130133
}
131134
}
@@ -170,13 +173,13 @@ public function processNode(Node $node, Scope $scope): array
170173
}) : $propertyType;
171174

172175
if (!$propertyTransformedType->isSuperTypeOf($writableToPropertyType)->yes()) {
173-
$errors[] = sprintf(
176+
$errors[] = RuleErrorBuilder::message(sprintf(
174177
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
175178
$className,
176179
$propertyName,
177180
$writableToPropertyType->describe(VerbosityLevel::getRecommendedLevelByType($propertyTransformedType, $writableToPropertyType)),
178181
$propertyType->describe(VerbosityLevel::getRecommendedLevelByType($propertyTransformedType, $writableToPropertyType))
179-
);
182+
))->build();
180183
}
181184

182185
if (
@@ -186,13 +189,13 @@ public function processNode(Node $node, Scope $scope): array
186189
: $propertyType
187190
)->yes()
188191
) {
189-
$errors[] = sprintf(
192+
$errors[] = RuleErrorBuilder::message(sprintf(
190193
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
191194
$className,
192195
$propertyName,
193196
$propertyTransformedType->describe(VerbosityLevel::getRecommendedLevelByType($writableToDatabaseType, $propertyType)),
194197
$writableToDatabaseType->describe(VerbosityLevel::getRecommendedLevelByType($writableToDatabaseType, $propertyType))
195-
);
198+
))->build();
196199
}
197200
return $errors;
198201
}

src/Rules/Doctrine/ORM/EntityConstructorNotFinalRule.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ public function processNode(Node $node, Scope $scope): array
5454
return [];
5555
}
5656

57-
return [RuleErrorBuilder::message(sprintf(
58-
'Constructor of class %s is final which can cause problems with proxies.',
59-
$classReflection->getDisplayName()
60-
))->build()];
57+
return [
58+
RuleErrorBuilder::message(sprintf(
59+
'Constructor of class %s is final which can cause problems with proxies.',
60+
$classReflection->getDisplayName()
61+
))->build(),
62+
];
6163
}
6264

6365
}

src/Rules/Doctrine/ORM/EntityMappingExceptionRule.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Node\InClassNode;
1010
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
1112
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1213
use ReflectionException;
1314

@@ -56,7 +57,11 @@ public function processNode(Node $node, Scope $scope): array
5657
try {
5758
$objectManager->getClassMetadata($className);
5859
} catch (\Doctrine\Persistence\Mapping\MappingException | MappingException | AnnotationException $e) {
59-
return [$e->getMessage()];
60+
return [
61+
RuleErrorBuilder::message($e->getMessage())
62+
->nonIgnorable()
63+
->build(),
64+
];
6065
}
6166

6267
return [];

src/Rules/Doctrine/ORM/EntityNotFinalRule.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassNode;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1112
use function sprintf;
@@ -48,10 +49,12 @@ public function processNode(Node $node, Scope $scope): array
4849
return [];
4950
}
5051

51-
return [sprintf(
52-
'Entity class %s is final which can cause problems with proxies.',
53-
$classReflection->getDisplayName()
54-
)];
52+
return [
53+
RuleErrorBuilder::message(sprintf(
54+
'Entity class %s is final which can cause problems with proxies.',
55+
$classReflection->getDisplayName()
56+
))->build(),
57+
];
5558
}
5659

5760
}

src/Rules/Doctrine/ORM/EntityRelationRule.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\ClassPropertyNode;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1011
use PHPStan\Type\ErrorType;
1112
use PHPStan\Type\IterableType;
@@ -128,13 +129,13 @@ public function processNode(Node $node, Scope $scope): array
128129
);
129130
}
130131
if (!$propertyTypeToCheckAgainst->isSuperTypeOf($columnType)->yes()) {
131-
$errors[] = sprintf(
132+
$errors[] = RuleErrorBuilder::message(sprintf(
132133
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
133134
$className,
134135
$propertyName,
135136
$columnType->describe(VerbosityLevel::typeOnly()),
136137
$propertyType->describe(VerbosityLevel::typeOnly())
137-
);
138+
))->build();
138139
}
139140
if (
140141
!$columnType->isSuperTypeOf(
@@ -143,13 +144,13 @@ public function processNode(Node $node, Scope $scope): array
143144
: $propertyType
144145
)->yes()
145146
) {
146-
$errors[] = sprintf(
147+
$errors[] = RuleErrorBuilder::message(sprintf(
147148
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
148149
$className,
149150
$propertyName,
150151
$propertyType->describe(VerbosityLevel::typeOnly()),
151152
$columnType->describe(VerbosityLevel::typeOnly())
152-
);
153+
))->build();
153154
}
154155
}
155156

src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\MethodCall;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Rules\Rule;
12+
use PHPStan\Rules\RuleErrorBuilder;
1213
use PHPStan\Type\Doctrine\DoctrineTypeUtils;
1314
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1415
use PHPStan\Type\ObjectType;
@@ -62,7 +63,8 @@ public function processNode(Node $node, Scope $scope): array
6263
&& (new ObjectType('Doctrine\ORM\QueryBuilder'))->isSuperTypeOf($calledOnType)->yes()
6364
) {
6465
return [
65-
'Could not analyse QueryBuilder with unknown beginning.',
66+
RuleErrorBuilder::message('Could not analyse QueryBuilder with unknown beginning.')
67+
->build(),
6668
];
6769
}
6870
return [];
@@ -71,14 +73,19 @@ public function processNode(Node $node, Scope $scope): array
7173
try {
7274
$dqlType = $scope->getType(new MethodCall($node, new Node\Identifier('getDQL'), []));
7375
} catch (Throwable $e) {
74-
return [sprintf('Internal error: %s', $e->getMessage())];
76+
return [
77+
RuleErrorBuilder::message(sprintf('Internal error: %s', $e->getMessage()))
78+
->nonIgnorable()
79+
->build(),
80+
];
7581
}
7682

7783
$dqls = TypeUtils::getConstantStrings($dqlType);
7884
if (count($dqls) === 0) {
7985
if ($this->reportDynamicQueryBuilders) {
8086
return [
81-
'Could not analyse QueryBuilder with dynamic arguments.',
87+
RuleErrorBuilder::message('Could not analyse QueryBuilder with dynamic arguments.')
88+
->build(),
8289
];
8390
}
8491
return [];
@@ -107,7 +114,8 @@ public function processNode(Node $node, Scope $scope): array
107114
$message .= sprintf("\nDQL: %s", $dql->getValue());
108115
}
109116

110-
$messages[] = $message;
117+
$messages[] = RuleErrorBuilder::message($message)
118+
->build();
111119
} catch (AssertionError $e) {
112120
continue;
113121
}

src/Rules/Doctrine/ORM/RepositoryMethodCallRule.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1011
use PHPStan\Type\VerbosityLevel;
1112
use function count;
@@ -76,13 +77,13 @@ public function processNode(Node $node, Scope $scope): array
7677
continue;
7778
}
7879

79-
$messages[] = sprintf(
80+
$messages[] = RuleErrorBuilder::message(sprintf(
8081
'Call to method %s::%s() - entity %s does not have a field named $%s.',
8182
$calledOnType->describe(VerbosityLevel::typeOnly()),
8283
$methodName,
8384
$entityClassNames[0],
8485
$fieldName->getValue()
85-
);
86+
))->build();
8687
}
8788
}
8889
}

0 commit comments

Comments
 (0)