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 |
||
| 25 | class Entity extends Generic |
||
| 26 | { |
||
| 27 | /** @var array Collection of all additional fields names */ |
||
| 28 | public static $fieldNames = array(); |
||
| 29 | |||
| 30 | /** @var array Collection of localized additional fields identifiers */ |
||
| 31 | protected static $localizedFieldIDs = array(); |
||
| 32 | |||
| 33 | /** @var array Collection of NOT localized additional fields identifiers */ |
||
| 34 | protected static $notLocalizedFieldIDs = array(); |
||
| 35 | |||
| 36 | /** @var array Collection of all additional fields identifiers */ |
||
| 37 | protected static $fieldIDs = array(); |
||
| 38 | |||
| 39 | /** @var @var array Collection of additional fields value column names */ |
||
| 40 | protected static $fieldValueColumns = array(); |
||
| 41 | |||
| 42 | /** @var Condition Collection of entity field filter */ |
||
| 43 | protected $fieldFilter = array(); |
||
| 44 | |||
| 45 | /** @var string Query locale */ |
||
| 46 | protected $locale = ''; |
||
| 47 | |||
| 48 | /** @var array Collection of additional fields for ordering */ |
||
| 49 | protected $entityOrderBy = array(); |
||
| 50 | |||
| 51 | /** @var array Collection of search fields for query */ |
||
| 52 | protected $searchFilter = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Select specified entity fields. |
||
| 56 | * If this method is called then only selected entity fields |
||
| 57 | * would be return in entity instances. |
||
| 58 | * |
||
| 59 | * @param mixed $fieldNames Entity field name or collection of names |
||
| 60 | * @return $this Chaining |
||
| 61 | */ |
||
| 62 | public function select($fieldNames) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Set additional field for sorting. |
||
| 79 | * |
||
| 80 | * @param string $fieldName Additional field name |
||
| 81 | * @param string $order Sorting order |
||
| 82 | * @return $this Chaining |
||
| 83 | */ |
||
| 84 | public function orderBy($fieldName, $order = 'ASC') |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Search entity fields by text. |
||
| 97 | * |
||
| 98 | * @param string $text Searching text |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function search($text) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set resulting query limits. |
||
| 110 | * |
||
| 111 | * @param integer $offset Starting index |
||
| 112 | * @param integer|null $count Entities count |
||
| 113 | * @return $this Chaining |
||
| 114 | */ |
||
| 115 | public function limit($offset, $count = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Add condition to current query. |
||
| 124 | * |
||
| 125 | * @param string $fieldName Entity field name |
||
| 126 | * @param string $fieldValue Value |
||
| 127 | * @param string $fieldRelation Entity field to value relation |
||
| 128 | * @return $this Chaining |
||
| 129 | */ |
||
| 130 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
||
| 143 | |||
| 144 | /** @return array Collection of entity identifiers */ |
||
| 145 | protected function findEntityIDs() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get collection of entity identifiers filtered by navigation identifiers. |
||
| 181 | * |
||
| 182 | * @param array $entityIDs Additional collection of entity identifiers for filtering |
||
| 183 | * @return array Collection of material identifiers by navigation identifiers |
||
| 184 | */ |
||
| 185 | protected function findByNavigationIDs($entityIDs = array()) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get collection of entity identifiers filtered by additional field and its value. |
||
| 192 | * |
||
| 193 | * @param Condition[] $additionalFields Collection of additional field identifiers => values |
||
| 194 | * @param array $entityIDs Additional collection of entity identifiers for filtering |
||
| 195 | * @return array Collection of material identifiers by navigation identifiers |
||
| 196 | */ |
||
| 197 | protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param array $entityIDs |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function applySearch(array $entityIDs) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Add sorting to entity identifiers. |
||
| 243 | * |
||
| 244 | * @param array $entityIDs |
||
| 245 | * @param string $fieldName Additional field name for sorting |
||
| 246 | * @param string $order Sorting order(ASC|DESC) |
||
| 247 | * @return array Collection of entity identifiers ordered by additional field value |
||
| 248 | */ |
||
| 249 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get entities additional field values. |
||
| 270 | * |
||
| 271 | * @param array $entityIDs Collection of entity identifiers |
||
| 272 | * @return array Collection of entities additional fields EntityID => [Additional field name => Value] |
||
| 273 | * @throws EntityFieldNotFound |
||
| 274 | */ |
||
| 275 | protected function findAdditionalFields($entityIDs) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Fill entity additional fields. |
||
| 340 | * |
||
| 341 | * @param \samsoncms\api\Entity $entity Entity instance for filling |
||
| 342 | * @param array $additionalFields Collection of additional field values |
||
| 343 | * @return Entity With filled additional field values |
||
| 344 | */ |
||
| 345 | protected function fillEntityFields(\samsoncms\api\Entity $entity, array $additionalFields) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Perform SamsonCMS query and get collection of entities. |
||
| 364 | * |
||
| 365 | * @param int $page Page number |
||
| 366 | * @param int $size Page size |
||
| 367 | * |
||
| 368 | * @return \samsoncms\api\Entity[] Collection of entity fields |
||
| 369 | */ |
||
| 370 | public function find($page = null, $size = null) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Perform SamsonCMS query and get first matching entity. |
||
| 407 | * |
||
| 408 | * @return \samsoncms\api\Entity Firt matching entity |
||
| 409 | */ |
||
| 410 | public function first() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Perform SamsonCMS query and get collection of entities fields. |
||
| 427 | * |
||
| 428 | * @param string $fieldName Entity field name |
||
| 429 | * @return array Collection of entity fields |
||
| 430 | * @throws EntityFieldNotFound |
||
| 431 | */ |
||
| 432 | public function fields($fieldName) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Perform SamsonCMS query and get amount resulting entities. |
||
| 463 | * |
||
| 464 | * @return int Amount of resulting entities |
||
| 465 | */ |
||
| 466 | public function count() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Generic constructor. |
||
| 489 | * |
||
| 490 | * @param QueryInterface $query Database query instance |
||
| 491 | * @param string $locale Query localization |
||
| 492 | */ |
||
| 493 | public function __construct(QueryInterface $query = null, $locale = null) |
||
| 502 | } |
||
| 503 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: