| Conditions | 16 |
| Paths | 29 |
| Total Lines | 48 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 14 | public function mergeNodeCollections(NodeCollection $nodesA, NodeCollection $nodesB): NodeCollection |
||
| 15 | { |
||
| 16 | foreach ($nodesB as $node) { |
||
| 17 | if (!$nodesA->hasNode($node->identifier())) { |
||
| 18 | $nodesA->add($node); |
||
| 19 | continue; |
||
| 20 | } |
||
| 21 | |||
| 22 | if ($nodesA->getNode($node->identifier())->hasValue() && $nodesB->getNode($node->identifier())->hasValue()) { |
||
| 23 | throw new \RuntimeException('Overwrite assignment!'); |
||
| 24 | } |
||
| 25 | |||
| 26 | if ($nodesA->getNode($node->identifier())->hasChildren() && $nodesB->getNode($node->identifier())->hasChildren()) { |
||
| 27 | $col = $this->mergeNodeCollections($nodesA->getNode($node->identifier())->children(), $nodesB->getNode($node->identifier())->children()); |
||
| 28 | |||
| 29 | $value = ''; |
||
| 30 | $operator = null; |
||
| 31 | |||
| 32 | if ($nodesA->getNode($node->identifier())->value() !== null) { |
||
| 33 | $value = $nodesA->getNode($node->identifier())->value(); |
||
| 34 | } elseif ($nodesB->getNode($node->identifier())->value() !== null) { |
||
| 35 | $value = $nodesB->getNode($node->identifier())->value(); |
||
| 36 | } |
||
| 37 | |||
| 38 | if ($nodesA->getNode($node->identifier())->operator() !== null) { |
||
| 39 | $operator = $nodesA->getNode($node->identifier())->operator(); |
||
| 40 | } elseif ($nodesB->getNode($node->identifier())->value() !== null) { |
||
| 41 | $operator = $nodesB->getNode($node->identifier())->operator(); |
||
| 42 | } |
||
| 43 | |||
| 44 | $subNode = new Node($node->identifier(), $value, $operator); |
||
| 45 | $subNode->updateCollection($col); |
||
| 46 | $nodesA->add($subNode); |
||
| 47 | } elseif ($nodesA->getNode($node->identifier())->hasValue() && $nodesB->getNode($node->identifier())->hasChildren()) { |
||
| 48 | $nodesA->getNode($node->identifier())->updateCollection($nodesB->getNode($node->identifier())->children()); |
||
| 49 | } elseif ($nodesA->getNode($node->identifier())->hasChildren() && $nodesB->getNode($node->identifier())->hasValue()) { |
||
| 50 | $newNode = new Node($node->identifier(), $nodesB->getNode($node->identifier())->value(), $nodesB->getNode($node->identifier())->operator()); |
||
| 51 | $newNode->updateCollection($nodesA->getNode($node->identifier())->children()); |
||
| 52 | $nodesA->add($newNode); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | // Update node levels for whole tree |
||
| 57 | foreach ($nodesA as $node) { |
||
| 58 | $node->updateLevel(0); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $nodesA; |
||
| 62 | } |
||
| 157 |