Skip to content

Commit a7825e7

Browse files
committed
FileTypeMapper - fix wrong template type scope for inline @var
1 parent 03b5200 commit a7825e7

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

src/Type/FileTypeMapper.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private function getNameScopeMap(string $fileName): array
325325
{
326326
if (!isset($this->memoryCache[$fileName])) {
327327
$cacheKey = sprintf('ftm-%s', $fileName);
328-
$variableCacheKey = sprintf('v2-%s', ComposerHelper::getPhpDocParserVersion());
328+
$variableCacheKey = sprintf('v3-%s', ComposerHelper::getPhpDocParserVersion());
329329
$cached = $this->loadCachedPhpDocNodeMap($cacheKey, $variableCacheKey);
330330
if ($cached === null) {
331331
[$nameScopeMap, $files] = $this->createPhpDocNodeMap($fileName, null, null, [], $fileName);
@@ -507,6 +507,19 @@ function (Node $node) use ($fileName, $lookForTrait, &$traitFound, $traitMethodA
507507
);
508508
} elseif ($node instanceof Node\Stmt\ClassLike) {
509509
$typeAliasStack[] = [];
510+
} else {
511+
$parentNameScope = array_last($typeMapStack) ?? null;
512+
$typeMapStack[] = new IntermediaryNameScope(
513+
$namespace,
514+
$uses,
515+
$className,
516+
$functionName,
517+
[],
518+
$parentNameScope,
519+
array_last($typeAliasStack) ?? [],
520+
constUses: $constUses,
521+
typeAliasClassName: $lookForTrait,
522+
);
510523
}
511524
}
512525

@@ -542,7 +555,7 @@ function (Node $node) use ($fileName, $lookForTrait, &$traitFound, $traitMethodA
542555
}
543556

544557
if ($node instanceof Node\Stmt\ClassLike || $node instanceof Node\Stmt\ClassMethod || $node instanceof Node\Stmt\Function_ || $node instanceof Node\PropertyHook) {
545-
if ($phpDocNode !== null) {
558+
if ($phpDocNode !== null || !$node instanceof Node\Stmt\ClassLike) {
546559
return self::POP_TYPE_MAP_STACK;
547560
}
548561

tests/PHPStan/Analyser/nsrt/generics.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,3 +1577,47 @@ function constantArrayBound(array $a): array
15771577
function (): void {
15781578
assertType('array{\'string\', true}', constantArrayBound(['string', true]));
15791579
};
1580+
1581+
/**
1582+
* @phpstan-template TParametersTypes of non-empty-list<mixed>
1583+
*/
1584+
class InlineVar
1585+
{
1586+
1587+
public function doFoo(): void
1588+
{
1589+
/** @var TParametersTypes $a */
1590+
$a = [1, 2, 3];
1591+
assertType('TParametersTypes of non-empty-list<mixed> (class PHPStan\Generics\FunctionsAssertType\InlineVar, parameter)', $a);
1592+
}
1593+
1594+
/**
1595+
* @template T
1596+
* @param T $b
1597+
*/
1598+
public function doBar($b): void
1599+
{
1600+
assertType('T (method PHPStan\Generics\FunctionsAssertType\InlineVar::doBar(), argument)', $b);
1601+
1602+
/** @var T $c */
1603+
$c = 2;
1604+
assertType('T (method PHPStan\Generics\FunctionsAssertType\InlineVar::doBar(), parameter)', $c);
1605+
1606+
/** @var TParametersTypes $a */
1607+
$a = [1, 2, 3];
1608+
assertType('TParametersTypes of non-empty-list<mixed> (class PHPStan\Generics\FunctionsAssertType\InlineVar, parameter)', $a);
1609+
}
1610+
1611+
/**
1612+
* @param int $b
1613+
*/
1614+
public function doBaz($b): void
1615+
{
1616+
assertType('int', $b);
1617+
1618+
/** @var TParametersTypes $a */
1619+
$a = [1, 2, 3];
1620+
assertType('TParametersTypes of non-empty-list<mixed> (class PHPStan\Generics\FunctionsAssertType\InlineVar, parameter)', $a);
1621+
}
1622+
1623+
}

0 commit comments

Comments
 (0)