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 bool |
||
64 | */ |
||
65 | 17 | protected function isInTopQuadrant(Box $box) |
|
70 | |||
71 | /** |
||
72 | * @param Box $box |
||
73 | * @return bool |
||
74 | */ |
||
75 | 17 | protected function isInBottomQuadrant(Box $box) |
|
80 | |||
81 | /** |
||
82 | * @param Box $box |
||
83 | * @return bool |
||
84 | */ |
||
85 | 17 | protected function isInLeftQuadrant(Box $box) |
|
90 | |||
91 | /** |
||
92 | * @param Box $box |
||
93 | * @return bool |
||
94 | */ |
||
95 | 17 | protected function isInRightQuadrant(Box $box) |
|
100 | |||
101 | /** |
||
102 | * @param Box $box |
||
103 | * @return int |
||
104 | */ |
||
105 | 17 | public function getIndex(Box $box) |
|
125 | |||
126 | /** |
||
127 | * @param Box $box |
||
128 | */ |
||
129 | 7 | public function insert(Box $box) |
|
159 | |||
160 | /** |
||
161 | * @return int |
||
162 | */ |
||
163 | 3 | public function count() |
|
176 | |||
177 | /** |
||
178 | * @param Box $box |
||
179 | * @return array |
||
180 | */ |
||
181 | 2 | public function retrieve(Box $box) |
|
205 | |||
206 | /** |
||
207 | * @param Box $box |
||
208 | * @return bool |
||
209 | */ |
||
210 | 2 | public function collides(Box $box) |
|
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | * @codeCoverageIgnore |
||
236 | */ |
||
237 | public function __toString() |
||
259 | |||
260 | /** |
||
261 | * @return array |
||
262 | */ |
||
263 | 2 | public function getAllObjects() |
|
278 | |||
279 | /** |
||
280 | * @return Box |
||
281 | */ |
||
282 | 3 | public function getBounds() |
|
286 | } |
||
287 |