Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
22 | class Entity extends Generic |
||
23 | { |
||
24 | /** @var array Collection of all additional fields names */ |
||
25 | protected static $fieldNames = array(); |
||
26 | |||
27 | /** @var array Collection of localized additional fields identifiers */ |
||
28 | protected static $localizedFieldIDs = array(); |
||
29 | |||
30 | /** @var array Collection of NOT localized additional fields identifiers */ |
||
31 | protected static $notLocalizedFieldIDs = array(); |
||
32 | |||
33 | /** @var array Collection of all additional fields identifiers */ |
||
34 | protected static $fieldIDs = array(); |
||
35 | |||
36 | /** @var @var array Collection of additional fields value column names */ |
||
37 | protected static $fieldValueColumns = array(); |
||
38 | |||
39 | |||
40 | /** @var array Collection of entity field filter */ |
||
41 | protected $fieldFilter = array(); |
||
42 | |||
43 | /** @var string Query locale */ |
||
44 | protected $locale = ''; |
||
45 | |||
46 | /** @var array Collection of ordering parameters */ |
||
47 | protected $orderBy = array(); |
||
48 | |||
49 | /** @var array Collection of limit parameters */ |
||
50 | protected $limit = array(); |
||
51 | |||
52 | protected function localizedFieldsCondition($fieldIDs, $locale) |
||
66 | |||
67 | public function save(\samsoncms\api\Entity &$instance, $locale = null) |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Select specified entity fields. |
||
82 | * If this method is called then only selected entity fields |
||
83 | * would be return in entity instances. |
||
84 | * |
||
85 | * @param mixed $fieldNames Entity field name or collection of names |
||
86 | * @return self Chaining |
||
87 | */ |
||
88 | public function select($fieldNames) |
||
102 | |||
103 | /** |
||
104 | * Set additional field for sorting. |
||
105 | * |
||
106 | * @param string $fieldName Additional field name |
||
107 | * @param string $order Sorting order |
||
108 | * @return self Chaining |
||
109 | */ |
||
110 | public function orderBy($fieldName, $order = 'ASC') |
||
116 | |||
117 | /** |
||
118 | * Set resulting query limits. |
||
119 | * |
||
120 | * @param integer $offset Starting index |
||
121 | * @param integer|null $count Entities count |
||
122 | * @return self Chaining |
||
123 | */ |
||
124 | public function limit($offset, $count = null) |
||
130 | |||
131 | /** |
||
132 | * Add condition to current query. |
||
133 | * |
||
134 | * @param string $fieldName Entity field name |
||
135 | * @param string $fieldValue Value |
||
136 | * @return self Chaining |
||
137 | */ |
||
138 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
||
151 | |||
152 | /** @return array Collection of entity identifiers */ |
||
153 | protected function findEntityIDs() |
||
181 | |||
182 | /** |
||
183 | * Get collection of entity identifiers filtered by navigation identifiers. |
||
184 | * |
||
185 | * @param array $entityIDs Additional collection of entity identifiers for filtering |
||
186 | * @return array Collection of material identifiers by navigation identifiers |
||
187 | */ |
||
188 | protected function findByNavigationIDs($entityIDs = array()) |
||
192 | |||
193 | /** |
||
194 | * Get collection of entity identifiers filtered by additional field and its value. |
||
195 | * |
||
196 | * @param array $additionalFields Collection of additional field identifiers => values |
||
197 | * @param array $entityIDs Additional collection of entity identifiers for filtering |
||
198 | * @return array Collection of material identifiers by navigation identifiers |
||
199 | */ |
||
200 | protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
||
220 | |||
221 | /** |
||
222 | * Add sorting to entity identifiers. |
||
223 | * |
||
224 | * @param array $entityIDs |
||
225 | * @param string $fieldName Additional field name for sorting |
||
226 | * @param string $order Sorting order(ASC|DESC) |
||
227 | * @return array Collection of entity identifiers ordered by additional field value |
||
228 | */ |
||
229 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
||
242 | |||
243 | /** |
||
244 | * Get entities additional field values. |
||
245 | * |
||
246 | * @param array $entityIDs Collection of entity identifiers |
||
247 | * @return array Collection of entities additional fields EntityID => [Additional field name => Value] |
||
248 | */ |
||
249 | protected function findAdditionalFields($entityIDs) |
||
306 | |||
307 | /** |
||
308 | * Fill entity additional fields. |
||
309 | * |
||
310 | * @param Entity $entity Entity instance for filling |
||
311 | * @param array $additionalFields Collection of additional field values |
||
312 | * @return Entity With filled additional field values |
||
313 | */ |
||
314 | protected function fillEntityFields($entity, array $additionalFields) |
||
330 | |||
331 | /** |
||
332 | * Perform SamsonCMS query and get collection of entities. |
||
333 | * |
||
334 | * @return \samsoncms\api\Entity[] Collection of entity fields |
||
335 | */ |
||
336 | public function find() |
||
359 | |||
360 | /** |
||
361 | * Perform SamsonCMS query and get first matching entity. |
||
362 | * |
||
363 | * @return \samsoncms\api\Entity Firt matching entity |
||
364 | */ |
||
365 | public function first() |
||
376 | |||
377 | /** |
||
378 | * Perform SamsonCMS query and get collection of entities fields. |
||
379 | * |
||
380 | * @param string $fieldName Entity field name |
||
381 | * @return array Collection of entity fields |
||
382 | * @throws EntityFieldNotFound |
||
383 | */ |
||
384 | public function fields($fieldName) |
||
406 | |||
407 | /** |
||
408 | * Perform SamsonCMS query and get amount resulting entities. |
||
409 | * |
||
410 | * @return int Amount of resulting entities |
||
411 | */ |
||
412 | public function count() |
||
422 | |||
423 | /** |
||
424 | * Generic constructor. |
||
425 | * |
||
426 | * @param QueryInterface $query Database query instance |
||
427 | * @param string $locale Query localization |
||
428 | */ |
||
429 | public function __construct(QueryInterface $query, $locale = NULL) |
||
438 | } |
||
439 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.