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