Mercurial > hg > isophonics-drupal-site
comparison vendor/nikic/php-parser/doc/component/Walking_the_AST.markdown @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 5fb285c0d0e3 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
213 | 213 |
214 ``` | 214 ``` |
215 private $class = null; | 215 private $class = null; |
216 public function enterNode(Node $node) { | 216 public function enterNode(Node $node) { |
217 if ($node instanceof Node\Stmt\Class_ && | 217 if ($node instanceof Node\Stmt\Class_ && |
218 $node->namespaceName->toString() === 'Foo\Bar\Baz' | 218 $node->namespacedName->toString() === 'Foo\Bar\Baz' |
219 ) { | 219 ) { |
220 $this->class = $node; | 220 $this->class = $node; |
221 return NodeTraverser::STOP_TRAVERSAL; | 221 return NodeTraverser::STOP_TRAVERSAL; |
222 } | 222 } |
223 } | 223 } |
233 | 233 |
234 ```php | 234 ```php |
235 $traverser = new NodeTraverser; | 235 $traverser = new NodeTraverser; |
236 $traverser->addVisitor($visitorA); | 236 $traverser->addVisitor($visitorA); |
237 $traverser->addVisitor($visitorB); | 237 $traverser->addVisitor($visitorB); |
238 $stmts = $traverser->traverser($stmts); | 238 $stmts = $traverser->traverse($stmts); |
239 ``` | 239 ``` |
240 | 240 |
241 It is important to understand that if a traverser is run with multiple visitors, the visitors will | 241 It is important to understand that if a traverser is run with multiple visitors, the visitors will |
242 be interleaved. Given the following AST excerpt | 242 be interleaved. Given the following AST excerpt |
243 | 243 |
279 When using multiple visitors, it is important to understand how they interact with the various | 279 When using multiple visitors, it is important to understand how they interact with the various |
280 special enterNode/leaveNode return values: | 280 special enterNode/leaveNode return values: |
281 | 281 |
282 * If *any* visitor returns `DONT_TRAVERSE_CHILDREN`, the children will be skipped for *all* | 282 * If *any* visitor returns `DONT_TRAVERSE_CHILDREN`, the children will be skipped for *all* |
283 visitors. | 283 visitors. |
284 * If *any* visitor returns `DONT_TRAVERSE_CURRENT_AND_CHILDREN`, the children will be skipped for *all* | |
285 visitors, and all *subsequent* visitors will not visit the current node. | |
284 * If *any* visitor returns `STOP_TRAVERSAL`, traversal is stopped for *all* visitors. | 286 * If *any* visitor returns `STOP_TRAVERSAL`, traversal is stopped for *all* visitors. |
285 * If a visitor returns a replacement node, subsequent visitors will be passed the replacement node, | 287 * If a visitor returns a replacement node, subsequent visitors will be passed the replacement node, |
286 not the original one. | 288 not the original one. |
287 * If a visitor returns `REMOVE_NODE`, subsequent visitors will not see this node. | 289 * If a visitor returns `REMOVE_NODE`, subsequent visitors will not see this node. |
288 * If a visitor returns an array of replacement nodes, subsequent visitors will see neither the node | 290 * If a visitor returns an array of replacement nodes, subsequent visitors will see neither the node |
303 | 305 |
304 // Find all class nodes. | 306 // Find all class nodes. |
305 $classes = $nodeFinder->findInstanceOf($stmts, Node\Stmt\Class_::class); | 307 $classes = $nodeFinder->findInstanceOf($stmts, Node\Stmt\Class_::class); |
306 | 308 |
307 // Find all classes that extend another class | 309 // Find all classes that extend another class |
308 $extendingClasses = $nodeFinder->findInstanceOf($stmts, function(Node $node) { | 310 $extendingClasses = $nodeFinder->find($stmts, function(Node $node) { |
309 return $node instanceof Node\Stmt\Class_ | 311 return $node instanceof Node\Stmt\Class_ |
310 && $node->extends !== null; | 312 && $node->extends !== null; |
311 }); | 313 }); |
312 | 314 |
313 // Find first class occuring in the AST. Returns null if no class exists. | 315 // Find first class occuring in the AST. Returns null if no class exists. |