-
Notifications
You must be signed in to change notification settings - Fork 542
More precise types in immediately invoked callables #3592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Parser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
final class ImmediatelyInvokedClosureVisitor extends NodeVisitorAbstract | ||
{ | ||
|
||
public const ATTRIBUTE_NAME = 'isImmediatelyInvokedClosure'; | ||
|
||
private bool $inFuncCall = false; | ||
|
||
public function beforeTraverse(array $nodes): ?array | ||
{ | ||
$this->inFuncCall = false; | ||
return null; | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Node\Expr\FuncCall) { | ||
$this->inFuncCall = true; | ||
} | ||
|
||
if ( | ||
$this->inFuncCall | ||
&& $node instanceof Node\Expr\Closure | ||
) { | ||
$node->setAttribute(self::ATTRIBUTE_NAME, true); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function leaveNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Node\Expr\FuncCall) { | ||
$this->inFuncCall = false; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace Bug11561; | ||
|
||
use function PHPStan\Testing\assertType; | ||
use DateTime; | ||
|
||
/** @param array{date: DateTime} $c */ | ||
function main(mixed $c): void{ | ||
assertType('array{date: DateTime}', $c); | ||
$c['id']=1; | ||
assertType('array{date: DateTime, id: 1}', $c); | ||
|
||
$x = (function() use (&$c) { | ||
assertType("array{date: DateTime, id: 1, name?: 'ruud'}", $c); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is wrong, see phpstan/phpstan#11945 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My reasoning is, that since this function is invoked immediately, it should be treated as the same code as normally, without the function. |
||
$c['name'] = 'ruud'; | ||
assertType("array{date: DateTime, id: 1, name: 'ruud'}", $c); | ||
return 'x'; | ||
})(); | ||
|
||
assertType("array{date: DateTime, id: 1, name?: 'ruud'}", $c); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is wrong, see phpstan/phpstan#11945 |
||
} | ||
|
||
|
||
/** @param array{date: DateTime} $c */ | ||
function main2(mixed $c): void{ | ||
assertType('array{date: DateTime}', $c); | ||
$c['id']=1; | ||
$c['name'] = 'staabm'; | ||
assertType("array{date: DateTime, id: 1, name: 'staabm'}", $c); | ||
|
||
$x = (function() use (&$c) { | ||
assertType("array{date: DateTime, id: 1, name: 'ruud'|'staabm'}", $c); | ||
$c['name'] = 'ruud'; | ||
assertType("array{date: DateTime, id: 1, name: 'ruud'}", $c); | ||
return 'x'; | ||
})(); | ||
|
||
assertType("array{date: DateTime, id: 1, name: 'ruud'|'staabm'}", $c); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would lead to a lot of false positives. You only want to tag this if in
$node->name
, but right now the visitor is tagging args too.Just check
$node->name
inside$node instanceof Node\Expr\FuncCall
.