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 | /** |
||
| 27 | * Recursively find sources in definition attributes. |
||
| 28 | * |
||
| 29 | * @param string $fieldType |
||
| 30 | * @param array $attributes |
||
| 31 | * @param string $indexFrom |
||
| 32 | * @param string $indexTo |
||
| 33 | * |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | 31 | public function findSources(string $fieldType, array $attributes, string $indexFrom, string $indexTo): array |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
||
| 53 | * |
||
| 54 | * @param string $fieldType |
||
| 55 | * @param string|array $sources |
||
| 56 | * @param string $indexFrom |
||
| 57 | * @param string $indexTo |
||
| 58 | * |
||
| 59 | * @return array|string |
||
| 60 | */ |
||
| 61 | 8 | public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
||
| 77 | * |
||
| 78 | * @TODO Break up and simplify this method |
||
| 79 | * |
||
| 80 | * @param string $fieldType |
||
| 81 | * @param string $source |
||
| 82 | * @param string $indexFrom |
||
| 83 | * @param string $indexTo |
||
| 84 | * |
||
| 85 | * @return string|null |
||
| 86 | * |
||
| 87 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 88 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
| 89 | */ |
||
| 90 | 9 | public function getSource(string $fieldType, string $source = null, string $indexFrom, string $indexTo) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get a folder by id |
||
| 188 | * |
||
| 189 | * @param int $folderId |
||
| 190 | * @return object |
||
| 191 | */ |
||
| 192 | 1 | private function getFolderById(int $folderId): object |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get a folder by handle |
||
| 204 | * |
||
| 205 | * @param string $folderHandle |
||
| 206 | * @return object |
||
| 207 | */ |
||
| 208 | 1 | private function getFolderByHandle(string $folderHandle): object |
|
| 217 | } |
||
| 218 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.