| Total Complexity | 43 |
| Total Lines | 236 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Traverser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Traverser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | final class Traverser |
||
| 22 | { |
||
| 23 | /** @var VisitorInterface[] */ |
||
| 24 | private $visitors = []; |
||
| 25 | |||
| 26 | /** @var bool */ |
||
| 27 | private $stopTraversal = false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param array $visitors |
||
| 31 | */ |
||
| 32 | public function __construct(array $visitors = []) |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Adds visitor. |
||
| 41 | * |
||
| 42 | * @param VisitorInterface $visitor |
||
| 43 | */ |
||
| 44 | public function addVisitor(VisitorInterface $visitor): void |
||
| 45 | { |
||
| 46 | $this->visitors[] = $visitor; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param VisitorInterface $visitor |
||
| 51 | */ |
||
| 52 | public function removeVisitor(VisitorInterface $visitor): void |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Traverses an array of nodes using added visitors. |
||
| 64 | * |
||
| 65 | * @param NodeInterface[] $nodes |
||
| 66 | * @param VisitorContext $context |
||
| 67 | * @return NodeInterface[] |
||
| 68 | * |
||
| 69 | * @throws \Throwable |
||
| 70 | */ |
||
| 71 | public function traverse(array $nodes, VisitorContext $context = null): array |
||
| 72 | { |
||
| 73 | $context = $context ?? new VisitorContext(); |
||
| 74 | |||
| 75 | $ctx = clone $context; |
||
| 76 | foreach ($nodes as $index => $node) { |
||
| 77 | if ($this->stopTraversal) { |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | |||
| 81 | $traverseChildren = true; |
||
| 82 | $breakVisitorID = null; |
||
| 83 | |||
| 84 | if ($node instanceof NodeInterface) { |
||
| 85 | $ctx = $context->withNode($node); |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach ($this->visitors as $visitorID => $visitor) { |
||
| 89 | $result = $visitor->enterNode($node, $ctx); |
||
| 90 | |||
| 91 | switch (true) { |
||
| 92 | case $result === null: |
||
| 93 | break; |
||
| 94 | |||
| 95 | case $result instanceof NodeInterface: |
||
| 96 | $node = $result; |
||
| 97 | break; |
||
| 98 | |||
| 99 | case $result === VisitorInterface::DONT_TRAVERSE_CHILDREN: |
||
| 100 | $traverseChildren = false; |
||
| 101 | break; |
||
| 102 | |||
| 103 | case $result === VisitorInterface::DONT_TRAVERSE_CURRENT_AND_CHILDREN: |
||
| 104 | $traverseChildren = false; |
||
| 105 | $breakVisitorID = $visitorID; |
||
| 106 | break 2; |
||
| 107 | |||
| 108 | case $result === VisitorInterface::STOP_TRAVERSAL: |
||
| 109 | $this->stopTraversal = true; |
||
| 110 | |||
| 111 | break 3; |
||
| 112 | |||
| 113 | default: |
||
| 114 | throw new \LogicException( |
||
| 115 | 'enterNode() returned invalid value of type ' . gettype($result) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // sub nodes |
||
| 121 | if ($traverseChildren && $node instanceof NodeInterface) { |
||
| 122 | $nodes[$index] = $this->traverseNode($node, $ctx); |
||
| 123 | if ($this->stopTraversal) { |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | foreach ($this->visitors as $visitorID => $visitor) { |
||
| 129 | $result = $visitor->leaveNode($node, $ctx); |
||
| 130 | |||
| 131 | switch (true) { |
||
| 132 | case $result === null: |
||
| 133 | break; |
||
| 134 | |||
| 135 | case $result instanceof NodeInterface: |
||
| 136 | $nodes[$index] = $result; |
||
| 137 | break; |
||
| 138 | |||
| 139 | case $result === VisitorInterface::REMOVE_NODE: |
||
| 140 | unset($nodes[$index]); |
||
| 141 | break; |
||
| 142 | |||
| 143 | case $result === VisitorInterface::STOP_TRAVERSAL: |
||
| 144 | $this->stopTraversal = true; |
||
| 145 | break 3; |
||
| 146 | |||
| 147 | default: |
||
| 148 | throw new \LogicException( |
||
| 149 | 'leaveNode() returned invalid value of type ' . gettype($result) |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($breakVisitorID === $visitorID) { |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return array_values($nodes); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Recursively traverse a node. |
||
| 164 | * |
||
| 165 | * @param NodeInterface $node |
||
| 166 | * @param VisitorContext $context |
||
| 167 | * @return NodeInterface |
||
| 168 | */ |
||
| 169 | private function traverseNode(NodeInterface $node, VisitorContext $context): NodeInterface |
||
| 257 | } |
||
| 258 | } |
||
| 259 |