Complex classes like Space 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 Space, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Space extends SplObjectStorage |
||
13 | { |
||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | protected $dimension; |
||
18 | |||
19 | /** |
||
20 | * @param $dimension |
||
21 | */ |
||
22 | public function __construct($dimension) |
||
30 | |||
31 | /** |
||
32 | * @return array |
||
33 | */ |
||
34 | public function toArray() |
||
43 | |||
44 | /** |
||
45 | * @param array $coordinates |
||
46 | * |
||
47 | * @return Point |
||
48 | */ |
||
49 | public function newPoint(array $coordinates) |
||
57 | |||
58 | /** |
||
59 | * @param array $coordinates |
||
60 | * @param null $data |
||
61 | */ |
||
62 | public function addPoint(array $coordinates, $data = null) |
||
66 | |||
67 | /** |
||
68 | * @param object $point |
||
69 | * @param null $data |
||
70 | */ |
||
71 | public function attach($point, $data = null) |
||
79 | |||
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
83 | public function getDimension() |
||
87 | |||
88 | /** |
||
89 | * @return array|bool |
||
90 | */ |
||
91 | public function getBoundaries() |
||
109 | |||
110 | /** |
||
111 | * @param Point $min |
||
112 | * @param Point $max |
||
113 | * |
||
114 | * @return Point |
||
115 | */ |
||
116 | public function getRandomPoint(Point $min, Point $max) |
||
126 | |||
127 | /** |
||
128 | * @param int $clustersNumber |
||
129 | * @param int $initMethod |
||
130 | * |
||
131 | * @return array|Cluster[] |
||
132 | */ |
||
133 | public function cluster(int $clustersNumber, int $initMethod = KMeans::INIT_RANDOM) |
||
142 | |||
143 | /** |
||
144 | * @param $clustersNumber |
||
145 | * @param $initMethod |
||
146 | * |
||
147 | * @return array|Cluster[] |
||
148 | */ |
||
149 | protected function initializeClusters(int $clustersNumber, int $initMethod) |
||
164 | |||
165 | /** |
||
166 | * @param $clusters |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | protected function iterate($clusters) |
||
207 | |||
208 | /** |
||
209 | * @param int $clustersNumber |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | private function initializeRandomClusters(int $clustersNumber) |
||
224 | |||
225 | /** |
||
226 | * @param int $clustersNumber |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function initializeKMPPClusters(int $clustersNumber) |
||
259 | } |
||
260 |
This check looks for
do
loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.