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  | 
            ||
| 26 | class Entity extends Generic  | 
            ||
| 27 | { | 
            ||
| 28 | /** @var array Collection of all additional fields names */  | 
            ||
| 29 | public static $fieldNames = array();  | 
            ||
| 30 | |||
| 31 | /** @var array Collection of localized additional fields identifiers */  | 
            ||
| 32 | protected static $localizedFieldIDs = array();  | 
            ||
| 33 | |||
| 34 | /** @var array Collection of NOT localized additional fields identifiers */  | 
            ||
| 35 | protected static $notLocalizedFieldIDs = array();  | 
            ||
| 36 | |||
| 37 | /** @var array Collection of all additional fields identifiers */  | 
            ||
| 38 | protected static $fieldIDs = array();  | 
            ||
| 39 | |||
| 40 | /** @var @var array Collection of additional fields value column names */  | 
            ||
| 41 | protected static $fieldValueColumns = array();  | 
            ||
| 42 | |||
| 43 | /** @var Condition Collection of entity field filter */  | 
            ||
| 44 | protected $fieldFilter = array();  | 
            ||
| 45 | |||
| 46 | /** @var string Query locale */  | 
            ||
| 47 | protected $locale = '';  | 
            ||
| 48 | |||
| 49 | /** @var array Collection of additional fields for ordering */  | 
            ||
| 50 | protected $entityOrderBy = array();  | 
            ||
| 51 | |||
| 52 | /** @var array Collection of search fields for query */  | 
            ||
| 53 | protected $searchFilter = array();  | 
            ||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * Select specified entity fields.  | 
            ||
| 57 | * If this method is called then only selected entity fields  | 
            ||
| 58 | * would be return in entity instances.  | 
            ||
| 59 | *  | 
            ||
| 60 | * @param mixed $fieldNames Entity field name or collection of names  | 
            ||
| 61 | * @return $this Chaining  | 
            ||
| 62 | */  | 
            ||
| 63 | public function select($fieldNames)  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * Set additional field for sorting.  | 
            ||
| 80 | *  | 
            ||
| 81 | * @param string $fieldName Additional field name  | 
            ||
| 82 | * @param string $order Sorting order  | 
            ||
| 83 | * @return $this Chaining  | 
            ||
| 84 | */  | 
            ||
| 85 | public function orderBy($fieldName, $order = 'ASC')  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * Search entity fields by text.  | 
            ||
| 98 | *  | 
            ||
| 99 | * @param string $text Searching text  | 
            ||
| 100 | * @return $this  | 
            ||
| 101 | */  | 
            ||
| 102 | public function search($text)  | 
            ||
| 108 | |||
| 109 | /**  | 
            ||
| 110 | * Set resulting query limits.  | 
            ||
| 111 | *  | 
            ||
| 112 | * @param integer $offset Starting index  | 
            ||
| 113 | * @param integer|null $count Entities count  | 
            ||
| 114 | * @return $this Chaining  | 
            ||
| 115 | */  | 
            ||
| 116 | public function limit($offset, $count = null)  | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * Add condition to current query.  | 
            ||
| 125 | *  | 
            ||
| 126 | * @param string $fieldName Entity field name  | 
            ||
| 127 | * @param string $fieldValue Value  | 
            ||
| 128 | * @param string $fieldRelation Entity field to value relation  | 
            ||
| 129 | * @return $this Chaining  | 
            ||
| 130 | */  | 
            ||
| 131 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL)  | 
            ||
| 144 | |||
| 145 | /** @return array Collection of entity identifiers */  | 
            ||
| 146 | protected function findEntityIDs()  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Get collection of entity identifiers filtered by navigation identifiers.  | 
            ||
| 177 | *  | 
            ||
| 178 | * @param array $entityIDs Additional collection of entity identifiers for filtering  | 
            ||
| 179 | * @return array Collection of material identifiers by navigation identifiers  | 
            ||
| 180 | */  | 
            ||
| 181 | protected function findByNavigationIDs($entityIDs = array())  | 
            ||
| 185 | |||
| 186 | /**  | 
            ||
| 187 | * Get collection of entity identifiers filtered by additional field and its value.  | 
            ||
| 188 | *  | 
            ||
| 189 | * @param Condition[] $additionalFields Collection of additional field identifiers => values  | 
            ||
| 190 | * @param array $entityIDs Additional collection of entity identifiers for filtering  | 
            ||
| 191 | * @return array Collection of material identifiers by navigation identifiers  | 
            ||
| 192 | */  | 
            ||
| 193 | protected function findByAdditionalFields($additionalFields, $entityIDs = array())  | 
            ||
| 213 | |||
| 214 | /**  | 
            ||
| 215 | * @param array $entityIDs  | 
            ||
| 216 | * @return array  | 
            ||
| 217 | */  | 
            ||
| 218 | protected function applySearch(array $entityIDs)  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Add sorting to entity identifiers.  | 
            ||
| 239 | *  | 
            ||
| 240 | * @param array $entityIDs  | 
            ||
| 241 | * @param string $fieldName Additional field name for sorting  | 
            ||
| 242 | * @param string $order Sorting order(ASC|DESC)  | 
            ||
| 243 | * @return array Collection of entity identifiers ordered by additional field value  | 
            ||
| 244 | */  | 
            ||
| 245 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC')  | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * Get entities additional field values.  | 
            ||
| 266 | *  | 
            ||
| 267 | * @param array $entityIDs Collection of entity identifiers  | 
            ||
| 268 | * @return array Collection of entities additional fields EntityID => [Additional field name => Value]  | 
            ||
| 269 | */  | 
            ||
| 270 | protected function findAdditionalFields($entityIDs)  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * Fill entity additional fields.  | 
            ||
| 335 | *  | 
            ||
| 336 | * @param Entity $entity Entity instance for filling  | 
            ||
| 337 | * @param array $additionalFields Collection of additional field values  | 
            ||
| 338 | * @return Entity With filled additional field values  | 
            ||
| 339 | */  | 
            ||
| 340 | protected function fillEntityFields($entity, array $additionalFields)  | 
            ||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * Perform SamsonCMS query and get collection of entities.  | 
            ||
| 359 | *  | 
            ||
| 360 | * @param int $page Page number  | 
            ||
| 361 | * @param int $size Page size  | 
            ||
| 362 | *  | 
            ||
| 363 | * @return \samsoncms\api\Entity[] Collection of entity fields  | 
            ||
| 364 | */  | 
            ||
| 365 | public function find($page = null, $size = null)  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * Perform SamsonCMS query and get first matching entity.  | 
            ||
| 405 | *  | 
            ||
| 406 | * @return \samsoncms\api\Entity Firt matching entity  | 
            ||
| 407 | */  | 
            ||
| 408 | public function first()  | 
            ||
| 419 | |||
| 420 | /**  | 
            ||
| 421 | * Perform SamsonCMS query and get collection of entities fields.  | 
            ||
| 422 | *  | 
            ||
| 423 | * @param string $fieldName Entity field name  | 
            ||
| 424 | * @return array Collection of entity fields  | 
            ||
| 425 | * @throws EntityFieldNotFound  | 
            ||
| 426 | */  | 
            ||
| 427 | public function fields($fieldName)  | 
            ||
| 455 | |||
| 456 | /**  | 
            ||
| 457 | * Perform SamsonCMS query and get amount resulting entities.  | 
            ||
| 458 | *  | 
            ||
| 459 | * @return int Amount of resulting entities  | 
            ||
| 460 | */  | 
            ||
| 461 | public function count()  | 
            ||
| 471 | |||
| 472 | /**  | 
            ||
| 473 | * Generic constructor.  | 
            ||
| 474 | *  | 
            ||
| 475 | * @param QueryInterface $query Database query instance  | 
            ||
| 476 | * @param string $locale Query localization  | 
            ||
| 477 | */  | 
            ||
| 478 | public function __construct(QueryInterface $query = null, $locale = null)  | 
            ||
| 487 | }  | 
            ||
| 488 | 
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: