Complex classes like Entity 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 Entity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Entity extends Generic |
||
24 | { |
||
25 | /** @var array Collection of all additional fields names */ |
||
26 | public static $fieldNames = array(); |
||
27 | |||
28 | /** @var array Collection of localized additional fields identifiers */ |
||
29 | protected static $localizedFieldIDs = array(); |
||
30 | |||
31 | /** @var array Collection of NOT localized additional fields identifiers */ |
||
32 | protected static $notLocalizedFieldIDs = array(); |
||
33 | |||
34 | /** @var array Collection of all additional fields identifiers */ |
||
35 | protected static $fieldIDs = array(); |
||
36 | |||
37 | /** @var @var array Collection of additional fields value column names */ |
||
38 | protected static $fieldValueColumns = array(); |
||
39 | |||
40 | |||
41 | /** @var array Collection of entity field filter */ |
||
42 | protected $fieldFilter = array(); |
||
43 | |||
44 | /** @var string Query locale */ |
||
45 | protected $locale = ''; |
||
46 | |||
47 | /** @var array Collection of additional fields for ordering */ |
||
48 | protected $entityOrderBy = array(); |
||
49 | |||
50 | /** |
||
51 | * Select specified entity fields. |
||
52 | * If this method is called then only selected entity fields |
||
53 | * would be return in entity instances. |
||
54 | * |
||
55 | * @param mixed $fieldNames Entity field name or collection of names |
||
56 | * @return $this Chaining |
||
57 | */ |
||
58 | public function select($fieldNames) |
||
72 | |||
73 | /** |
||
74 | * Set additional field for sorting. |
||
75 | * |
||
76 | * @param string $fieldName Additional field name |
||
77 | * @param string $order Sorting order |
||
78 | * @return $this Chaining |
||
79 | */ |
||
80 | public function orderBy($fieldName, $order = 'ASC') |
||
90 | |||
91 | /** |
||
92 | * Set resulting query limits. |
||
93 | * |
||
94 | * @param integer $offset Starting index |
||
95 | * @param integer|null $count Entities count |
||
96 | * @return $this Chaining |
||
97 | */ |
||
98 | public function limit($offset, $count = null) |
||
104 | |||
105 | /** |
||
106 | * Add condition to current query. |
||
107 | * |
||
108 | * @param string $fieldName Entity field name |
||
109 | * @param string $fieldValue Value |
||
110 | * @return $this Chaining |
||
111 | */ |
||
112 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
||
125 | |||
126 | /** @return array Collection of entity identifiers */ |
||
127 | protected function findEntityIDs() |
||
155 | |||
156 | /** |
||
157 | * Get collection of entity identifiers filtered by navigation identifiers. |
||
158 | * |
||
159 | * @param array $entityIDs Additional collection of entity identifiers for filtering |
||
160 | * @return array Collection of material identifiers by navigation identifiers |
||
161 | */ |
||
162 | protected function findByNavigationIDs($entityIDs = array()) |
||
166 | |||
167 | /** |
||
168 | * Get collection of entity identifiers filtered by additional field and its value. |
||
169 | * |
||
170 | * @param array $additionalFields Collection of additional field identifiers => values |
||
171 | * @param array $entityIDs Additional collection of entity identifiers for filtering |
||
172 | * @return array Collection of material identifiers by navigation identifiers |
||
173 | */ |
||
174 | protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
||
194 | |||
195 | /** |
||
196 | * Add sorting to entity identifiers. |
||
197 | * |
||
198 | * @param array $entityIDs |
||
199 | * @param string $fieldName Additional field name for sorting |
||
200 | * @param string $order Sorting order(ASC|DESC) |
||
201 | * @return array Collection of entity identifiers ordered by additional field value |
||
202 | */ |
||
203 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
||
221 | |||
222 | /** |
||
223 | * Get entities additional field values. |
||
224 | * |
||
225 | * @param array $entityIDs Collection of entity identifiers |
||
226 | * @return array Collection of entities additional fields EntityID => [Additional field name => Value] |
||
227 | */ |
||
228 | protected function findAdditionalFields($entityIDs) |
||
290 | |||
291 | /** |
||
292 | * Fill entity additional fields. |
||
293 | * |
||
294 | * @param Entity $entity Entity instance for filling |
||
295 | * @param array $additionalFields Collection of additional field values |
||
296 | * @return Entity With filled additional field values |
||
297 | */ |
||
298 | protected function fillEntityFields($entity, array $additionalFields) |
||
314 | |||
315 | /** |
||
316 | * Perform SamsonCMS query and get collection of entities. |
||
317 | * |
||
318 | * @param int $page Page number |
||
319 | * @param int $size Page size |
||
320 | * |
||
321 | * @return \samsoncms\api\Entity[] Collection of entity fields |
||
322 | */ |
||
323 | public function find($page = null, $size = null) |
||
351 | |||
352 | /** |
||
353 | * Perform SamsonCMS query and get first matching entity. |
||
354 | * |
||
355 | * @return \samsoncms\api\Entity Firt matching entity |
||
356 | */ |
||
357 | public function first() |
||
368 | |||
369 | /** |
||
370 | * Perform SamsonCMS query and get collection of entities fields. |
||
371 | * |
||
372 | * @param string $fieldName Entity field name |
||
373 | * @return array Collection of entity fields |
||
374 | * @throws EntityFieldNotFound |
||
375 | */ |
||
376 | public function fields($fieldName) |
||
402 | |||
403 | /** |
||
404 | * Perform SamsonCMS query and get amount resulting entities. |
||
405 | * |
||
406 | * @return int Amount of resulting entities |
||
407 | */ |
||
408 | public function count() |
||
418 | |||
419 | /** |
||
420 | * Generic constructor. |
||
421 | * |
||
422 | * @param QueryInterface $query Database query instance |
||
423 | * @param string $locale Query localization |
||
424 | */ |
||
425 | public function __construct(QueryInterface $query = null, $locale = null) |
||
434 | } |
||
435 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.