NodeVisitor.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. interface NodeVisitor {
  4. /**
  5. * If NodeVisitor::enterNode() returns DONT_TRAVERSE_CHILDREN, child nodes
  6. * of the current node will not be traversed for any visitors.
  7. *
  8. * For subsequent visitors enterNode() will still be called on the current
  9. * node and leaveNode() will also be invoked for the current node.
  10. */
  11. public const DONT_TRAVERSE_CHILDREN = 1;
  12. /**
  13. * If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns
  14. * STOP_TRAVERSAL, traversal is aborted.
  15. *
  16. * The afterTraverse() method will still be invoked.
  17. */
  18. public const STOP_TRAVERSAL = 2;
  19. /**
  20. * If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs
  21. * in an array, it will be removed from the array.
  22. *
  23. * For subsequent visitors leaveNode() will still be invoked for the
  24. * removed node.
  25. */
  26. public const REMOVE_NODE = 3;
  27. /**
  28. * If NodeVisitor::enterNode() returns DONT_TRAVERSE_CURRENT_AND_CHILDREN, child nodes
  29. * of the current node will not be traversed for any visitors.
  30. *
  31. * For subsequent visitors enterNode() will not be called as well.
  32. * leaveNode() will be invoked for visitors that has enterNode() method invoked.
  33. */
  34. public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = 4;
  35. /**
  36. * If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns REPLACE_WITH_NULL,
  37. * the node will be replaced with null. This is not a legal return value if the node is part
  38. * of an array, rather than another node.
  39. */
  40. public const REPLACE_WITH_NULL = 5;
  41. /**
  42. * Called once before traversal.
  43. *
  44. * Return value semantics:
  45. * * null: $nodes stays as-is
  46. * * otherwise: $nodes is set to the return value
  47. *
  48. * @param Node[] $nodes Array of nodes
  49. *
  50. * @return null|Node[] Array of nodes
  51. */
  52. public function beforeTraverse(array $nodes);
  53. /**
  54. * Called when entering a node.
  55. *
  56. * Return value semantics:
  57. * * null
  58. * => $node stays as-is
  59. * * array (of Nodes)
  60. * => The return value is merged into the parent array (at the position of the $node)
  61. * * NodeVisitor::REMOVE_NODE
  62. * => $node is removed from the parent array
  63. * * NodeVisitor::REPLACE_WITH_NULL
  64. * => $node is replaced with null
  65. * * NodeVisitor::DONT_TRAVERSE_CHILDREN
  66. * => Children of $node are not traversed. $node stays as-is
  67. * * NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
  68. * => Further visitors for the current node are skipped, and its children are not
  69. * traversed. $node stays as-is.
  70. * * NodeVisitor::STOP_TRAVERSAL
  71. * => Traversal is aborted. $node stays as-is
  72. * * otherwise
  73. * => $node is set to the return value
  74. *
  75. * @param Node $node Node
  76. *
  77. * @return null|int|Node|Node[] Replacement node (or special return value)
  78. */
  79. public function enterNode(Node $node);
  80. /**
  81. * Called when leaving a node.
  82. *
  83. * Return value semantics:
  84. * * null
  85. * => $node stays as-is
  86. * * NodeVisitor::REMOVE_NODE
  87. * => $node is removed from the parent array
  88. * * NodeVisitor::REPLACE_WITH_NULL
  89. * => $node is replaced with null
  90. * * NodeVisitor::STOP_TRAVERSAL
  91. * => Traversal is aborted. $node stays as-is
  92. * * array (of Nodes)
  93. * => The return value is merged into the parent array (at the position of the $node)
  94. * * otherwise
  95. * => $node is set to the return value
  96. *
  97. * @param Node $node Node
  98. *
  99. * @return null|int|Node|Node[] Replacement node (or special return value)
  100. */
  101. public function leaveNode(Node $node);
  102. /**
  103. * Called once after traversal.
  104. *
  105. * Return value semantics:
  106. * * null: $nodes stays as-is
  107. * * otherwise: $nodes is set to the return value
  108. *
  109. * @param Node[] $nodes Array of nodes
  110. *
  111. * @return null|Node[] Array of nodes
  112. */
  113. public function afterTraverse(array $nodes);
  114. }