Skip to content

Commit f4ef9aa

Browse files
committed
Rework ClassStatementsGatherer to be usable in GNSR
1 parent 25f1cd2 commit f4ef9aa

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,26 @@ private function processStmtNode(
988988
throw new ShouldNotHappenException();
989989
}
990990

991-
$classStatementsGatherer = new ClassStatementsGatherer($classReflection, $nodeCallback);
992-
$this->processAttributeGroups($stmt, $stmt->attrGroups, $classScope, $classStatementsGatherer);
991+
$classStatementsGatherer = new ClassStatementsGatherer($classReflection);
992+
$classStatementsGathererCallback = new class ($classStatementsGatherer, $nodeCallback) {
993+
994+
/**
995+
* @param callable(Node $node, Scope $scope): void $nodeCallback
996+
*/
997+
public function __construct(
998+
private ClassStatementsGatherer $classStatementsGatherer,
999+
private $nodeCallback,
1000+
)
1001+
{
1002+
}
1003+
1004+
public function __invoke(Node $node, Scope $scope): void
1005+
{
1006+
($this->classStatementsGatherer)($node, $scope, $this->nodeCallback);
1007+
}
1008+
1009+
};
1010+
$this->processAttributeGroups($stmt, $stmt->attrGroups, $classScope, $classStatementsGathererCallback);
9931011

9941012
$classLikeStatements = $stmt->stmts;
9951013
if ($this->narrowMethodScopeFromConstructor) {
@@ -1010,7 +1028,7 @@ private function processStmtNode(
10101028
});
10111029
}
10121030

1013-
$this->processStmtNodesInternal($stmt, $classLikeStatements, $classScope, $classStatementsGatherer, $context);
1031+
$this->processStmtNodesInternal($stmt, $classLikeStatements, $classScope, $classStatementsGathererCallback, $context);
10141032
$nodeCallback(new ClassPropertiesNode($stmt, $this->readWritePropertiesExtensionProvider, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls(), $classStatementsGatherer->getReturnStatementsNodes(), $classStatementsGatherer->getPropertyAssigns(), $classReflection), $classScope);
10151033
$nodeCallback(new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls(), $classReflection), $classScope);
10161034
$nodeCallback(new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches(), $classReflection), $classScope);

src/Node/ClassStatementsGatherer.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ final class ClassStatementsGatherer
3232
'array_walk',
3333
];
3434

35-
/** @var callable(Node $node, Scope $scope): void */
36-
private $nodeCallback;
37-
3835
/** @var ClassPropertyNode[] */
3936
private array $properties = [];
4037

@@ -59,15 +56,10 @@ final class ClassStatementsGatherer
5956
/** @var list<PropertyAssign> */
6057
private array $propertyAssigns = [];
6158

62-
/**
63-
* @param callable(Node $node, Scope $scope): void $nodeCallback
64-
*/
6559
public function __construct(
6660
private ClassReflection $classReflection,
67-
callable $nodeCallback,
6861
)
6962
{
70-
$this->nodeCallback = $nodeCallback;
7163
}
7264

7365
/**
@@ -134,9 +126,11 @@ public function getPropertyAssigns(): array
134126
return $this->propertyAssigns;
135127
}
136128

137-
public function __invoke(Node $node, Scope $scope): void
129+
/**
130+
* @param callable(Node $node, Scope $scope): void $nodeCallback
131+
*/
132+
public function __invoke(Node $node, Scope $scope, callable $nodeCallback): void
138133
{
139-
$nodeCallback = $this->nodeCallback;
140134
$nodeCallback($node, $scope);
141135
$this->gatherNodes($node, $scope);
142136
}

0 commit comments

Comments
 (0)