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 |
||
22 | class SourcesBehavior extends Behavior |
||
23 | { |
||
24 | /** |
||
25 | * Recursively find sources in definition attributes. |
||
26 | * |
||
27 | * @param string $fieldType |
||
28 | * @param array $attributes |
||
29 | * @param string $indexFrom |
||
30 | * @param string $indexTo |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | 32 | public function findSources(string $fieldType, array $attributes, string $indexFrom, string $indexTo): array |
|
48 | |||
49 | /** |
||
50 | * Get sources based on the indexFrom attribute and return them with the indexTo attribute. |
||
51 | * |
||
52 | * @param string $fieldType |
||
53 | * @param string|array $sources |
||
54 | * @param string $indexFrom |
||
55 | * @param string $indexTo |
||
56 | * |
||
57 | * @return array|string |
||
58 | */ |
||
59 | 9 | public function getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
|
72 | |||
73 | /** |
||
74 | * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. |
||
75 | * |
||
76 | * @TODO Break up and simplify this method |
||
77 | * |
||
78 | * @param string $fieldType |
||
79 | * @param string $source |
||
80 | * @param string $indexFrom |
||
81 | * @param string $indexTo |
||
82 | * |
||
83 | * @return string|null |
||
84 | * |
||
85 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
86 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
87 | */ |
||
88 | 9 | public function getSource(string $fieldType, string $source = null, string $indexFrom, string $indexTo) |
|
167 | } |
||
168 |