Skip to content

Commit 3a0da5b

Browse files
committed
VirtualAssignNodeCallback + ShallowNodeCallback
1 parent a512714 commit 3a0da5b

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,7 +2553,7 @@ public function processExprNode(
25532553

25542554
$existingExprResult = $storage->findResult($expr);
25552555
if ($existingExprResult !== null) {
2556-
if ($nodeCallback instanceof NoopNodeCallback) {
2556+
if ($nodeCallback instanceof ShallowNodeCallback) {
25572557
return $existingExprResult;
25582558
}
25592559
throw new ShouldNotHappenException(sprintf('Expr %s on line %d has already been analysed', get_class($expr), $expr->getStartLine()));
@@ -6404,13 +6404,7 @@ private function processVirtualAssign(MutatingScope $scope, ExpressionResultStor
64046404
$stmt,
64056405
$var,
64066406
$assignedExpr,
6407-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
6408-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
6409-
return;
6410-
}
6411-
6412-
$nodeCallback($node, $scope);
6413-
},
6407+
new VirtualAssignNodeCallback($nodeCallback),
64146408
ExpressionContext::createDeep(),
64156409
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
64166410
false,

src/Analyser/NoopNodeCallback.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace PHPStan\Analyser;
44

5-
final class NoopNodeCallback
5+
use PhpParser\Node;
6+
7+
final class NoopNodeCallback implements ShallowNodeCallback
68
{
79

8-
public function __invoke(): void
10+
public function __invoke(Node $node, Scope $scope): void
911
{
1012
// noop
1113
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node;
6+
7+
interface ShallowNodeCallback
8+
{
9+
10+
public function __invoke(Node $node, Scope $scope): void;
11+
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Node\PropertyAssignNode;
7+
use PHPStan\Node\VariableAssignNode;
8+
9+
final class VirtualAssignNodeCallback implements ShallowNodeCallback
10+
{
11+
12+
/**
13+
* @param callable(Node $node, Scope $scope): void $originalNodeCallback
14+
*/
15+
public function __construct(private mixed $originalNodeCallback)
16+
{
17+
}
18+
19+
public function __invoke(Node $node, Scope $scope): void
20+
{
21+
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
22+
return;
23+
}
24+
25+
($this->originalNodeCallback)($node, $scope);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)