diff --git a/src/Rules/Classes/LocalTypeAliasesCheck.php b/src/Rules/Classes/LocalTypeAliasesCheck.php index f71222e44..cd6362f8d 100644 --- a/src/Rules/Classes/LocalTypeAliasesCheck.php +++ b/src/Rules/Classes/LocalTypeAliasesCheck.php @@ -12,6 +12,7 @@ use PHPStan\Rules\ClassNameNodePair; use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\MissingTypehintCheck; +use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\CircularTypeAliasErrorType; use PHPStan\Type\ErrorType; @@ -37,6 +38,7 @@ public function __construct( private TypeNodeResolver $typeNodeResolver, private MissingTypehintCheck $missingTypehintCheck, private ClassNameCheck $classCheck, + private UnresolvableTypeHelper $unresolvableTypeHelper, private bool $checkMissingTypehints, private bool $checkClassCaseSensitivity, private bool $absentTypeChecks, @@ -248,6 +250,12 @@ public function check(ClassReflection $reflection, ClassLike $node): array ); } } + + if ($this->unresolvableTypeHelper->containsUnresolvableType($resolvedType)) { + $errors[] = RuleErrorBuilder::message(sprintf('Type alias %s contains unresolvable type.', $aliasName)) + ->identifier('typeAlias.unresolvableType') + ->build(); + } } return $errors; diff --git a/tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php b/tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php index 7cdffa770..089014a7a 100644 --- a/tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php +++ b/tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php @@ -7,6 +7,7 @@ use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; use PHPStan\Rules\MissingTypehintCheck; +use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; use const PHP_VERSION_ID; @@ -31,6 +32,7 @@ protected function getRule(): Rule new ClassCaseSensitivityCheck($reflectionProvider, true), new ClassForbiddenNameCheck(self::getContainer()), ), + new UnresolvableTypeHelper(), true, true, true, @@ -131,6 +133,10 @@ public function testRule(): void 'Class LocalTypeAliases\Foo referenced with incorrect case: LocalTypeAliases\fOO.', 87, ], + [ + 'Type alias A contains unresolvable type.', + 95, + ], ]); } diff --git a/tests/PHPStan/Rules/Classes/LocalTypeTraitAliasesRuleTest.php b/tests/PHPStan/Rules/Classes/LocalTypeTraitAliasesRuleTest.php index 7106d6599..1f53ff745 100644 --- a/tests/PHPStan/Rules/Classes/LocalTypeTraitAliasesRuleTest.php +++ b/tests/PHPStan/Rules/Classes/LocalTypeTraitAliasesRuleTest.php @@ -7,6 +7,7 @@ use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; use PHPStan\Rules\MissingTypehintCheck; +use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; @@ -30,6 +31,7 @@ protected function getRule(): Rule new ClassCaseSensitivityCheck($reflectionProvider, true), new ClassForbiddenNameCheck(self::getContainer()), ), + new UnresolvableTypeHelper(), true, true, true, diff --git a/tests/PHPStan/Rules/Classes/data/local-type-aliases.php b/tests/PHPStan/Rules/Classes/data/local-type-aliases.php index ba4c47f57..5aa52e908 100644 --- a/tests/PHPStan/Rules/Classes/data/local-type-aliases.php +++ b/tests/PHPStan/Rules/Classes/data/local-type-aliases.php @@ -88,3 +88,11 @@ class NonexistentClasses { } + +/** + * @phpstan-type A = string&int + */ +class UnresolvableExample +{ + +}