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
11 changes: 9 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
}
} elseif ($expr instanceof Expr\Closure) {
$processClosureResult = $this->processClosureNode($stmt, $expr, $scope, $storage, $nodeCallback, $context, null);
$scope = $processClosureResult->getScope();
$scope = $processClosureResult->applyByRefUseScope($processClosureResult->getScope());

return new ExpressionResult(
$scope,
Expand Down Expand Up @@ -5142,7 +5142,7 @@ private function processClosureNode(
array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints),
), $closureScope, $storage);

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

/**
Expand Down Expand Up @@ -5551,6 +5551,8 @@ private function processArgs(
$isAlwaysTerminating = false;
/** @var list<array{InvalidateExprNode[], string[]}> $deferredInvalidateExpressions */
$deferredInvalidateExpressions = [];
/** @var ProcessClosureResult[] $deferredByRefClosureResults */
$deferredByRefClosureResults = [];
foreach ($args as $i => $arg) {
$assignByReference = false;
$parameter = null;
Expand Down Expand Up @@ -5662,6 +5664,7 @@ private function processArgs(
}

$scope = $closureResult->getScope();
$deferredByRefClosureResults[] = $closureResult;
$invalidateExpressions = $closureResult->getInvalidateExpressions();
if ($restoreThisScope !== null) {
$nodeFinder = new NodeFinder();
Expand Down Expand Up @@ -5753,6 +5756,10 @@ private function processArgs(
$scope = $this->processImmediatelyCalledCallable($scope, $invalidateExpressions, $uses);
}

foreach ($deferredByRefClosureResults as $deferredClosureResult) {
$scope = $deferredClosureResult->applyByRefUseScope($scope);
}

if ($parameters !== null) {
foreach ($args as $i => $arg) {
$assignByReference = false;
Expand Down
13 changes: 13 additions & 0 deletions src/Analyser/ProcessClosureResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Analyser;

use PhpParser\Node\ClosureUse;
use PHPStan\Node\InvalidateExprNode;

final class ProcessClosureResult
Expand All @@ -11,13 +12,16 @@ final class ProcessClosureResult
* @param InternalThrowPoint[] $throwPoints
* @param ImpurePoint[] $impurePoints
* @param InvalidateExprNode[] $invalidateExpressions
* @param ClosureUse[] $byRefUses
*/
public function __construct(
private MutatingScope $scope,
private array $throwPoints,
private array $impurePoints,
private array $invalidateExpressions,
private bool $isAlwaysTerminating,
private ?MutatingScope $byRefClosureResultScope = null,
private array $byRefUses = [],
)
{
}
Expand All @@ -27,6 +31,15 @@ public function getScope(): MutatingScope
return $this->scope;
}

public function applyByRefUseScope(MutatingScope $scope): MutatingScope
{
if ($this->byRefClosureResultScope === null) {
return $scope;
}

return $scope->processClosureScope($this->byRefClosureResultScope, null, $this->byRefUses);
}

/**
* @return InternalThrowPoint[]
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Methods/data/discussion-14038.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ function(): void {
);
}
}

class ByRefTest
{
private function foo(callable $c, string $s): void
{
}

public function testByRef(): void
{
$x = 'hello';
$this->foo(
function() use (&$x): void {
$x = 42;
},
$x,
);
}
}
Loading