Skip to content

Commit

Permalink
Bleeding edge - check unresolvable types in LocalTypeAliasesCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 24, 2024
1 parent 82f7e14 commit 5f7d12b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Rules/Classes/LocalTypeAliasesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@ protected function getRule(): Rule
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck(self::getContainer()),
),
new UnresolvableTypeHelper(),
true,
true,
true,
Expand Down Expand Up @@ -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,
],
]);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Classes/LocalTypeTraitAliasesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +31,7 @@ protected function getRule(): Rule
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck(self::getContainer()),
),
new UnresolvableTypeHelper(),
true,
true,
true,
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Classes/data/local-type-aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ class NonexistentClasses
{

}

/**
* @phpstan-type A = string&int
*/
class UnresolvableExample
{

}

0 comments on commit 5f7d12b

Please sign in to comment.