Skip to content

Commit 25aeb12

Browse files
author
Florian Krämer
committed
Fixing PHPStan errors
1 parent e99c8c4 commit 25aeb12

13 files changed

+120
-61
lines changed

src/Architecture/CatchExceptionOfTypeNotAllowedRule.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public function getNodeType(): string
4545

4646
public function processNode(Node $node, Scope $scope): array
4747
{
48-
if (!$node instanceof Catch_) {
49-
return [];
50-
}
51-
5248
$errors = [];
5349

5450
foreach ($node->types as $type) {

src/Architecture/ClassMustBeFinalRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getNodeType(): string
4545
*/
4646
public function processNode(Node $node, Scope $scope): array
4747
{
48-
if (!$node instanceof Class_ || !isset($node->name)) {
48+
if (!isset($node->name)) {
4949
return [];
5050
}
5151

@@ -67,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
6767
return [];
6868
}
6969

70-
private function buildRuleError(string $fullClassName)
70+
private function buildRuleError(string $fullClassName): \PHPStan\Rules\RuleError
7171
{
7272
return RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $fullClassName))
7373
->identifier(self::IDENTIFIER)

src/Architecture/ClassMustBeReadonlyRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getNodeType(): string
4444

4545
public function processNode(Node $node, Scope $scope): array
4646
{
47-
if (!$node instanceof Class_ || !isset($node->name)) {
47+
if (!isset($node->name)) {
4848
return [];
4949
}
5050

src/Architecture/ClassMustHaveSpecificationDocblockRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private function extractDocblockLines(string $text): array
183183
}, $lines);
184184

185185
// Remove empty first and last lines that might be from the /** */ delimiters
186-
if ($lines !== [] && trim($lines[0]) === '') {
186+
if (count($lines) > 0 && trim($lines[0]) === '') {
187187
array_shift($lines);
188188
}
189189
$lastIndex = count($lines) - 1;

src/Architecture/ClassnameMustMatchPatternRule.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ public function getNodeType(): string
4141

4242
public function processNode(Node $node, Scope $scope): array
4343
{
44-
if (!$node instanceof Namespace_) {
45-
return [];
46-
}
47-
4844
$namespaceName = $node->name ? $node->name->toString() : '';
4945
$errors = [];
5046

@@ -65,7 +61,7 @@ public function processNode(Node $node, Scope $scope): array
6561
}
6662
}
6763

68-
return $errors;
64+
return array_values($errors);
6965
}
7066

7167
private function namespaceMatches(string $namespace, string $namespacePattern): bool
@@ -106,16 +102,16 @@ private function buildErrorMessage(string $fqcn, string $namespace, array $patte
106102
/**
107103
* @param string $namespaceName
108104
* @param string $className
109-
* @param $classPatterns
105+
* @param string[] $classPatterns
110106
* @param Class_ $stmt
111-
* @param array $errors
112-
* @return array
107+
* @param array<\PHPStan\Rules\RuleError> $errors
108+
* @return array<\PHPStan\Rules\RuleError>
113109
* @throws ShouldNotHappenException
114110
*/
115111
public function buildRuleError(
116112
string $namespaceName,
117113
string $className,
118-
$classPatterns,
114+
array $classPatterns,
119115
Class_ $stmt,
120116
array $errors
121117
): array {

src/Architecture/ForbiddenNamespacesRule.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public function getNodeType(): string
5151
*/
5252
public function processNode(Node $node, Scope $scope): array
5353
{
54-
if (!$node instanceof Namespace_) {
55-
return [];
56-
}
57-
5854
$namespaceName = $node->name ? $node->name->toString() : '';
5955

6056
// Empty namespace is allowed (global namespace)

src/Architecture/MethodMustReturnTypeRule.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* - Check if the types of the parameters match the expected types.
2222
* - If an object type is expected, it can match a specific class or a pattern.
2323
* - Supports union types with "oneOf" (one type must match) and "allOf" (all types must match).
24+
*
25+
* @implements Rule<Class_>
2426
*/
2527
class MethodMustReturnTypeRule implements Rule
2628
{
@@ -60,7 +62,7 @@ public function getNodeType(): string
6062
/**
6163
* @param Class_ $node
6264
* @param Scope $scope
63-
* @return array
65+
* @return array<\PHPStan\Rules\RuleError>
6466
*/
6567
public function processNode(Node $node, Scope $scope): array
6668
{
@@ -143,8 +145,8 @@ public function processNode(Node $node, Scope $scope): array
143145
/**
144146
* Normalize configuration with defaults
145147
*
146-
* @param array $config
147-
* @return array
148+
* @param array<string, mixed> $config
149+
* @return array<string, mixed>
148150
*/
149151
private function normalizeConfig(array $config): array
150152
{
@@ -163,12 +165,15 @@ private function normalizeConfig(array $config): array
163165
return $normalized;
164166
}
165167

168+
/**
169+
* @param array<string, mixed> $patternConfig
170+
*/
166171
private function shouldErrorOnVoid(array $patternConfig, ?string $returnType): bool
167172
{
168173
return $patternConfig['void'] && $returnType !== 'void';
169174
}
170175

171-
private function buildVoidError(string $fullName, int $line)
176+
private function buildVoidError(string $fullName, int $line): \PHPStan\Rules\RuleError
172177
{
173178
return RuleErrorBuilder::message(
174179
sprintf(
@@ -186,6 +191,9 @@ private function shouldErrorOnMissingReturnType(?string $returnType): bool
186191
return $returnType === null;
187192
}
188193

194+
/**
195+
* @param array<string, mixed> $patternConfig
196+
*/
189197
private function getExpectedTypeDescription(array $patternConfig): string
190198
{
191199
if (isset($patternConfig['oneOf'])) {
@@ -197,7 +205,7 @@ private function getExpectedTypeDescription(array $patternConfig): string
197205
return $patternConfig['type'] ?? 'void';
198206
}
199207

200-
private function buildMissingReturnTypeError(string $fullName, string $expectedType, int $line)
208+
private function buildMissingReturnTypeError(string $fullName, string $expectedType, int $line): \PHPStan\Rules\RuleError
201209
{
202210
return RuleErrorBuilder::message(
203211
sprintf(
@@ -211,12 +219,15 @@ private function buildMissingReturnTypeError(string $fullName, string $expectedT
211219
->build();
212220
}
213221

222+
/**
223+
* @param array<string, mixed> $patternConfig
224+
*/
214225
private function shouldErrorOnNullability(array $patternConfig, bool $isNullable): bool
215226
{
216227
return $patternConfig['nullable'] !== $isNullable;
217228
}
218229

219-
private function buildNullabilityError(string $fullName, bool $expectedNullable, int $line)
230+
private function buildNullabilityError(string $fullName, bool $expectedNullable, int $line): \PHPStan\Rules\RuleError
220231
{
221232
return RuleErrorBuilder::message(
222233
sprintf(
@@ -230,6 +241,9 @@ private function buildNullabilityError(string $fullName, bool $expectedNullable,
230241
->build();
231242
}
232243

244+
/**
245+
* @param array<string> $expectedTypes
246+
*/
233247
private function shouldErrorOnOneOf(array $expectedTypes, ?string $returnType): bool
234248
{
235249
if ($returnType === null) {
@@ -245,7 +259,10 @@ private function shouldErrorOnOneOf(array $expectedTypes, ?string $returnType):
245259
return true;
246260
}
247261

248-
private function buildOneOfError(string $fullName, array $expectedTypes, ?string $actualType, int $line)
262+
/**
263+
* @param array<string> $expectedTypes
264+
*/
265+
private function buildOneOfError(string $fullName, array $expectedTypes, ?string $actualType, int $line): \PHPStan\Rules\RuleError
249266
{
250267
return RuleErrorBuilder::message(
251268
sprintf(
@@ -260,6 +277,9 @@ private function buildOneOfError(string $fullName, array $expectedTypes, ?string
260277
->build();
261278
}
262279

280+
/**
281+
* @param array<string> $expectedTypes
282+
*/
263283
private function shouldErrorOnAllOf(array $expectedTypes, ?string $returnType): bool
264284
{
265285
if ($returnType === null) {
@@ -286,7 +306,10 @@ private function shouldErrorOnAllOf(array $expectedTypes, ?string $returnType):
286306
return false;
287307
}
288308

289-
private function buildAllOfError(string $fullName, array $expectedTypes, ?string $actualType, int $line)
309+
/**
310+
* @param array<string> $expectedTypes
311+
*/
312+
private function buildAllOfError(string $fullName, array $expectedTypes, ?string $actualType, int $line): \PHPStan\Rules\RuleError
290313
{
291314
return RuleErrorBuilder::message(
292315
sprintf(
@@ -301,6 +324,9 @@ private function buildAllOfError(string $fullName, array $expectedTypes, ?string
301324
->build();
302325
}
303326

327+
/**
328+
* @return array<string>
329+
*/
304330
private function parseUnionType(?string $type): array
305331
{
306332
if ($type === null) {
@@ -354,13 +380,16 @@ private function isTypeMatch(?string $actual, string $expected): bool
354380
return false;
355381
}
356382

383+
/**
384+
* @param array<string, mixed> $patternConfig
385+
*/
357386
private function shouldErrorOnObjectTypePattern(array $patternConfig, string $objectType): bool
358387
{
359388
return $patternConfig['objectTypePattern'] !== null &&
360389
!preg_match($patternConfig['objectTypePattern'], $objectType);
361390
}
362391

363-
private function buildObjectTypePatternError(string $fullName, string $pattern, string $objectType, int $line)
392+
private function buildObjectTypePatternError(string $fullName, string $pattern, string $objectType, int $line): \PHPStan\Rules\RuleError
364393
{
365394
return RuleErrorBuilder::message(
366395
sprintf(
@@ -375,7 +404,7 @@ private function buildObjectTypePatternError(string $fullName, string $pattern,
375404
->build();
376405
}
377406

378-
private function buildObjectTypeError(string $fullName, int $line)
407+
private function buildObjectTypeError(string $fullName, int $line): \PHPStan\Rules\RuleError
379408
{
380409
return RuleErrorBuilder::message(
381410
sprintf(
@@ -393,7 +422,7 @@ private function shouldErrorOnTypeMismatch(?string $returnType, string $expected
393422
return !$this->isTypeMatch($returnType, $expectedType);
394423
}
395424

396-
private function buildTypeMismatchError(string $fullName, string $expectedType, ?string $actualType, int $line)
425+
private function buildTypeMismatchError(string $fullName, string $expectedType, ?string $actualType, int $line): \PHPStan\Rules\RuleError
397426
{
398427
return RuleErrorBuilder::message(
399428
sprintf(
@@ -424,7 +453,7 @@ private function getTypeAsString(mixed $type): ?string
424453
};
425454
}
426455

427-
private function isNullableType($type): bool
456+
private function isNullableType(mixed $type): bool
428457
{
429458
return $type instanceof NullableType;
430459
}

0 commit comments

Comments
 (0)