| Total Complexity | 41 |
| Total Lines | 194 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Tree extends Node |
||
| 14 | { |
||
| 15 | public $root; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * undocumented function. |
||
| 19 | * |
||
| 20 | * @param $intervals array of Interval |
||
| 21 | * |
||
| 22 | * @return void |
||
| 23 | */ |
||
| 24 | public function __construct(array $intervals = [], bool $preSorted = false) |
||
| 25 | { |
||
| 26 | !$preSorted && usort($intervals, function (IntervalInterface $i0, IntervalInterface $i) { |
||
| 27 | return $i0->getStart() <=> $i->getStart(); |
||
| 28 | }); |
||
| 29 | $this->root = $this->toTree($intervals); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * An augmented tree can be built from a simple ordered tree, |
||
| 34 | * for example a binary search tree or self-balancing binary search tree, |
||
| 35 | * ordered by the 'low' values of the intervals. |
||
| 36 | */ |
||
| 37 | public function toTree(array $intervals) |
||
| 38 | { |
||
| 39 | $max = 0; |
||
| 40 | $len = count($intervals); |
||
| 41 | $mid = (int) floor($len / 2); |
||
| 42 | |||
| 43 | if (!$len) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | array_walk($intervals, function (IntervalInterface $el) use (&$max) { |
||
| 48 | if ($max < $el->getEnd()) { |
||
| 49 | $max = $el->getEnd(); |
||
| 50 | } |
||
| 51 | }); |
||
| 52 | $l_intervals = array_splice($intervals, 0, $mid); |
||
| 53 | $r_intervals = array_splice($intervals, -$mid); |
||
| 54 | $interval = $intervals ? reset($intervals) : array_pop($l_intervals); |
||
| 55 | $interval = $interval ?? array_pop($r_intervals); |
||
| 56 | |||
| 57 | $node = new Node( |
||
| 58 | $interval, |
||
| 59 | $this->toTree($l_intervals), |
||
| 60 | $this->toTree($r_intervals), |
||
| 61 | $max |
||
| 62 | ); |
||
| 63 | |||
| 64 | $node->root = &$this; |
||
| 65 | |||
| 66 | return $node; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * returns intervals that exclusively fall within range given |
||
| 71 | * to retrieve eather bound inclusevely, just modify parameters. |
||
| 72 | * |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public function select(int $low, int $high): array |
||
| 76 | { |
||
| 77 | return $this->root ? $this->root->select($low, $high) : []; |
||
|
|
|||
| 78 | } |
||
| 79 | |||
| 80 | public function selectNode(int $low, int $high): array |
||
| 81 | { |
||
| 82 | return $this->root ? $this->root->selectNode($low, $high) : []; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * rebalance messed up tree overall. |
||
| 87 | */ |
||
| 88 | public function balance() |
||
| 89 | { |
||
| 90 | // todo |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * understandable var dump. |
||
| 95 | */ |
||
| 96 | public function __debugInfo() |
||
| 97 | { |
||
| 98 | $arr = $this->all(); |
||
| 99 | |||
| 100 | usort($arr, function (IntervalInterface $i0, IntervalInterface $i) { |
||
| 101 | return $i0->getStart() <=> $i->getStart(); |
||
| 102 | }); |
||
| 103 | |||
| 104 | return $arr; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function yieldInterSelect(int $low, int $high) |
||
| 108 | { |
||
| 109 | return $this->root ? $this->root->yieldInterSelect($low, $high) : []; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function interSelectNode(int $low, int $high): array |
||
| 115 | } |
||
| 116 | |||
| 117 | public function interSelect(int $low, int $high): array |
||
| 118 | { |
||
| 119 | return $this->root ? $this->root->interSelect($low, $high) : []; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function yieldSelect(int $low, int $high) |
||
| 123 | { |
||
| 124 | return $this->root ? $this->root->yieldSelect($low, $high) : []; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function all(): array |
||
| 131 | { |
||
| 132 | return $this->root ? $this->root->all() : []; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * undocumented function. |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function yieldAll() |
||
| 143 | } |
||
| 144 | |||
| 145 | public function removeRegion(int $low, int $high) |
||
| 146 | { |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | public function remove(IntervalInterface $i) |
||
| 197 | } |
||
| 198 | |||
| 199 | public function add(IntervalInterface $i) |
||
| 207 | } |
||
| 208 | } |
||
| 209 |