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) |
|
91 | { |
||
92 | 9 | if (false === strpos($source, ':')) { |
|
93 | 5 | return $source; |
|
94 | } |
||
95 | |||
96 | /** @var Model $sourceObject */ |
||
97 | 9 | $sourceObject = null; |
|
98 | |||
99 | // Get service and method by source |
||
100 | 9 | list($sourceType, $sourceFrom) = explode(':', $source); |
|
101 | 9 | switch ($sourceType) { |
|
102 | 9 | case 'editSite': |
|
103 | $service = Craft::$app->sites; |
||
104 | $method = 'getSiteBy'; |
||
105 | break; |
||
106 | 9 | case 'single': |
|
107 | 9 | case 'section': |
|
108 | 9 | case 'createEntries': |
|
109 | 9 | case 'editPeerEntries': |
|
110 | 9 | case 'deleteEntries': |
|
111 | 9 | case 'deletePeerEntries': |
|
112 | 9 | case 'deletePeerEntryDrafts': |
|
113 | 9 | case 'editEntries': |
|
114 | 9 | case 'editPeerEntryDrafts': |
|
115 | 9 | case 'publishEntries': |
|
116 | 9 | case 'publishPeerEntries': |
|
117 | 9 | case 'publishPeerEntryDrafts': |
|
118 | 5 | $service = Craft::$app->sections; |
|
119 | 5 | $method = 'getSectionBy'; |
|
120 | 5 | break; |
|
121 | 9 | case 'group': |
|
122 | 7 | case 'editCategories': |
|
123 | 5 | $service = 'Users' == $fieldType ? Craft::$app->userGroups : Craft::$app->categories; |
|
124 | 5 | $method = 'getGroupBy'; |
|
125 | 5 | break; |
|
126 | 7 | case 'folder': |
|
127 | 2 | $service = $this; |
|
128 | 2 | $method = 'getFolderBy'; |
|
129 | 2 | break; |
|
130 | 5 | case 'createFoldersInVolume': |
|
131 | 5 | case 'deleteFilesAndFoldersInVolume': |
|
132 | 5 | case 'saveAssetInVolume': |
|
133 | 5 | case 'viewVolume': |
|
134 | 3 | $service = Craft::$app->volumes; |
|
135 | 3 | $method = 'getVolumeBy'; |
|
136 | 3 | break; |
|
137 | 2 | case 'taggroup': |
|
138 | $service = Craft::$app->tags; |
||
139 | $method = 'getTagGroupBy'; |
||
140 | break; |
||
141 | 2 | case 'field': |
|
142 | 2 | $service = Craft::$app->fields; |
|
143 | 2 | $method = 'getFieldBy'; |
|
144 | 2 | break; |
|
145 | case 'editGlobalSet': |
||
146 | $service = Craft::$app->globals; |
||
147 | $method = 'getSetBy'; |
||
148 | break; |
||
149 | case 'utility': |
||
150 | return $source; |
||
151 | } |
||
152 | |||
153 | // Send event |
||
154 | 9 | $plugin = Craft::$app->controller->module; |
|
155 | 9 | $event = new SourceMappingEvent([ |
|
156 | 9 | 'source' => $source, |
|
157 | 9 | 'service' => $service ?? null, |
|
158 | 9 | 'method' => $method ?? null, |
|
159 | ]); |
||
160 | 9 | $plugin->trigger($plugin::EVENT_MAP_SOURCE, $event); |
|
161 | 9 | $service = $event->service; |
|
162 | 9 | $method = $event->method; |
|
163 | |||
164 | // Try service and method |
||
165 | 9 | if (isset($service) && isset($method) && isset($sourceFrom)) { |
|
166 | 9 | $method = $method.ucfirst($indexFrom); |
|
167 | try { |
||
168 | 9 | $sourceObject = $service->$method($sourceFrom); |
|
|
|||
169 | 2 | } catch (TypeError $e) { |
|
170 | Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo); |
||
171 | Schematic::error($e->getMessage()); |
||
172 | |||
173 | return null; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | 7 | if ($sourceObject) { |
|
178 | 6 | return $sourceType.':'.$sourceObject->$indexTo; |
|
179 | } |
||
180 | |||
181 | 1 | Schematic::warning('No mapping found for source '.$source); |
|
182 | |||
183 | 1 | return null; |
|
184 | } |
||
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.