Skip to content

Commit f18bd86

Browse files
committed
Virtual nodes - pass specific reflection so that Scope::getFunction() does not need to be used
1 parent 34e10fc commit f18bd86

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ private function processStmtNode(
421421
$isPure,
422422
$acceptsNamedArguments,
423423
);
424-
$nodeCallback(new InFunctionNode($stmt), $functionScope);
424+
$functionReflection = $functionScope->getFunction();
425+
if (!$functionReflection instanceof FunctionReflection) {
426+
throw new ShouldNotHappenException();
427+
}
428+
429+
$nodeCallback(new InFunctionNode($functionReflection, $stmt), $functionScope);
425430

426431
$gatheredReturnStatements = [];
427432
$executionEnds = [];
@@ -506,7 +511,11 @@ private function processStmtNode(
506511
}
507512

508513
if ($stmt->getAttribute('virtual', false) === false) {
509-
$nodeCallback(new InClassMethodNode($stmt), $methodScope);
514+
$methodReflection = $methodScope->getFunction();
515+
if (!$methodReflection instanceof MethodReflection) {
516+
throw new ShouldNotHappenException();
517+
}
518+
$nodeCallback(new InClassMethodNode($methodReflection, $stmt), $methodScope);
510519
}
511520

512521
if ($stmt->stmts !== null) {
@@ -2978,7 +2987,12 @@ private function processClosureNode(
29782987

29792988
$closureScope = $scope->enterAnonymousFunction($expr, $callableParameters);
29802989
$closureScope = $closureScope->processClosureScope($scope, null, $byRefUses);
2981-
$nodeCallback(new InClosureNode($expr), $closureScope);
2990+
$closureType = $closureScope->getAnonymousFunctionReflection();
2991+
if (!$closureType instanceof ClosureType) {
2992+
throw new ShouldNotHappenException();
2993+
}
2994+
2995+
$nodeCallback(new InClosureNode($closureType, $expr), $closureScope);
29822996

29832997
$gatheredReturnStatements = [];
29842998
$gatheredYieldStatements = [];

src/Node/InClassMethodNode.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
namespace PHPStan\Node;
44

55
use PhpParser\Node;
6+
use PHPStan\Reflection\MethodReflection;
67

78
/** @api */
89
class InClassMethodNode extends Node\Stmt implements VirtualNode
910
{
1011

11-
public function __construct(private Node\Stmt\ClassMethod $originalNode)
12+
public function __construct(
13+
private MethodReflection $methodReflection,
14+
private Node\Stmt\ClassMethod $originalNode,
15+
)
1216
{
1317
parent::__construct($originalNode->getAttributes());
1418
}
1519

20+
public function getMethodReflection(): MethodReflection
21+
{
22+
return $this->methodReflection;
23+
}
24+
1625
public function getOriginalNode(): Node\Stmt\ClassMethod
1726
{
1827
return $this->originalNode;

src/Node/InClosureNode.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\Closure;
77
use PhpParser\NodeAbstract;
8+
use PHPStan\Type\ClosureType;
89

910
/** @api */
1011
class InClosureNode extends NodeAbstract implements VirtualNode
1112
{
1213

1314
private Node\Expr\Closure $originalNode;
1415

15-
public function __construct(Closure $originalNode)
16+
public function __construct(private ClosureType $closureType, Closure $originalNode)
1617
{
1718
parent::__construct($originalNode->getAttributes());
1819
$this->originalNode = $originalNode;
1920
}
2021

22+
public function getClosureType(): ClosureType
23+
{
24+
return $this->closureType;
25+
}
26+
2127
public function getOriginalNode(): Closure
2228
{
2329
return $this->originalNode;

src/Node/InFunctionNode.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
namespace PHPStan\Node;
44

55
use PhpParser\Node;
6+
use PHPStan\Reflection\FunctionReflection;
67

78
/** @api */
89
class InFunctionNode extends Node\Stmt implements VirtualNode
910
{
1011

11-
public function __construct(private Node\Stmt\Function_ $originalNode)
12+
public function __construct(
13+
private FunctionReflection $functionReflection,
14+
private Node\Stmt\Function_ $originalNode,
15+
)
1216
{
1317
parent::__construct($originalNode->getAttributes());
1418
}
1519

20+
public function getFunctionReflection(): FunctionReflection
21+
{
22+
return $this->functionReflection;
23+
}
24+
1625
public function getOriginalNode(): Node\Stmt\Function_
1726
{
1827
return $this->originalNode;

0 commit comments

Comments
 (0)