Skip to content

Commit 3186fff

Browse files
committed
Fix inline @var priority with prefixed PHPDoc tags
1 parent ec8912c commit 3186fff

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public function __construct(TypeNodeResolver $typeNodeResolver, ConstExprNodeRes
4848
*/
4949
public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
5050
{
51-
foreach (['@phpstan-var', '@psalm-var', '@var'] as $tagName) {
52-
$resolved = [];
51+
$resolved = [];
52+
$resolvedByTag = [];
53+
foreach (['@var', '@psalm-var', '@phpstan-var'] as $tagName) {
54+
$tagResolved = [];
5355
foreach ($phpDocNode->getVarTagValues($tagName) as $tagValue) {
5456
$type = $this->typeNodeResolver->resolve($tagValue->type, $nameScope);
5557
if ($this->shouldSkipType($tagName, $type)) {
@@ -59,16 +61,23 @@ public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): ar
5961
$variableName = substr($tagValue->variableName, 1);
6062
$resolved[$variableName] = new VarTag($type);
6163
} else {
62-
$resolved[] = new VarTag($type);
64+
$varTag = new VarTag($type);
65+
$tagResolved[] = $varTag;
6366
}
6467
}
6568

66-
if (count($resolved) > 0) {
67-
return $resolved;
69+
if (count($tagResolved) === 0) {
70+
continue;
6871
}
72+
73+
$resolvedByTag[] = $tagResolved;
6974
}
7075

71-
return [];
76+
if (count($resolvedByTag) > 0) {
77+
return array_reverse($resolvedByTag)[0];
78+
}
79+
80+
return $resolved;
7281
}
7382

7483
/**

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10928,6 +10928,11 @@ public function dataBug4587(): array
1092810928
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4587.php');
1092910929
}
1093010930

10931+
public function dataBug4606(): array
10932+
{
10933+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4606.php');
10934+
}
10935+
1093110936
/**
1093210937
* @param string $file
1093310938
* @return array<string, mixed[]>
@@ -11175,6 +11180,7 @@ private function gatherAssertTypes(string $file): array
1117511180
* @dataProvider dataBugInstanceOfClassString
1117611181
* @dataProvider dataBug4498
1117711182
* @dataProvider dataBug4587
11183+
* @dataProvider dataBug4606
1117811184
* @param string $assertType
1117911185
* @param string $file
1118011186
* @param mixed ...$args
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug4606;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
/**
8+
* @var Foo $this
9+
* @var array $assigned
10+
* @phpstan-var list<array{\stdClass, int}> $assigned
11+
*/
12+
13+
assertType(Foo::class, $this);
14+
assertType('array<int, array(stdClass, int)>', $assigned);
15+
16+
17+
/**
18+
* @var array
19+
* @phpstan-var array{\stdClass, int}
20+
*/
21+
$foo = doFoo();
22+
23+
assertType('array(stdClass, int)', $foo);

0 commit comments

Comments
 (0)