Skip to content

Commit

Permalink
Handle invalid type aliases better
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 4, 2023
1 parent 31b6296 commit fc5515a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use PHPStan\PhpDocParser\Ast\Type\InvalidTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
Expand Down Expand Up @@ -164,6 +165,8 @@ public function resolve(TypeNode $typeNode, NameScope $nameScope): Type
return $this->resolveConstTypeNode($typeNode, $nameScope);
} elseif ($typeNode instanceof OffsetAccessTypeNode) {
return $this->resolveOffsetAccessNode($typeNode, $nameScope);
} elseif ($typeNode instanceof InvalidTypeNode) {
return new MixedType(true);
}

return new ErrorType();
Expand Down
19 changes: 17 additions & 2 deletions src/Rules/PhpDoc/InvalidPhpDocTagValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\VirtualNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\InvalidTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
Expand Down Expand Up @@ -71,11 +73,24 @@ public function processNode(Node $node, Scope $scope): array

$errors = [];
foreach ($phpDocNode->getTags() as $phpDocTag) {
if (!($phpDocTag->value instanceof InvalidTagValueNode)) {
if (strpos($phpDocTag->name, '@psalm-') === 0) {
continue;
}

if (strpos($phpDocTag->name, '@psalm-') === 0) {
if ($phpDocTag->value instanceof TypeAliasTagValueNode) {
if (!$phpDocTag->value->type instanceof InvalidTypeNode) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf(
'PHPDoc tag %s %s has invalid value: %s',
$phpDocTag->name,
$phpDocTag->value->alias,
$phpDocTag->value->type->getException()->getMessage(),
))->build();

continue;
} elseif (!($phpDocTag->value instanceof InvalidTagValueNode)) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/PhpDoc/data/bug-8609-function.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9131.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/more-types.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/invalid-type-aliases.php');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/invalid-type-aliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace InvalidTypeAliases;

use function PHPStan\Testing\assertType;

/** @psalm-type MyObject = what{foo: 'bar'} */
class HelloWorld
{
/** @psalm-param MyObject $a */
public function acceptsAlias($a)
{
assertType('mixed', $a);
assertType('mixed', $this->returnsAlias());
}

/** @psalm-return MyObject */
public function returnsAlias()
{

}
}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Classes/data/local-type-aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ class Generic
class Invalid
{
}

/** @psalm-type MyObject = what{} */
class InvalidTypeDefinitionToIgnoreBecauseItsAParseErrorAlreadyReportedInInvalidPhpDocTagValueRule
{

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/InvalidPhpDocTagValueRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,15 @@ public function testBug4731WithoutFirstTag(): void
$this->analyse([__DIR__ . '/data/bug-4731-no-first-tag.php'], []);
}

public function testInvalidTypeInTypeAlias(): void
{
$this->checkAllInvalidPhpDocs = true;
$this->analyse([__DIR__ . '/data/invalid-type-type-alias.php'], [
[
'PHPDoc tag @phpstan-type InvalidFoo has invalid value: Unexpected token "{", expected TOKEN_PHPDOC_EOL at offset 65',
12,
],
]);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/invalid-type-type-alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace InvalidTypeInTypeAlias;

/**
* @phpstan-type Foo array{}
* @phpstan-type InvalidFoo what{}
*
* @psalm-type Foo array{}
* @psalm-type InvalidFoo what{}
*/
class Foo
{



}

0 comments on commit fc5515a

Please sign in to comment.