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 | public function __construct(int $dimension) |
||
27 | |||
28 | public function toArray(): array |
||
39 | |||
40 | /** |
||
41 | * @param mixed $label |
||
42 | */ |
||
43 | public function newPoint(array $coordinates, $label = null): Point |
||
51 | |||
52 | /** |
||
53 | * @param mixed $label |
||
54 | * @param mixed $data |
||
55 | */ |
||
56 | public function addPoint(array $coordinates, $label = null, $data = null): void |
||
60 | |||
61 | /** |
||
62 | * @param object $point |
||
63 | * @param mixed $data |
||
64 | */ |
||
65 | public function attach($point, $data = null): void |
||
73 | |||
74 | public function getDimension(): int |
||
78 | |||
79 | /** |
||
80 | * @return array|bool |
||
81 | */ |
||
82 | public function getBoundaries() |
||
106 | |||
107 | public function getRandomPoint(Point $min, Point $max): Point |
||
117 | |||
118 | /** |
||
119 | * @return array|Cluster[] |
||
120 | */ |
||
121 | public function cluster(int $clustersNumber, int $initMethod = KMeans::INIT_RANDOM): array |
||
130 | |||
131 | /** |
||
132 | * @return array|Cluster[] |
||
133 | */ |
||
134 | protected function initializeClusters(int $clustersNumber, int $initMethod): array |
||
155 | |||
156 | /** |
||
157 | * @param Cluster[] $clusters |
||
158 | */ |
||
159 | protected function iterate(array $clusters): bool |
||
198 | |||
199 | /** |
||
200 | * @return Cluster[] |
||
201 | */ |
||
202 | protected function initializeKMPPClusters(int $clustersNumber): array |
||
244 | |||
245 | /** |
||
246 | * @return Cluster[] |
||
247 | */ |
||
248 | private function initializeRandomClusters(int $clustersNumber): array |
||
259 | } |
||
260 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.