Complex classes like Fields 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 Fields, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Fields extends Base |
||
25 | { |
||
26 | /** |
||
27 | * @var FieldModel[] |
||
28 | */ |
||
29 | private $fields = []; |
||
30 | |||
31 | /** |
||
32 | * @var FieldGroupModel[] |
||
33 | */ |
||
34 | private $groups = []; |
||
35 | |||
36 | /** |
||
37 | * @var FieldFactory |
||
38 | */ |
||
39 | private $fieldFactory; |
||
40 | |||
41 | /** |
||
42 | * @return FieldFactory |
||
43 | */ |
||
44 | public function getFieldFactory() |
||
48 | |||
49 | //============================================================================================================== |
||
50 | //================================================ EXPORT ==================================================== |
||
51 | //============================================================================================================== |
||
52 | |||
53 | /** |
||
54 | * Export fields. |
||
55 | * |
||
56 | * @param FieldGroupModel[] $groups |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | public function export(array $groups = []) |
||
78 | |||
79 | /** |
||
80 | * Get field definition. |
||
81 | * |
||
82 | * @param FieldModel $field |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | private function getFieldDefinition(FieldModel $field) |
||
94 | |||
95 | //============================================================================================================== |
||
96 | //================================================ IMPORT ==================================================== |
||
97 | //============================================================================================================== |
||
98 | |||
99 | /** |
||
100 | * Attempt to import fields. |
||
101 | * |
||
102 | * @param array $groupDefinitions |
||
103 | * @param bool $force if set to true items not in the import will be deleted |
||
104 | * |
||
105 | * @return Result |
||
106 | */ |
||
107 | public function import(array $groupDefinitions, $force = false) |
||
143 | |||
144 | /** |
||
145 | * Save field group. |
||
146 | * |
||
147 | * @param FieldGroupModel $group |
||
148 | * |
||
149 | * @throws Exception |
||
150 | */ |
||
151 | private function saveFieldGroupModel(FieldGroupModel $group) |
||
159 | |||
160 | /** |
||
161 | * Save field. |
||
162 | * |
||
163 | * @param FieldModel $field |
||
164 | * |
||
165 | * @throws \Exception |
||
166 | */ |
||
167 | private function saveFieldModel(FieldModel $field) |
||
179 | |||
180 | /** |
||
181 | * Removes fields that where not imported. |
||
182 | */ |
||
183 | private function deleteFields() |
||
191 | |||
192 | /** |
||
193 | * Removes groups that where not imported. |
||
194 | */ |
||
195 | private function deleteGroups() |
||
202 | |||
203 | /** |
||
204 | * Removes fields and groups that where not imported. |
||
205 | */ |
||
206 | private function deleteFieldsAndGroups() |
||
211 | |||
212 | /** |
||
213 | * Creates new or updates existing group model. |
||
214 | * |
||
215 | * @param string $group |
||
216 | * |
||
217 | * @return FieldGroupModel |
||
218 | */ |
||
219 | private function createFieldGroupModel($group) |
||
228 | |||
229 | /** |
||
230 | * @param string $field |
||
231 | * |
||
232 | * @return FieldModel |
||
233 | */ |
||
234 | private function getFieldModel($field) |
||
238 | |||
239 | /** |
||
240 | * Validates field type, throw error when it's incorrect. |
||
241 | * |
||
242 | * @param FieldModel $field |
||
243 | * |
||
244 | * @throws \Exception |
||
245 | */ |
||
246 | private function validateFieldModel(FieldModel $field) |
||
257 | |||
258 | /** |
||
259 | * Import field group fields. |
||
260 | * |
||
261 | * @param array $fieldDefinitions |
||
262 | * @param FieldGroupModel $group |
||
263 | * @param bool $force |
||
264 | * |
||
265 | * @throws \Exception |
||
266 | */ |
||
267 | private function importFields(array $fieldDefinitions, FieldGroupModel $group, $force = false) |
||
286 | |||
287 | /** |
||
288 | * Unset group and field data else $force flag will delete it. |
||
289 | * |
||
290 | * @param string $name |
||
291 | * @param array $definitions |
||
292 | */ |
||
293 | private function unsetData($name, array $definitions) |
||
302 | |||
303 | /** |
||
304 | * Set global field context. |
||
305 | */ |
||
306 | private function setGlobalContext() |
||
311 | |||
312 | //============================================================================================================== |
||
313 | //============================================= FIELD LAYOUT ================================================= |
||
314 | //============================================================================================================== |
||
315 | |||
316 | /** |
||
317 | * Get field layout definition. |
||
318 | * |
||
319 | * @param FieldLayoutModel $fieldLayout |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | public function getFieldLayoutDefinition(FieldLayoutModel $fieldLayout) |
||
337 | |||
338 | /** |
||
339 | * Get field layout fields definition. |
||
340 | * |
||
341 | * @param FieldLayoutFieldModel[] $fields |
||
342 | * |
||
343 | * @return array |
||
344 | */ |
||
345 | private function getFieldLayoutFieldsDefinition(array $fields) |
||
355 | |||
356 | /** |
||
357 | * Attempt to import a field layout. |
||
358 | * |
||
359 | * @param array $fieldLayoutDef |
||
360 | * |
||
361 | * @return FieldLayoutModel |
||
362 | */ |
||
363 | public function getFieldLayout(array $fieldLayoutDef) |
||
385 | |||
386 | /** |
||
387 | * Get a prepared fieldLayout for the craft assembleLayout function. |
||
388 | * |
||
389 | * @param array $fieldLayoutDef |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | private function getPrepareFieldLayout(array $fieldLayoutDef) |
||
414 | |||
415 | /** |
||
416 | * Reset craft fields service groups cache using reflection. |
||
417 | */ |
||
418 | private function resetCraftFieldsServiceGroupsCache() |
||
426 | |||
427 | /** |
||
428 | * Reset craft fields service fields cache using reflection. |
||
429 | */ |
||
430 | private function resetCraftFieldsServiceFieldsCache() |
||
441 | |||
442 | /** |
||
443 | * Reset craft db schema content table cache using reflection. |
||
444 | */ |
||
445 | private function resetCraftDbSchemaContentTableCache() |
||
453 | } |
||
454 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.