Complex classes like SimpleBag 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SimpleBag, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class SimpleBag implements UnitBagInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var UnitInterface[] |
||
| 9 | */ |
||
| 10 | protected $units; |
||
| 11 | /** |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $levels = []; |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $children = []; |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $siblings = []; |
||
| 23 | /** |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | private $compiled = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | 41 | public function getIterator() |
|
| 32 | { |
||
| 33 | 41 | $units = array_values($this->units); |
|
| 34 | // parent units should go first |
||
| 35 | usort($units, function (UnitInterface $unit1, UnitInterface $unit2) use ($units) { |
||
| 36 | 25 | $parent1 = $unit1->getParent(); |
|
| 37 | 25 | $parent2 = $unit2->getParent(); |
|
| 38 | 25 | if ($parent1 && !$parent2) { |
|
| 39 | 6 | return 1; |
|
| 40 | 24 | } elseif(!$parent1 && $parent2) { |
|
|
|
|||
| 41 | 18 | return -1; |
|
| 42 | } else { |
||
| 43 | 12 | if ($parent1 == $unit2) { |
|
| 44 | return 1; |
||
| 45 | 12 | } elseif ($parent2 == $unit1) { |
|
| 46 | 1 | return -1; |
|
| 47 | } |
||
| 48 | } |
||
| 49 | 11 | $ind1 = array_search($unit1, $units); |
|
| 50 | 11 | $ind2 = array_search($unit2, $units); |
|
| 51 | 11 | if ($ind1 > $ind2) { |
|
| 52 | return 1; |
||
| 53 | 11 | } elseif ($ind1 < $ind2) { |
|
| 54 | 11 | return -1; |
|
| 55 | } |
||
| 56 | return 0; |
||
| 57 | 41 | }); |
|
| 58 | 41 | return new \ArrayIterator($units); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 15 | public function count() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 55 | public function add(UnitInterface $unit) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param array $units |
||
| 79 | */ |
||
| 80 | 15 | public function addSet(array $units) |
|
| 81 | { |
||
| 82 | 15 | foreach ($units as $unit) { |
|
| 83 | 15 | $this->add($unit); |
|
| 84 | } |
||
| 85 | 15 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 26 | public function getUnitByCode($code) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 41 | public function compileTree() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * compile children array |
||
| 114 | */ |
||
| 115 | 41 | protected function compileChildren() |
|
| 116 | { |
||
| 117 | 41 | foreach ($this->units as $parent) { |
|
| 118 | 41 | foreach ($this->units as $unit) { |
|
| 119 | 41 | if (($innerP = $unit->getParent()) && $innerP->getCode() == $parent->getCode()) { |
|
| 120 | 41 | $this->children[$parent->getCode()][] = $unit; |
|
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | 41 | } |
|
| 125 | |||
| 126 | /** |
||
| 127 | * compile levels array |
||
| 128 | */ |
||
| 129 | 41 | protected function compileLevels() |
|
| 130 | { |
||
| 131 | 41 | foreach ($this->units as $unit) { |
|
| 132 | 41 | $toCheck = clone $unit; |
|
| 133 | 41 | $parentLevel = 1; |
|
| 134 | 41 | while (($parent = $toCheck->getParent()) !== null) { |
|
| 135 | 29 | $parentLevel++; |
|
| 136 | 29 | $toCheck = $parent; |
|
| 137 | } |
||
| 138 | 41 | $siblingsLevels = []; |
|
| 139 | 41 | $i = 0; |
|
| 140 | 41 | $siblings = $toCheck->getSiblings(); |
|
| 141 | 41 | foreach ($siblings as $siblingUnit) { |
|
| 142 | 17 | $siblingsLevels[$i] = 1; |
|
| 143 | 17 | while (($parent = $siblingUnit->getParent()) !== null) { |
|
| 144 | 10 | $siblingsLevels[$i]++; |
|
| 145 | 10 | $siblingUnit = $parent; |
|
| 146 | } |
||
| 147 | 17 | $i++; |
|
| 148 | } |
||
| 149 | 41 | if (count($siblingsLevels)) { |
|
| 150 | 17 | $level = max(max($siblingsLevels), $parentLevel); |
|
| 151 | } else { |
||
| 152 | 30 | $level = $parentLevel; |
|
| 153 | } |
||
| 154 | 41 | $this->levels[$level][] = $unit->getCode(); |
|
| 155 | } |
||
| 156 | 41 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Compile siblings array |
||
| 160 | */ |
||
| 161 | 41 | public function compileSiblings() |
|
| 162 | { |
||
| 163 | 41 | foreach ($this->units as $unit) { |
|
| 164 | 41 | $siblings = $unit->getSiblings(); |
|
| 165 | 41 | if (!empty($siblings)) { |
|
| 166 | 17 | $mergedSet = array_merge([$unit->getCode() => $unit], $siblings); |
|
| 167 | 17 | if (!in_array($mergedSet, $this->siblings)) { |
|
| 168 | 41 | $this->siblings[] = $mergedSet; |
|
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | 41 | } |
|
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 17 | public function isLowest($code) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | 22 | public function getChildren($code) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | 24 | public function getLowestLevel() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | 28 | public function getUnitLevel($code) |
|
| 202 | { |
||
| 203 | 28 | foreach ($this->levels as $level => $codes) { |
|
| 204 | 25 | if (in_array($code, $codes)) { |
|
| 205 | 25 | return $level; |
|
| 206 | } |
||
| 207 | } |
||
| 208 | 3 | return 1; |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 25 | public function getUnitsFromLevel($level) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | 15 | public function getRelations() |
|
| 223 | { |
||
| 224 | // from parent-child |
||
| 266 | } |
||
| 267 |