1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #4422 - be aware of nested template params

This commit is contained in:
Matt Brown 2020-10-27 10:01:17 -04:00 committed by Daniil Gentili
parent 1762f39a74
commit 7f975045f4
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 34 additions and 5 deletions

View File

@ -215,10 +215,9 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements FileSour
$functionlike_types = [];
if ($this->functionlike_node_scanners) {
$functionlike_node_scanner = end($this->functionlike_node_scanners);
foreach ($this->functionlike_node_scanners as $functionlike_node_scanner) {
$functionlike_storage = $functionlike_node_scanner->storage;
$functionlike_types = $functionlike_storage->template_types ?? [];
$functionlike_types += $functionlike_storage->template_types ?? [];
}
$functionlike_node_scanner = new Reflector\FunctionLikeNodeScanner(
@ -341,8 +340,7 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements FileSour
$template_types = $classlike_storage->template_types ?? [];
}
if ($this->functionlike_node_scanners) {
$functionlike_node_scanner = end($this->functionlike_node_scanners);
foreach ($this->functionlike_node_scanners as $functionlike_node_scanner) {
$functionlike_storage = $functionlike_node_scanner->storage;
$template_types += $functionlike_storage->template_types ?? [];
}

View File

@ -1402,6 +1402,37 @@ class FunctionTemplateTest extends TestCase
}
}'
],
'doublyNestedFunctionTemplates' => [
'<?php
/**
* @psalm-template Tk
* @psalm-template Tv
*
* @psalm-param iterable<Tk, Tv> $iterable
* @psalm-param (callable(Tk, Tv): bool)|null $predicate
*
* @psalm-return iterable<Tk, Tv>
*/
function filter_with_key(iterable $iterable, ?callable $predicate = null): iterable
{
return (static function () use ($iterable, $predicate): Generator {
$predicate = $predicate ??
/**
* @psalm-param Tk $_k
* @psalm-param Tv $v
*
* @return bool
*/
function($_k, $v) { return (bool) $v; };
foreach ($iterable as $k => $v) {
if ($predicate($k, $v)) {
yield $k => $v;
}
}
})();
}'
],
];
}