Skip to content

Commit

Permalink
Fix inline @var priority with prefixed PHPDoc tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 26, 2021
1 parent ec8912c commit 3186fff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public function __construct(TypeNodeResolver $typeNodeResolver, ConstExprNodeRes
*/
public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
{
foreach (['@phpstan-var', '@psalm-var', '@var'] as $tagName) {
$resolved = [];
$resolved = [];
$resolvedByTag = [];
foreach (['@var', '@psalm-var', '@phpstan-var'] as $tagName) {
$tagResolved = [];
foreach ($phpDocNode->getVarTagValues($tagName) as $tagValue) {
$type = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
if ($this->shouldSkipType($tagName, $type)) {
Expand All @@ -59,16 +61,23 @@ public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): ar
$variableName = substr($tagValue->variableName, 1);
$resolved[$variableName] = new VarTag($type);
} else {
$resolved[] = new VarTag($type);
$varTag = new VarTag($type);
$tagResolved[] = $varTag;
}
}

if (count($resolved) > 0) {
return $resolved;
if (count($tagResolved) === 0) {
continue;
}

$resolvedByTag[] = $tagResolved;
}

return [];
if (count($resolvedByTag) > 0) {
return array_reverse($resolvedByTag)[0];
}

return $resolved;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10928,6 +10928,11 @@ public function dataBug4587(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4587.php');
}

public function dataBug4606(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4606.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -11175,6 +11180,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataBugInstanceOfClassString
* @dataProvider dataBug4498
* @dataProvider dataBug4587
* @dataProvider dataBug4606
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4606.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug4606;

use function PHPStan\Analyser\assertType;

/**
* @var Foo $this
* @var array $assigned
* @phpstan-var list<array{\stdClass, int}> $assigned
*/

assertType(Foo::class, $this);
assertType('array<int, array(stdClass, int)>', $assigned);


/**
* @var array
* @phpstan-var array{\stdClass, int}
*/
$foo = doFoo();

assertType('array(stdClass, int)', $foo);

0 comments on commit 3186fff

Please sign in to comment.