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 | public 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 limit parameters */ | ||
| 47 | protected $limit = array(); | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Select specified entity fields. | ||
| 51 | * If this method is called then only selected entity fields | ||
| 52 | * would be return in entity instances. | ||
| 53 | * | ||
| 54 | * @param mixed $fieldNames Entity field name or collection of names | ||
| 55 | * @return self Chaining | ||
| 56 | */ | ||
| 57 | public function select($fieldNames) | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Set additional field for sorting. | ||
| 74 | * | ||
| 75 | * @param string $fieldName Additional field name | ||
| 76 | * @param string $order Sorting order | ||
| 77 | * @return self Chaining | ||
| 78 | */ | ||
| 79 | public function orderBy($fieldName, $order = 'ASC') | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Set resulting query limits. | ||
| 88 | * | ||
| 89 | * @param integer $offset Starting index | ||
| 90 | * @param integer|null $count Entities count | ||
| 91 | * @return self Chaining | ||
| 92 | */ | ||
| 93 | public function limit($offset, $count = null) | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Add condition to current query. | ||
| 102 | * | ||
| 103 | * @param string $fieldName Entity field name | ||
| 104 | * @param string $fieldValue Value | ||
| 105 | * @return self Chaining | ||
| 106 | */ | ||
| 107 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) | ||
| 120 | |||
| 121 | /** @return array Collection of entity identifiers */ | ||
| 122 | protected function findEntityIDs() | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Get collection of entity identifiers filtered by navigation identifiers. | ||
| 153 | * | ||
| 154 | * @param array $entityIDs Additional collection of entity identifiers for filtering | ||
| 155 | * @return array Collection of material identifiers by navigation identifiers | ||
| 156 | */ | ||
| 157 | protected function findByNavigationIDs($entityIDs = array()) | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Get collection of entity identifiers filtered by additional field and its value. | ||
| 164 | * | ||
| 165 | * @param array $additionalFields Collection of additional field identifiers => values | ||
| 166 | * @param array $entityIDs Additional collection of entity identifiers for filtering | ||
| 167 | * @return array Collection of material identifiers by navigation identifiers | ||
| 168 | */ | ||
| 169 | protected function findByAdditionalFields($additionalFields, $entityIDs = array()) | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Add sorting to entity identifiers. | ||
| 192 | * | ||
| 193 | * @param array $entityIDs | ||
| 194 | * @param string $fieldName Additional field name for sorting | ||
| 195 | * @param string $order Sorting order(ASC|DESC) | ||
| 196 | * @return array Collection of entity identifiers ordered by additional field value | ||
| 197 | */ | ||
| 198 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') | ||
| 211 | |||
| 212 | /** | ||
| 213 | * Get entities additional field values. | ||
| 214 | * | ||
| 215 | * @param array $entityIDs Collection of entity identifiers | ||
| 216 | * @return array Collection of entities additional fields EntityID => [Additional field name => Value] | ||
| 217 | */ | ||
| 218 | protected function findAdditionalFields($entityIDs) | ||
| 275 | |||
| 276 | /** | ||
| 277 | * Fill entity additional fields. | ||
| 278 | * | ||
| 279 | * @param Entity $entity Entity instance for filling | ||
| 280 | * @param array $additionalFields Collection of additional field values | ||
| 281 | * @return Entity With filled additional field values | ||
| 282 | */ | ||
| 283 | protected function fillEntityFields($entity, array $additionalFields) | ||
| 299 | |||
| 300 | /** | ||
| 301 | * Perform SamsonCMS query and get collection of entities. | ||
| 302 | * | ||
| 303 | * @param int $page Page number | ||
| 304 | * @param int $size Page size | ||
| 305 | * | ||
| 306 | * @return \samsoncms\api\Entity[] Collection of entity fields | ||
| 307 | */ | ||
| 308 | public function find($page = null, $size = null) | ||
| 336 | |||
| 337 | /** | ||
| 338 | * Perform SamsonCMS query and get first matching entity. | ||
| 339 | * | ||
| 340 | * @return \samsoncms\api\Entity Firt matching entity | ||
| 341 | */ | ||
| 342 | public function first() | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Perform SamsonCMS query and get collection of entities fields. | ||
| 356 | * | ||
| 357 | * @param string $fieldName Entity field name | ||
| 358 | * @return array Collection of entity fields | ||
| 359 | * @throws EntityFieldNotFound | ||
| 360 | */ | ||
| 361 | public function fields($fieldName) | ||
| 383 | |||
| 384 | /** | ||
| 385 | * Perform SamsonCMS query and get amount resulting entities. | ||
| 386 | * | ||
| 387 | * @return int Amount of resulting entities | ||
| 388 | */ | ||
| 389 | public function count() | ||
| 399 | |||
| 400 | /** | ||
| 401 | * Generic constructor. | ||
| 402 | * | ||
| 403 | * @param QueryInterface $query Database query instance | ||
| 404 | * @param string $locale Query localization | ||
| 405 | */ | ||
| 406 | public function __construct(QueryInterface $query, $locale = NULL) | ||
| 415 | } | ||
| 416 | 
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.