annotate vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/FirstFindingVisitor.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser\NodeVisitor;
Chris@0 4
Chris@0 5 use PhpParser\Node;
Chris@0 6 use PhpParser\NodeTraverser;
Chris@0 7 use PhpParser\NodeVisitorAbstract;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * This visitor can be used to find the first node satisfying some criterion determined by
Chris@0 11 * a filter callback.
Chris@0 12 */
Chris@0 13 class FirstFindingVisitor extends NodeVisitorAbstract
Chris@0 14 {
Chris@0 15 /** @var callable Filter callback */
Chris@0 16 protected $filterCallback;
Chris@0 17 /** @var null|Node Found node */
Chris@0 18 protected $foundNode;
Chris@0 19
Chris@0 20 public function __construct(callable $filterCallback) {
Chris@0 21 $this->filterCallback = $filterCallback;
Chris@0 22 }
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Get found node satisfying the filter callback.
Chris@0 26 *
Chris@0 27 * Returns null if no node satisfies the filter callback.
Chris@0 28 *
Chris@0 29 * @return null|Node Found node (or null if not found)
Chris@0 30 */
Chris@0 31 public function getFoundNode() {
Chris@0 32 return $this->foundNode;
Chris@0 33 }
Chris@0 34
Chris@0 35 public function beforeTraverse(array $nodes) {
Chris@0 36 $this->foundNode = null;
Chris@0 37
Chris@0 38 return null;
Chris@0 39 }
Chris@0 40
Chris@0 41 public function enterNode(Node $node) {
Chris@0 42 $filterCallback = $this->filterCallback;
Chris@0 43 if ($filterCallback($node)) {
Chris@0 44 $this->foundNode = $node;
Chris@0 45 return NodeTraverser::STOP_TRAVERSAL;
Chris@0 46 }
Chris@0 47
Chris@0 48 return null;
Chris@0 49 }
Chris@0 50 }