Skip to content

Commit 5f7d12b

Browse files
committed
Bleeding edge - check unresolvable types in LocalTypeAliasesCheck
1 parent 82f7e14 commit 5f7d12b

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/Rules/Classes/LocalTypeAliasesCheck.php

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Rules\ClassNameNodePair;
1313
use PHPStan\Rules\IdentifierRuleError;
1414
use PHPStan\Rules\MissingTypehintCheck;
15+
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
1516
use PHPStan\Rules\RuleErrorBuilder;
1617
use PHPStan\Type\CircularTypeAliasErrorType;
1718
use PHPStan\Type\ErrorType;
@@ -37,6 +38,7 @@ public function __construct(
3738
private TypeNodeResolver $typeNodeResolver,
3839
private MissingTypehintCheck $missingTypehintCheck,
3940
private ClassNameCheck $classCheck,
41+
private UnresolvableTypeHelper $unresolvableTypeHelper,
4042
private bool $checkMissingTypehints,
4143
private bool $checkClassCaseSensitivity,
4244
private bool $absentTypeChecks,
@@ -248,6 +250,12 @@ public function check(ClassReflection $reflection, ClassLike $node): array
248250
);
249251
}
250252
}
253+
254+
if ($this->unresolvableTypeHelper->containsUnresolvableType($resolvedType)) {
255+
$errors[] = RuleErrorBuilder::message(sprintf('Type alias %s contains unresolvable type.', $aliasName))
256+
->identifier('typeAlias.unresolvableType')
257+
->build();
258+
}
251259
}
252260

253261
return $errors;

tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Rules\ClassForbiddenNameCheck;
88
use PHPStan\Rules\ClassNameCheck;
99
use PHPStan\Rules\MissingTypehintCheck;
10+
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
1011
use PHPStan\Rules\Rule;
1112
use PHPStan\Testing\RuleTestCase;
1213
use const PHP_VERSION_ID;
@@ -31,6 +32,7 @@ protected function getRule(): Rule
3132
new ClassCaseSensitivityCheck($reflectionProvider, true),
3233
new ClassForbiddenNameCheck(self::getContainer()),
3334
),
35+
new UnresolvableTypeHelper(),
3436
true,
3537
true,
3638
true,
@@ -131,6 +133,10 @@ public function testRule(): void
131133
'Class LocalTypeAliases\Foo referenced with incorrect case: LocalTypeAliases\fOO.',
132134
87,
133135
],
136+
[
137+
'Type alias A contains unresolvable type.',
138+
95,
139+
],
134140
]);
135141
}
136142

tests/PHPStan/Rules/Classes/LocalTypeTraitAliasesRuleTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Rules\ClassForbiddenNameCheck;
88
use PHPStan\Rules\ClassNameCheck;
99
use PHPStan\Rules\MissingTypehintCheck;
10+
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
1011
use PHPStan\Rules\Rule;
1112
use PHPStan\Testing\RuleTestCase;
1213

@@ -30,6 +31,7 @@ protected function getRule(): Rule
3031
new ClassCaseSensitivityCheck($reflectionProvider, true),
3132
new ClassForbiddenNameCheck(self::getContainer()),
3233
),
34+
new UnresolvableTypeHelper(),
3335
true,
3436
true,
3537
true,

tests/PHPStan/Rules/Classes/data/local-type-aliases.php

+8
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,11 @@ class NonexistentClasses
8888
{
8989

9090
}
91+
92+
/**
93+
* @phpstan-type A = string&int
94+
*/
95+
class UnresolvableExample
96+
{
97+
98+
}

0 commit comments

Comments
 (0)