Skip to content

Commit

Permalink
Bleeding edge - call GenericObjectTypeCheck from LocalTypeAliasesCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 24, 2024
1 parent 5f7d12b commit 5a2d441
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Rules/Classes/LocalTypeAliasesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Analyser\NameScope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\ClassNameNodePair;
use PHPStan\Rules\Generics\GenericObjectTypeCheck;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
Expand Down Expand Up @@ -39,6 +41,7 @@ public function __construct(
private MissingTypehintCheck $missingTypehintCheck,
private ClassNameCheck $classCheck,
private UnresolvableTypeHelper $unresolvableTypeHelper,
private GenericObjectTypeCheck $genericObjectTypeCheck,
private bool $checkMissingTypehints,
private bool $checkClassCaseSensitivity,
private bool $absentTypeChecks,
Expand Down Expand Up @@ -256,6 +259,35 @@ public function check(ClassReflection $reflection, ClassLike $node): array
->identifier('typeAlias.unresolvableType')
->build();
}

$escapedTypeAlias = SprintfHelper::escapeFormatString($aliasName);
$errors = array_merge($errors, $this->genericObjectTypeCheck->check(
$resolvedType,
sprintf(
'Type alias %s contains generic type %%s but %%s %%s is not generic.',
$escapedTypeAlias,
),
sprintf(
'Generic type %%s in type alias %s does not specify all template types of %%s %%s: %%s',
$escapedTypeAlias,
),
sprintf(
'Generic type %%s in type alias %s specifies %%d template types, but %%s %%s supports only %%d: %%s',
$escapedTypeAlias,
),
sprintf(
'Type %%s in generic type %%s in type alias %s is not subtype of template type %%s of %%s %%s.',
$escapedTypeAlias,
),
sprintf(
'Call-site variance of %%s in generic type %%s in type alias %s is in conflict with %%s template type %%s of %%s %%s.',
$escapedTypeAlias,
),
sprintf(
'Call-site variance of %%s in generic type %%s in type alias %s is redundant, template type %%s of %%s %%s has the same variance.',
$escapedTypeAlias,
),
));
}

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 @@ -6,6 +6,7 @@
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\Generics\GenericObjectTypeCheck;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -33,6 +34,7 @@ protected function getRule(): Rule
new ClassForbiddenNameCheck(self::getContainer()),
),
new UnresolvableTypeHelper(),
new GenericObjectTypeCheck(),
true,
true,
true,
Expand Down Expand Up @@ -137,6 +139,10 @@ public function testRule(): void
'Type alias A contains unresolvable type.',
95,
],
[
'Type alias A contains generic type Exception<int> but class Exception is not generic.',
103,
],
]);
}

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 @@ -6,6 +6,7 @@
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\Generics\GenericObjectTypeCheck;
use PHPStan\Rules\MissingTypehintCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
use PHPStan\Rules\Rule;
Expand All @@ -32,6 +33,7 @@ protected function getRule(): Rule
new ClassForbiddenNameCheck(self::getContainer()),
),
new UnresolvableTypeHelper(),
new GenericObjectTypeCheck(),
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 @@ -96,3 +96,11 @@ class UnresolvableExample
{

}

/**
* @phpstan-type A = \Exception<int>
*/
class GenericsCheck
{

}

0 comments on commit 5a2d441

Please sign in to comment.