NodeFinder.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\NodeVisitor\FindingVisitor;
  4. use PhpParser\NodeVisitor\FirstFindingVisitor;
  5. class NodeFinder {
  6. /**
  7. * Find all nodes satisfying a filter callback.
  8. *
  9. * @param Node|Node[] $nodes Single node or array of nodes to search in
  10. * @param callable $filter Filter callback: function(Node $node) : bool
  11. *
  12. * @return Node[] Found nodes satisfying the filter callback
  13. */
  14. public function find($nodes, callable $filter): array {
  15. if ($nodes === []) {
  16. return [];
  17. }
  18. if (!is_array($nodes)) {
  19. $nodes = [$nodes];
  20. }
  21. $visitor = new FindingVisitor($filter);
  22. $traverser = new NodeTraverser($visitor);
  23. $traverser->traverse($nodes);
  24. return $visitor->getFoundNodes();
  25. }
  26. /**
  27. * Find all nodes that are instances of a certain class.
  28. * @template TNode as Node
  29. *
  30. * @param Node|Node[] $nodes Single node or array of nodes to search in
  31. * @param class-string<TNode> $class Class name
  32. *
  33. * @return TNode[] Found nodes (all instances of $class)
  34. */
  35. public function findInstanceOf($nodes, string $class): array {
  36. return $this->find($nodes, function ($node) use ($class) {
  37. return $node instanceof $class;
  38. });
  39. }
  40. /**
  41. * Find first node satisfying a filter callback.
  42. *
  43. * @param Node|Node[] $nodes Single node or array of nodes to search in
  44. * @param callable $filter Filter callback: function(Node $node) : bool
  45. *
  46. * @return null|Node Found node (or null if none found)
  47. */
  48. public function findFirst($nodes, callable $filter): ?Node {
  49. if ($nodes === []) {
  50. return null;
  51. }
  52. if (!is_array($nodes)) {
  53. $nodes = [$nodes];
  54. }
  55. $visitor = new FirstFindingVisitor($filter);
  56. $traverser = new NodeTraverser($visitor);
  57. $traverser->traverse($nodes);
  58. return $visitor->getFoundNode();
  59. }
  60. /**
  61. * Find first node that is an instance of a certain class.
  62. *
  63. * @template TNode as Node
  64. *
  65. * @param Node|Node[] $nodes Single node or array of nodes to search in
  66. * @param class-string<TNode> $class Class name
  67. *
  68. * @return null|TNode Found node, which is an instance of $class (or null if none found)
  69. */
  70. public function findFirstInstanceOf($nodes, string $class): ?Node {
  71. return $this->findFirst($nodes, function ($node) use ($class) {
  72. return $node instanceof $class;
  73. });
  74. }
  75. }