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() |
|
| 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) |
|
| 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() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * compile levels array |
||
| 128 | */ |
||
| 129 | 41 | protected function compileLevels() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Compile siblings array |
||
| 160 | */ |
||
| 161 | 41 | public function compileSiblings() |
|
| 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) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 25 | public function getUnitsFromLevel($level) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | 15 | public function getRelations() |
|
| 266 | } |
||
| 267 |