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) |
|
95 | { |
||
96 | 9 | if (false === strpos($source, ':')) { |
|
97 | 7 | return $source; |
|
98 | } |
||
99 | |||
100 | /** @var Model $sourceObject */ |
||
101 | 9 | $sourceObject = null; |
|
102 | |||
103 | // Get service and method by source |
||
104 | 9 | list($sourceType, $sourceFrom) = explode(':', $source); |
|
105 | 9 | switch ($sourceType) { |
|
106 | 9 | case 'editSite': |
|
107 | $service = Craft::$app->sites; |
||
108 | $method = 'getSiteBy'; |
||
109 | break; |
||
110 | 9 | case 'single': |
|
111 | 9 | case 'section': |
|
112 | 9 | case 'createEntries': |
|
113 | 9 | case 'editPeerEntries': |
|
114 | 9 | case 'deleteEntries': |
|
115 | 9 | case 'deletePeerEntries': |
|
116 | 9 | case 'deletePeerEntryDrafts': |
|
117 | 9 | case 'editEntries': |
|
118 | 9 | case 'editPeerEntryDrafts': |
|
119 | 9 | case 'publishEntries': |
|
120 | 9 | case 'publishPeerEntries': |
|
121 | 9 | case 'publishPeerEntryDrafts': |
|
122 | 5 | $service = Craft::$app->sections; |
|
123 | 5 | $method = 'getSectionBy'; |
|
124 | 5 | break; |
|
125 | 9 | case 'group': |
|
126 | 7 | case 'editCategories': |
|
127 | 5 | $service = Users::class == $fieldType ? Craft::$app->userGroups : Craft::$app->categories; |
|
128 | 5 | $method = 'getGroupBy'; |
|
129 | 5 | break; |
|
130 | 7 | case 'folder': |
|
131 | 2 | $service = $this; |
|
132 | 2 | $method = 'getFolderBy'; |
|
133 | 2 | break; |
|
134 | 5 | case 'createFoldersInVolume': |
|
135 | 5 | case 'deleteFilesAndFoldersInVolume': |
|
136 | 5 | case 'saveAssetInVolume': |
|
137 | 5 | case 'viewVolume': |
|
138 | 3 | $service = Craft::$app->volumes; |
|
139 | 3 | $method = 'getVolumeBy'; |
|
140 | 3 | break; |
|
141 | 2 | case 'taggroup': |
|
142 | $service = Craft::$app->tags; |
||
143 | $method = 'getTagGroupBy'; |
||
144 | break; |
||
145 | 2 | case 'field': |
|
146 | 2 | $service = Craft::$app->fields; |
|
147 | 2 | $method = 'getFieldBy'; |
|
148 | 2 | break; |
|
149 | case 'editGlobalSet': |
||
150 | $service = Craft::$app->globals; |
||
151 | $method = 'getSetBy'; |
||
152 | break; |
||
153 | case 'utility': |
||
154 | return $source; |
||
155 | } |
||
156 | |||
157 | // Send event |
||
158 | 9 | $plugin = Craft::$app->controller->module; |
|
159 | 9 | $event = new SourceMappingEvent([ |
|
160 | 9 | 'source' => $source, |
|
161 | 9 | 'service' => $service ?? null, |
|
162 | 9 | 'method' => $method ?? null, |
|
163 | ]); |
||
164 | 9 | $plugin->trigger($plugin::EVENT_MAP_SOURCE, $event); |
|
165 | 9 | $service = $event->service; |
|
166 | 9 | $method = $event->method; |
|
167 | |||
168 | // Try service and method |
||
169 | 9 | if (isset($service) && isset($method) && isset($sourceFrom)) { |
|
170 | 9 | $method = $method.ucfirst($indexFrom); |
|
171 | try { |
||
172 | 9 | $sourceObject = $service->$method($sourceFrom); |
|
|
|||
173 | } catch (TypeError $e) { |
||
174 | Schematic::error('An error occured mapping source '.$source.' from '.$indexFrom.' to '.$indexTo); |
||
175 | Schematic::error($e->getMessage()); |
||
176 | |||
177 | return null; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | 9 | if ($sourceObject) { |
|
182 | 8 | return $sourceType.':'.$sourceObject->$indexTo; |
|
183 | } |
||
184 | |||
185 | 1 | Schematic::warning('No mapping found for source '.$source); |
|
186 | |||
187 | 1 | return null; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get a folder by id |
||
192 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
193 | * |
||
194 | * @param int $folderId |
||
195 | * @return object |
||
196 | */ |
||
197 | 1 | private function getFolderById(int $folderId): \stdClass |
|
209 | |||
210 | /** |
||
211 | * Get folder by volume id |
||
212 | * |
||
213 | * @param int $volumeId |
||
214 | * @return VolumeFolder |
||
215 | */ |
||
216 | 1 | private function getFolderByVolumeId(int $volumeId): VolumeFolder |
|
220 | |||
221 | /** |
||
222 | * Set a mock folder for the tests |
||
223 | * |
||
224 | * @param VolumeFolder $mockFolder |
||
225 | */ |
||
226 | 1 | public function setMockFolder(VolumeFolder $mockFolder) |
|
230 | |||
231 | /** |
||
232 | * Get a folder by volume handle |
||
233 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
||
234 | * |
||
235 | * @param string $folderHandle |
||
236 | * @return object |
||
237 | */ |
||
238 | 1 | private function getFolderByHandle(string $folderHandle): \stdClass |
|
252 | } |
||
253 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.