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 |
||
| 25 | class SourcesBehavior extends Behavior |
||
| 26 | { |
||
| 27 | /** Hack to be able to avoid the active record call in VolumeFolder::findOne() */ |
||
| 28 | public $mockFolder = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Recursively find sources in definition attributes. |
||
| 32 | * |
||
| 33 | * @param string $fieldType |
||
| 34 | * @param array $attributes |
||
| 35 | * @param string $indexFrom |
||
| 36 | * @param string $indexTo |
||
| 37 | * |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | 32 | public function findSources(string $fieldType, array $attributes, string $indexFrom, string $indexTo): array |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
||
| 57 | * |
||
| 58 | * @param string $fieldType |
||
| 59 | * @param string|array $sources |
||
| 60 | * @param string $indexFrom |
||
| 61 | * @param string $indexTo |
||
| 62 | * |
||
| 63 | * @return array|string |
||
| 64 | */ |
||
| 65 | 9 | public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
||
| 81 | * |
||
| 82 | * @TODO Break up and simplify this method |
||
| 83 | * |
||
| 84 | * @param string $fieldType |
||
| 85 | * @param string $source |
||
| 86 | * @param string $indexFrom |
||
| 87 | * @param string $indexTo |
||
| 88 | * |
||
| 89 | * @return string|null |
||
| 90 | * |
||
| 91 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 92 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
| 93 | */ |
||
| 94 | 9 | public function getSource(string $fieldType, string $source = null, string $indexFrom, string $indexTo) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Get a folder by id |
||
| 196 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
| 197 | * |
||
| 198 | * @param int $folderId |
||
| 199 | * @return object |
||
| 200 | */ |
||
| 201 | 1 | private function getFolderById(int $folderId): \stdClass |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Get folder by volume id |
||
| 216 | * |
||
| 217 | * @param int $volumeId |
||
| 218 | * @return VolumeFolder |
||
| 219 | */ |
||
| 220 | 1 | private function getFolderByVolumeId(int $volumeId): VolumeFolder |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Set a mock folder for the tests |
||
| 227 | * |
||
| 228 | * @param VolumeFolder $mockFolder |
||
| 229 | */ |
||
| 230 | 1 | public function setMockFolder(VolumeFolder $mockFolder) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get a folder by volume handle |
||
| 237 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
| 238 | * |
||
| 239 | * @param string $folderHandle |
||
| 240 | * @return object |
||
| 241 | */ |
||
| 242 | 1 | private function getFolderByHandle(string $folderHandle): \stdClass |
|
| 256 | } |
||
| 257 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.