Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Parser/ArrowFunctionArgVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ final class ArrowFunctionArgVisitor extends NodeVisitorAbstract

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Expr\ArrowFunction && !$node->isFirstClassCallable()) {
$args = $node->getArgs();
if (!$node instanceof Node\Expr\FuncCall) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (count($args) > 0) {
$node->name->setAttribute(self::ATTRIBUTE_NAME, $args);
}
if ($node->name instanceof Node\Expr\Assign && $node->name->expr instanceof Node\Expr\ArrowFunction) {
$arrow = $node->name->expr;
} elseif ($node->name instanceof Node\Expr\ArrowFunction) {
$arrow = $node->name;
} else {
return null;
}

$args = $node->getArgs();

if (count($args) > 0) {
$arrow->setAttribute(self::ATTRIBUTE_NAME, $args);
}

return null;
}

Expand Down
25 changes: 20 additions & 5 deletions src/Parser/ClosureArgVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ final class ClosureArgVisitor extends NodeVisitorAbstract

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Expr\Closure && !$node->isFirstClassCallable()) {
$args = $node->getArgs();
if (!$node instanceof Node\Expr\FuncCall) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (count($args) > 0) {
$node->name->setAttribute(self::ATTRIBUTE_NAME, $args);
}
if ($node->name instanceof Node\Expr\Assign && $node->name->expr instanceof Node\Expr\Closure) {
$closure = $node->name->expr;
} elseif ($node->name instanceof Node\Expr\Closure) {
$closure = $node->name;
} else {
return null;
}

$args = $node->getArgs();

if (count($args) > 0) {
$closure->setAttribute(self::ATTRIBUTE_NAME, $args);
}

return null;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/nsrt/arrow-function-argument-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function doFoo(int $integer, array $array, ?string $nullableString)
(fn($a, $b, $c) => assertType('array{int, array{a: int}, string|null}', [$a, $b, $c]))($integer, $array, $nullableString);

(fn($a, $b, $c = null) => assertType('array{int, array{a: int}, mixed}', [$a, $b, $c]))($integer, $array);

($callback = fn($context) => assertType('int', $context))($integer);
}

}
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/nsrt/closure-argument-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function doFoo(int $integer, array $array, ?string $nullableString)
assertType('array{a: int}', $context2);
assertType('mixed', $context3);
})($integer, $array);

($callback = function($context) {
assertType('int', $context);
})($integer);
}

}
Loading