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 | 31 | 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 | 8 | 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 | 8 | public function getSource(string $fieldType, string $source = null, string $indexFrom, string $indexTo) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get a folder by id |
||
| 191 | * |
||
| 192 | * @param int $folderId |
||
| 193 | * @return object |
||
| 194 | */ |
||
| 195 | 1 | private function getFolderById(int $folderId): object |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get folder by volume id |
||
| 210 | * |
||
| 211 | * @param int $volumeId |
||
| 212 | * @return VolumeFolder |
||
| 213 | */ |
||
| 214 | private function getFolderByVolumeId(int $volumeId): VolumeFolder |
||
| 218 | |||
| 219 | 1 | public function setMockFolder(VolumeFolder $mockFolder): void |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get a folder by volume handle |
||
| 226 | * |
||
| 227 | * @param string $folderHandle |
||
| 228 | * @return object |
||
| 229 | */ |
||
| 230 | private function getFolderByHandle(string $folderHandle): object |
||
| 244 | } |
||
| 245 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.