Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish circular type aliases from invalid type definitions #678

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Rules/Classes/LocalTypeAliasesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\CircularTypeAliasErrorType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -137,12 +138,18 @@ public function processNode(Node $node, Scope $scope): array
return $type;
}

if ($type instanceof ErrorType) {
if ($type instanceof CircularTypeAliasErrorType) {
$errors[] = RuleErrorBuilder::message(sprintf('Circular definition detected in type alias %s.', $aliasName))->build();
$foundError = true;
return $type;
}

if ($type instanceof ErrorType) {
$errors[] = RuleErrorBuilder::message(sprintf('Invalid type definition detected in type alias %s.', $aliasName))->build();
$foundError = true;
return $type;
}

return $traverse($type);
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/Type/CircularTypeAliasErrorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

/** @api */
class CircularTypeAliasErrorType extends ErrorType
{

}
2 changes: 1 addition & 1 deletion src/Type/TypeAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(
public static function invalid(): self
{
$self = new self(new IdentifierTypeNode('*ERROR*'), new NameScope(null, []));
$self->resolvedType = new ErrorType();
$self->resolvedType = new CircularTypeAliasErrorType();
return $self;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/TypeAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function resolveLocalTypeAlias(string $aliasName, NameScope $nameScope):
$unresolvedAlias = $localTypeAliases[$aliasName];
$resolvedAliasType = $unresolvedAlias->resolve($this->typeNodeResolver);
} catch (\PHPStan\Type\CircularTypeAliasDefinitionException $e) {
$resolvedAliasType = new ErrorType();
$resolvedAliasType = new CircularTypeAliasErrorType();
}

$this->resolvedLocalTypeAliases[$aliasNameInClassScope] = $resolvedAliasType;
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Classes/LocalTypeAliasesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function testRule(): void
'Circular definition detected in type alias CircularTypeAliasImport1.',
47,
],
[
'Invalid type definition detected in type alias InvalidTypeAlias.',
62,
],
]);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Classes/data/local-type-aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ class Qux
class Generic
{
}

/**
* @phpstan-type InvalidTypeAlias invalid-type-definition
*/
class Invalid
{
}