| Conditions | 6 |
| Paths | 4 |
| Total Lines | 29 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 16 |
| CRAP Score | 6 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 27 | 4 | public static function fromLevelOrderList(array $list, callable $nodeCreator): ?Node |
|
| 28 | { |
||
| 29 | 4 | $tailIdx = count($list) - 1; |
|
| 30 | 4 | if ($tailIdx < 0 || (0 === $tailIdx && null === $list[0])) { |
|
| 31 | 2 | return null; |
|
| 32 | } |
||
| 33 | |||
| 34 | /** @var \SplQueue<Node> $queue */ |
||
| 35 | 2 | $queue = new \SplQueue(); |
|
| 36 | |||
| 37 | 2 | $root = call_user_func($nodeCreator, $list[0]); // @phpstan-ignore argument.type |
|
| 38 | 2 | $queue->enqueue($root); |
|
| 39 | |||
| 40 | 2 | $idx = 2; // Should be index 1, but it contains the null value indicating end of children for "root" level ! |
|
| 41 | 2 | while ($idx <= $tailIdx) { |
|
| 42 | 2 | $parentNode = $queue->bottom(); // =peek() => next value to dequeue from a SplQueue! |
|
| 43 | |||
| 44 | // Append children to the current parent node until a null value is found |
||
| 45 | 2 | if (null !== $list[$idx]) { |
|
| 46 | 2 | $parentNode->children[] = $node = call_user_func($nodeCreator, $list[$idx]); |
|
| 47 | 2 | $queue->enqueue($node); |
|
| 48 | } else { |
||
| 49 | // Drop current parent node as there is no more children to attach |
||
| 50 | 2 | $queue->dequeue(); |
|
| 51 | } |
||
| 52 | 2 | ++$idx; |
|
| 53 | } |
||
| 54 | |||
| 55 | 2 | return $root; |
|
| 56 | } |
||
| 58 |