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
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,11 @@ services:
tags:
- phpstan.parser.richParserNodeVisitor

-
class: PHPStan\Parser\ImmediatelyInvokedClosureVisitor
tags:
- phpstan.parser.richParserNodeVisitor

-
class: PHPStan\Parallel\ParallelAnalyser
arguments:
Expand Down
6 changes: 5 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Parser\ArrayMapArgVisitor;
use PHPStan\Parser\ImmediatelyInvokedClosureVisitor;
use PHPStan\Parser\NewAssignedToPropertyVisitor;
use PHPStan\Parser\Parser;
use PHPStan\Php\PhpVersion;
Expand Down Expand Up @@ -4794,6 +4795,7 @@ private function processFinallyScopeVariableTypeHolders(
* @param Expr\ClosureUse[] $byRefUses
*/
public function processClosureScope(
Expr\Closure $expr,
self $closureScope,
?self $prevScope,
array $byRefUses,
Expand Down Expand Up @@ -4826,7 +4828,9 @@ public function processClosureScope(
$prevVariableType = $prevScope->getVariableType($variableName);
if (!$variableType->equals($prevVariableType)) {
$variableType = TypeCombinator::union($variableType, $prevVariableType);
$variableType = self::generalizeType($variableType, $prevVariableType, 0);
if ($expr->getAttribute(ImmediatelyInvokedClosureVisitor::ATTRIBUTE_NAME) !== true) {
$variableType = self::generalizeType($variableType, $prevVariableType, 0);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4232,7 +4232,7 @@ private function processClosureNode(
}

$closureScope = $scope->enterAnonymousFunction($expr, $callableParameters);
$closureScope = $closureScope->processClosureScope($scope, null, $byRefUses);
$closureScope = $closureScope->processClosureScope($expr, $scope, null, $byRefUses);
$closureType = $closureScope->getAnonymousFunctionReflection();
if (!$closureType instanceof ClosureType) {
throw new ShouldNotHappenException();
Expand Down Expand Up @@ -4302,7 +4302,7 @@ private function processClosureNode(
$intermediaryClosureScope = $intermediaryClosureScope->mergeWith($exitPoint->getScope());
}
$closureScope = $scope->enterAnonymousFunction($expr, $callableParameters);
$closureScope = $closureScope->processClosureScope($intermediaryClosureScope, $prevScope, $byRefUses);
$closureScope = $closureScope->processClosureScope($expr, $intermediaryClosureScope, $prevScope, $byRefUses);
if ($closureScope->equals($prevScope)) {
break;
}
Expand All @@ -4322,7 +4322,7 @@ private function processClosureNode(
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
), $closureScope);

return new ProcessClosureResult($scope->processClosureScope($closureScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);
return new ProcessClosureResult($scope->processClosureScope($expr, $closureScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Parser/ImmediatelyInvokedClosureVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;

final class ImmediatelyInvokedClosureVisitor extends NodeVisitorAbstract
{

public const ATTRIBUTE_NAME = 'isImmediatelyInvokedClosure';

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Expr\Closure) {
$node->name->setAttribute(self::ATTRIBUTE_NAME, true);
}

return null;
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11561.php
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);
$c['name'] = 'ruud';
assertType("array{date: DateTime, id: 1, name: 'ruud'}", $c);
return 'x';
})();

assertType("array{date: DateTime, id: 1, name?: 'ruud'}", $c);
}


/** @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);
}
Loading