Complex classes like QuadTree 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 QuadTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class QuadTree |
||
| 8 | { |
||
| 9 | const Q_TOP_LEFT = 0; |
||
| 10 | const Q_TOP_RIGHT = 1; |
||
| 11 | const Q_BOTTOM_LEFT = 2; |
||
| 12 | const Q_BOTTOM_RIGHT = 3; |
||
| 13 | |||
| 14 | /** @var int */ |
||
| 15 | protected $level; |
||
| 16 | /** @var int */ |
||
| 17 | protected $maxObjects; |
||
| 18 | /** @var int */ |
||
| 19 | protected $maxLevels; |
||
| 20 | /** @var ArrayCollection */ |
||
| 21 | protected $objects; |
||
| 22 | /** @var \SixtyNine\DataTypes\Box */ |
||
| 23 | protected $bounds; |
||
| 24 | /** @var bool */ |
||
| 25 | protected $isSplit = false; |
||
| 26 | /** @var array QuadTree[] */ |
||
| 27 | protected $nodes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param Box $bounds |
||
| 31 | * @param int $level |
||
| 32 | * @param int $maxObjects |
||
| 33 | * @param int $maxLevels |
||
| 34 | */ |
||
| 35 | 20 | public function __construct(Box $bounds, $level = 0, $maxObjects = 10, $maxLevels = 10) |
|
| 43 | |||
| 44 | 7 | public function split() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param Box $box |
||
| 63 | * @return int |
||
| 64 | */ |
||
| 65 | 17 | public function getIndex(Box $box) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param Box $box |
||
| 92 | */ |
||
| 93 | 7 | public function insert(Box $box) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @return int |
||
| 126 | */ |
||
| 127 | 3 | public function count() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param Box $box |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | 2 | public function retrieve(Box $box) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param Box $box |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | 2 | public function collides(Box $box) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | * @codeCoverageIgnore |
||
| 200 | */ |
||
| 201 | public function __toString() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | 2 | public function getAllObjects() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return Box |
||
| 245 | */ |
||
| 246 | 3 | public function getBounds() |
|
| 250 | } |
||
| 251 |