Complex classes like SourcesBehavior 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 SourcesBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class SourcesBehavior extends Behavior |
||
25 | { |
||
26 | /** Hack to be able to avoid the active record call in VolumeFolder::findOne() */ |
||
27 | public $mockFolder = null; |
||
28 | |||
29 | /** |
||
30 | * Recursively find sources in definition attributes. |
||
31 | * |
||
32 | * @param string $fieldType |
||
33 | * @param array $attributes |
||
34 | * @param string $indexFrom |
||
35 | * @param string $indexTo |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | 32 | public function findSources(string $fieldType, array $attributes, string $indexFrom, string $indexTo): array |
|
53 | |||
54 | /** |
||
55 | * Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
||
56 | * |
||
57 | * @param string $fieldType |
||
58 | * @param string|array $sources |
||
59 | * @param string $indexFrom |
||
60 | * @param string $indexTo |
||
61 | * |
||
62 | * @return array|string |
||
63 | */ |
||
64 | 9 | public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
|
77 | |||
78 | /** |
||
79 | * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
||
80 | * |
||
81 | * @TODO Break up and simplify this method |
||
82 | * |
||
83 | * @param string $fieldType |
||
84 | * @param string $source |
||
85 | * @param string $indexFrom |
||
86 | * @param string $indexTo |
||
87 | * |
||
88 | * @return string|null |
||
89 | * |
||
90 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
91 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
92 | */ |
||
93 | 9 | public function getSource(string $fieldType, string $source = null, string $indexFrom, string $indexTo) |
|
188 | |||
189 | /** |
||
190 | * Get a folder by id |
||
191 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
192 | * |
||
193 | * @param int $folderId |
||
194 | * @return object |
||
195 | */ |
||
196 | 1 | private function getFolderById(int $folderId): \stdClass |
|
208 | |||
209 | /** |
||
210 | * Get folder by volume id |
||
211 | * |
||
212 | * @param int $volumeId |
||
213 | * @return VolumeFolder |
||
214 | */ |
||
215 | 1 | private function getFolderByVolumeId(int $volumeId): VolumeFolder |
|
219 | |||
220 | /** |
||
221 | * Set a mock folder for the tests |
||
222 | * |
||
223 | * @param VolumeFolder $mockFolder |
||
224 | */ |
||
225 | 1 | public function setMockFolder(VolumeFolder $mockFolder) |
|
229 | |||
230 | /** |
||
231 | * Get a folder by volume handle |
||
232 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
233 | * |
||
234 | * @param string $folderHandle |
||
235 | * @return object |
||
236 | */ |
||
237 | 1 | private function getFolderByHandle(string $folderHandle): \stdClass |
|
251 | } |
||
252 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.