22namespace StubsGenerator ;
33
44use PhpParser \Node ;
5+ use PhpParser \Node \Arg ;
56use PhpParser \Node \Expr \ArrayDimFetch ;
67use PhpParser \Node \Expr \Assign ;
78use PhpParser \Node \Expr \ConstFetch ;
2526use PhpParser \NodeTraverser ;
2627use PhpParser \NodeVisitorAbstract ;
2728
29+ use function count ;
30+ use function defined ;
31+ use function is_string ;
32+
2833/**
2934 * On node traversal, this visitor converts any AST to one just containing stub
3035 * definitions, removing anything uninteresting.
@@ -53,22 +58,21 @@ class NodeVisitor extends NodeVisitorAbstract
5358 private $ includeInaccessibleClassNodes ;
5459
5560 /**
56- * @psalm-suppress PropertyNotSetInConstructor
57- * @var Node[]
61+ * @var array<Node>
5862 */
5963 protected $ stack ;
6064
61- /** @var Namespace_[] */
65+ /** @var array< Namespace_> */
6266 private $ namespaces = [];
6367 /** @var Namespace_ */
6468 private $ globalNamespace ;
65- /** @var Node[] */
69+ /** @var array< Node> */
6670 private $ globalExpressions = [];
67- /** @var ClassLikeWithDependencies[] */
71+ /** @var array<string, ClassLikeWithDependencies> */
6872 private $ classLikes = [];
69- /** @var true[] */
73+ /** @var array<string, true> */
7074 private $ resolvedClassLikes = [];
71- /** @var Namespace_[] */
75+ /** @var array< Namespace_> */
7276 private $ classLikeNamespaces = [];
7377 /** @var Namespace_|null */
7478 private $ currentClassLikeNamespace = null ;
@@ -79,8 +83,7 @@ class NodeVisitor extends NodeVisitorAbstract
7983 private $ isInIf = false ;
8084
8185 /**
82- * @var int[][]
83- * @psalm-var array<string, array<string, int>>
86+ * @var array<string, array<string, int>>
8487 */
8588 private $ counts = [
8689 'functions ' => [],
@@ -93,6 +96,8 @@ class NodeVisitor extends NodeVisitorAbstract
9396
9497 /**
9598 * @param int $symbols Set of symbol types to include stubs for.
99+ * @param array<mixed> $config
100+ * @return void
96101 */
97102 public function init (int $ symbols = StubsGenerator::DEFAULT , array $ config = [])
98103 {
@@ -113,6 +118,7 @@ public function init(int $symbols = StubsGenerator::DEFAULT, array $config = [])
113118 public function beforeTraverse (array $ nodes )
114119 {
115120 $ this ->stack = [];
121+ return null ;
116122 }
117123
118124 public function enterNode (Node $ node )
@@ -121,7 +127,7 @@ public function enterNode(Node $node)
121127
122128 if ($ node instanceof Namespace_) {
123129 // We always need to parse the children of namespaces.
124- return ;
130+ return null ;
125131 }
126132
127133 if (($ this ->needsClasses && $ node instanceof Class_)
@@ -180,14 +186,15 @@ public function enterNode(Node $node)
180186 // We'll examine the first level inside of an if statement to
181187 // look for function/class/etc. declarations.
182188 $ this ->isInIf = true ;
183- return ; // Traverse children.
189+ return null ; // Traverse children.
184190 }
185191 }
186192
187193 if (!$ this ->isInDeclaration ) {
188194 // Don't bother parsing descendents of uninteresting nodes.
189195 return NodeTraverser::DONT_TRAVERSE_CHILDREN ;
190196 }
197+ return null ;
191198 }
192199
193200 public function leaveNode (Node $ node , bool $ preserveStack = false )
@@ -231,12 +238,12 @@ public function leaveNode(Node $node, bool $preserveStack = false)
231238
232239 if ($ node instanceof Namespace_) {
233240 $ this ->namespaces [] = $ node ;
234- return ;
241+ return null ;
235242 }
236243
237244 if ($ node instanceof Name) {
238245 // Can't delete namespace names!
239- return ;
246+ return null ;
240247 }
241248
242249 if ($ parent && !($ parent instanceof Namespace_)) {
@@ -250,7 +257,7 @@ public function leaveNode(Node $node, bool $preserveStack = false)
250257 }
251258 }
252259
253- return ;
260+ return null ;
254261 }
255262
256263 $ namespaceName = ($ parent && $ parent ->name ) ? $ parent ->name ->toString () : '' ;
@@ -265,7 +272,7 @@ public function leaveNode(Node $node, bool $preserveStack = false)
265272 } elseif ($ parent ) {
266273 // If we're here, `$parent` is a namespace. Let's just keep the
267274 // `$node` around in `$parent->stmts`.
268- return ; // Don't remove.
275+ return null ; // Don't remove.
269276 } elseif ($ node instanceof Stmt) {
270277 // Anything other than a namespace which doesn't have a parent
271278 // node must belong in the global namespace. We can still remove
@@ -290,12 +297,13 @@ public function afterTraverse(array $nodes)
290297 $ this ->namespaces = array_filter ($ this ->namespaces , function (Namespace_ $ ns ): bool {
291298 return (bool ) $ ns ->stmts ;
292299 });
300+ return null ;
293301 }
294302
295303 /**
296304 * Returns the stored set of stub nodes which are built up during traversal.
297305 *
298- * @return Node[]
306+ * @return array< Node>
299307 */
300308 public function getStubStmts (): array
301309 {
@@ -325,7 +333,7 @@ public function getStubStmts(): array
325333 *
326334 * These counts are built up during traversal.
327335 *
328- * @psalm- return array<string, array<string, int>>
336+ * @return array<string, array<string, int>>
329337 */
330338 public function getCounts (): array
331339 {
@@ -394,7 +402,9 @@ function (\PhpParser\Node\Const_ $const) {
394402 $ node instanceof Expression &&
395403 $ node ->expr instanceof FuncCall &&
396404 $ node ->expr ->name instanceof Name &&
397- $ node ->expr ->name ->getFirst () === 'define '
405+ $ node ->expr ->name ->getFirst () === 'define ' &&
406+ $ node ->expr ->args [0 ] instanceof Arg &&
407+ $ node ->expr ->args [0 ]->value instanceof String_
398408 ) {
399409 $ fullyQualifiedName = $ node ->expr ->args [0 ]->value ->value ;
400410
0 commit comments