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 |
||
| 30 | abstract class Entity implements Serializable |
||
| 31 | { |
||
| 32 | use Validation, Relations, Naming, EventHandlers; |
||
| 33 | |||
| 34 | /** @deprecated Use Relation::OPT_CLASS instead */ |
||
| 35 | const OPT_RELATION_CLASS = 'class'; |
||
| 36 | /** @deprecated Use Relation::OPT_CARDINALITY instead */ |
||
| 37 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 38 | /** @deprecated Use Relation::OPT_REFERENCE instead */ |
||
| 39 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 40 | /** @deprecated Use Relation::OPT_OPPONENT instead */ |
||
| 41 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 42 | /** @deprecated Use Relation::OPT_TABLE instead */ |
||
| 43 | const OPT_RELATION_TABLE = 'table'; |
||
| 44 | |||
| 45 | /** The variable(s) used for primary key. |
||
| 46 | * @var string[]|string */ |
||
| 47 | protected static $primaryKey = ['id']; |
||
| 48 | |||
| 49 | /** Whether or not the primary key is auto incremented. |
||
| 50 | * @var bool */ |
||
| 51 | protected static $autoIncrement = true; |
||
| 52 | |||
| 53 | /** Additional attributes to show in toArray method |
||
| 54 | * @var array */ |
||
| 55 | protected static $includedAttributes = []; |
||
| 56 | |||
| 57 | /** Attributes to hide for toArray method (overruled by $attributes parameter) |
||
| 58 | * @var array */ |
||
| 59 | protected static $excludedAttributes = []; |
||
| 60 | |||
| 61 | /** The reflections of the classes. |
||
| 62 | * @internal |
||
| 63 | * @var ReflectionClass[] */ |
||
| 64 | protected static $reflections = []; |
||
| 65 | |||
| 66 | /** The current data of a row. |
||
| 67 | * @var mixed[] */ |
||
| 68 | protected $data = []; |
||
| 69 | |||
| 70 | /** The original data of the row. |
||
| 71 | * @var mixed[] */ |
||
| 72 | protected $originalData = []; |
||
| 73 | |||
| 74 | /** The entity manager from which this entity got created |
||
| 75 | * @var EM */ |
||
| 76 | protected $entityManager; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Constructor |
||
| 80 | * |
||
| 81 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 82 | * |
||
| 83 | * @param mixed[] $data The current data |
||
| 84 | * @param EM $entityManager The EntityManager that created this entity |
||
| 85 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 86 | */ |
||
| 87 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Create an entityFetcher for this entity |
||
| 99 | * |
||
| 100 | * @return EntityFetcher |
||
| 101 | */ |
||
| 102 | public static function query() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the primary key vars |
||
| 109 | * |
||
| 110 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 111 | * columns. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | 122 | */ |
|
| 115 | public static function getPrimaryKeyVars() |
||
| 119 | 122 | ||
| 120 | 122 | /** |
|
| 121 | 122 | * Check if the table has a auto increment column |
|
| 122 | 122 | * |
|
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public static function isAutoIncremented() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param EM $entityManager |
||
| 132 | * @return static |
||
| 133 | */ |
||
| 134 | public function setEntityManager(EM $entityManager) |
||
| 139 | 166 | ||
| 140 | 6 | /** |
|
| 141 | * @param string $attribute |
||
| 142 | * @return mixed|null |
||
| 143 | 164 | * @see self::getAttribute |
|
| 144 | 164 | * @codeCoverageIgnore Alias for getAttribute |
|
| 145 | */ |
||
| 146 | public function __get($attribute) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the value from $attribute |
||
| 153 | * |
||
| 154 | * If there is a custom getter this method get called instead. |
||
| 155 | 72 | * |
|
| 156 | * @param string $attribute The variable to get |
||
| 157 | 72 | * @return mixed|null |
|
| 158 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 159 | */ |
||
| 160 | public function getAttribute($attribute) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if a column is defined |
||
| 181 | * |
||
| 182 | * @param $attribute |
||
| 183 | * @return bool |
||
| 184 | 19 | */ |
|
| 185 | public function __isset($attribute) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $attribute The variable to change |
||
| 206 | * @param mixed $value The value to store |
||
| 207 | * @see self::getAttribute |
||
| 208 | * @codeCoverageIgnore Alias for getAttribute |
||
| 209 | */ |
||
| 210 | 112 | public function __set($attribute, $value) |
|
| 214 | |||
| 215 | 112 | /** |
|
| 216 | 4 | * Set $attribute to $value |
|
| 217 | * |
||
| 218 | 108 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
|
| 219 | 108 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
|
| 220 | * written and give wrong results for dirty checking. |
||
| 221 | 108 | * |
|
| 222 | 1 | * The onChange event is called after something got changed. |
|
| 223 | * |
||
| 224 | * The method throws an error when the validation fails (also when the column does not exist). |
||
| 225 | 107 | * |
|
| 226 | * @param string $attribute The variable to change |
||
| 227 | * @param mixed $value The value to store |
||
| 228 | * @return static |
||
| 229 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 230 | * @throws Error |
||
| 231 | */ |
||
| 232 | public function setAttribute($attribute, $value) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Fill the entity with $data |
||
| 266 | * |
||
| 267 | * When $checkMissing is set to true it also proves that the absent columns are nullable. |
||
| 268 | * |
||
| 269 | * @param array $data |
||
| 270 | 38 | * @param bool $ignoreUnknown |
|
| 271 | * @param bool $checkMissing |
||
| 272 | 38 | * @throws UnknownColumn |
|
| 273 | * @throws Error |
||
| 274 | 38 | */ |
|
| 275 | 38 | public function fill(array $data, $ignoreUnknown = false, $checkMissing = false) |
|
| 291 | 31 | ||
| 292 | /** |
||
| 293 | * Resets the entity or $attribute to original data |
||
| 294 | 34 | * |
|
| 295 | 31 | * @param string $attribute Reset only this variable or all variables |
|
| 296 | */ |
||
| 297 | 34 | public function reset($attribute = null) |
|
| 311 | |||
| 312 | 8 | /** |
|
| 313 | * Save the entity to EntityManager |
||
| 314 | 7 | * |
|
| 315 | 2 | * @return Entity |
|
| 316 | 2 | * @throws IncompletePrimaryKey |
|
| 317 | 7 | */ |
|
| 318 | public function save() |
||
| 359 | |||
| 360 | /** |
||
| 361 | 16 | * Generates a primary key |
|
| 362 | * |
||
| 363 | * This method should only be executed from save method. |
||
| 364 | * @codeCoverageIgnore no operations |
||
| 365 | */ |
||
| 366 | protected function generatePrimaryKey() |
||
| 370 | |||
| 371 | /** |
||
| 372 | 16 | * Checks if entity or $attribute got changed |
|
| 373 | 16 | * |
|
| 374 | * @param string $attribute Check only this variable or all variables |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | 16 | public function isDirty($attribute = null) |
|
| 390 | 1 | ||
| 391 | 1 | /** |
|
| 392 | * Get the primary key |
||
| 393 | 1 | * |
|
| 394 | * @return array |
||
| 395 | * @throws IncompletePrimaryKey |
||
| 396 | */ |
||
| 397 | 14 | public function getPrimaryKey() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get current data |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | * @internal |
||
| 415 | */ |
||
| 416 | public function getData() |
||
| 420 | |||
| 421 | 20 | /** |
|
| 422 | * Set new original data |
||
| 423 | 20 | * |
|
| 424 | 4 | * @param array $data |
|
| 425 | 4 | * @internal |
|
| 426 | 4 | */ |
|
| 427 | public function setOriginalData(array $data) |
||
| 431 | |||
| 432 | 17 | /** |
|
| 433 | * Get an array of the entity |
||
| 434 | * |
||
| 435 | * @param array $attributes |
||
| 436 | * @param bool $includeRelations |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | public function toArray(array $attributes = [], $includeRelations = true) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * String representation of data |
||
| 471 | * |
||
| 472 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function serialize() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Constructs the object |
||
| 482 | * |
||
| 483 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 484 | * @param string $serialized The string representation of data |
||
| 485 | */ |
||
| 486 | public function unserialize($serialized) |
||
| 492 | } |
||
| 493 |