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 |
||
| 31 | abstract class Entity implements \Serializable |
||
| 32 | { |
||
| 33 | use Validation, Relations; |
||
| 34 | |||
| 35 | /** @deprecated Use Relation::OPT_CLASS instead */ |
||
| 36 | const OPT_RELATION_CLASS = 'class'; |
||
| 37 | /** @deprecated Use Relation::OPT_CARDINALITY instead */ |
||
| 38 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 39 | /** @deprecated Use Relation::OPT_REFERENCE instead */ |
||
| 40 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 41 | /** @deprecated Use Relation::OPT_OPPONENT instead */ |
||
| 42 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 43 | /** @deprecated Use Relation::OPT_TABLE instead */ |
||
| 44 | const OPT_RELATION_TABLE = 'table'; |
||
| 45 | |||
| 46 | /** The template to use to calculate the table name. |
||
| 47 | * @var string */ |
||
| 48 | protected static $tableNameTemplate; |
||
| 49 | |||
| 50 | /** The naming scheme to use for table names. |
||
| 51 | * @var string */ |
||
| 52 | protected static $namingSchemeTable; |
||
| 53 | |||
| 54 | /** The naming scheme to use for column names. |
||
| 55 | * @var string */ |
||
| 56 | protected static $namingSchemeColumn; |
||
| 57 | |||
| 58 | /** The naming scheme to use for method names. |
||
| 59 | * @var string */ |
||
| 60 | protected static $namingSchemeMethods; |
||
| 61 | |||
| 62 | /** Fixed table name (ignore other settings) |
||
| 63 | * @var string */ |
||
| 64 | protected static $tableName; |
||
| 65 | |||
| 66 | /** The variable(s) used for primary key. |
||
| 67 | * @var string[]|string */ |
||
| 68 | protected static $primaryKey = ['id']; |
||
| 69 | |||
| 70 | /** Fixed column names (ignore other settings) |
||
| 71 | * @var string[] */ |
||
| 72 | protected static $columnAliases = []; |
||
| 73 | |||
| 74 | /** A prefix for column names. |
||
| 75 | * @var string */ |
||
| 76 | protected static $columnPrefix; |
||
| 77 | |||
| 78 | /** Whether or not the primary key is auto incremented. |
||
| 79 | * @var bool */ |
||
| 80 | protected static $autoIncrement = true; |
||
| 81 | |||
| 82 | /** Whether or not the validator for this class is enabled. |
||
| 83 | * @var bool */ |
||
| 84 | protected static $enableValidator = false; |
||
| 85 | |||
| 86 | /** Whether or not the validator for a class got enabled during runtime. |
||
| 87 | * @var bool[] */ |
||
| 88 | protected static $enabledValidators = []; |
||
| 89 | |||
| 90 | /** The reflections of the classes. |
||
| 91 | * @internal |
||
| 92 | * @var \ReflectionClass[] */ |
||
| 93 | protected static $reflections = []; |
||
| 94 | |||
| 95 | /** The current data of a row. |
||
| 96 | * @var mixed[] */ |
||
| 97 | protected $data = []; |
||
| 98 | |||
| 99 | /** The original data of the row. |
||
| 100 | * @var mixed[] */ |
||
| 101 | protected $originalData = []; |
||
| 102 | |||
| 103 | /** The entity manager from which this entity got created |
||
| 104 | * @var EM */ |
||
| 105 | protected $entityManager; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Constructor |
||
| 109 | * |
||
| 110 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 111 | * |
||
| 112 | * @param mixed[] $data The current data |
||
| 113 | * @param EM $entityManager The EntityManager that created this entity |
||
| 114 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 115 | */ |
||
| 116 | 122 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get the column name of $attribute |
||
| 128 | * |
||
| 129 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 130 | * to $namingSchemeColumn. |
||
| 131 | * |
||
| 132 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 133 | * the same as getColumnName($name). |
||
| 134 | * |
||
| 135 | * @param string $attribute |
||
| 136 | * @return string |
||
| 137 | * @throws InvalidConfiguration |
||
| 138 | */ |
||
| 139 | 166 | public static function getColumnName($attribute) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get the primary key vars |
||
| 151 | * |
||
| 152 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 153 | * columns. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | 72 | public static function getPrimaryKeyVars() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the table name |
||
| 164 | * |
||
| 165 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 166 | * $tableName. |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | * @throws InvalidName|InvalidConfiguration |
||
| 170 | */ |
||
| 171 | 154 | public static function getTableName() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Check if the table has a auto increment column |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | 19 | public static function isAutoIncremented() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param EM $entityManager |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | 12 | public function setEntityManager(EM $entityManager) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get the value from $attribute |
||
| 203 | * |
||
| 204 | * If there is a custom getter this method get called instead. |
||
| 205 | * |
||
| 206 | * @param string $attribute The variable to get |
||
| 207 | * @return mixed|null |
||
| 208 | * @throws IncompletePrimaryKey |
||
| 209 | * @throws InvalidConfiguration |
||
| 210 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 211 | */ |
||
| 212 | 112 | public function __get($attribute) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Check if a column is defined |
||
| 233 | * |
||
| 234 | * @param $attribute |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 3 | public function __isset($attribute) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Set $attribute to $value |
||
| 258 | * |
||
| 259 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 260 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 261 | * written and give wrong results for dirty checking. |
||
| 262 | * |
||
| 263 | * The onChange event is called after something got changed. |
||
| 264 | * |
||
| 265 | * The method throws an error when the validation fails (also when the column does not exist). |
||
| 266 | * |
||
| 267 | * @param string $attribute The variable to change |
||
| 268 | * @param mixed $value The value to store |
||
| 269 | * @throws Error |
||
| 270 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 271 | */ |
||
| 272 | 38 | public function __set($attribute, $value) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Fill the entity with $data |
||
| 303 | * |
||
| 304 | * When $checkMissing is set to true it also proves that the absent columns are nullable. |
||
| 305 | * |
||
| 306 | * @param array $data |
||
| 307 | * @param bool $ignoreUnknown |
||
| 308 | * @param bool $checkMissing |
||
| 309 | * @throws Error |
||
| 310 | * @throws UnknownColumn |
||
| 311 | */ |
||
| 312 | 8 | public function fill(array $data, $ignoreUnknown = false, $checkMissing = false) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Resets the entity or $attribute to original data |
||
| 331 | * |
||
| 332 | * @param string $attribute Reset only this variable or all variables |
||
| 333 | * @throws InvalidConfiguration |
||
| 334 | */ |
||
| 335 | 22 | public function reset($attribute = null) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Save the entity to EntityManager |
||
| 352 | * |
||
| 353 | * @return Entity |
||
| 354 | * @throws Exception\NoConnection |
||
| 355 | * @throws Exception\NoEntity |
||
| 356 | * @throws Exception\NotScalar |
||
| 357 | * @throws Exception\UnsupportedDriver |
||
| 358 | * @throws IncompletePrimaryKey |
||
| 359 | * @throws InvalidConfiguration |
||
| 360 | * @throws InvalidName |
||
| 361 | * @throws NoEntityManager |
||
| 362 | */ |
||
| 363 | 16 | public function save() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Generates a primary key |
||
| 407 | * |
||
| 408 | * This method should only be executed from save method. |
||
| 409 | * @codeCoverageIgnore no operations |
||
| 410 | */ |
||
| 411 | protected function generatePrimaryKey() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Checks if entity or $attribute got changed |
||
| 418 | * |
||
| 419 | * @param string $attribute Check only this variable or all variables |
||
| 420 | * @return bool |
||
| 421 | * @throws InvalidConfiguration |
||
| 422 | */ |
||
| 423 | 20 | public function isDirty($attribute = null) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Empty event handler |
||
| 439 | * |
||
| 440 | * Get called when something is changed with magic setter. |
||
| 441 | * |
||
| 442 | * @param string $attribute The variable that got changed.merge(node.inheritedProperties) |
||
| 443 | * @param mixed $oldValue The old value of the variable |
||
| 444 | * @param mixed $value The new value of the variable |
||
| 445 | * @codeCoverageIgnore dummy event handler |
||
| 446 | */ |
||
| 447 | public function onChange($attribute, $oldValue, $value) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Empty event handler |
||
| 453 | * |
||
| 454 | * Get called when the entity get initialized. |
||
| 455 | * |
||
| 456 | * @param bool $new Whether or not the entity is new or from database |
||
| 457 | * @codeCoverageIgnore dummy event handler |
||
| 458 | */ |
||
| 459 | public function onInit($new) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Empty event handler |
||
| 465 | * |
||
| 466 | * Get called before the entity get updated in database. |
||
| 467 | * |
||
| 468 | * @codeCoverageIgnore dummy event handler |
||
| 469 | */ |
||
| 470 | public function preUpdate() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Empty event handler |
||
| 476 | * |
||
| 477 | * Get called before the entity get inserted in database. |
||
| 478 | * |
||
| 479 | * @codeCoverageIgnore dummy event handler |
||
| 480 | */ |
||
| 481 | public function prePersist() |
||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * Empty event handler |
||
| 488 | * |
||
| 489 | * Get called after the entity got inserted in database. |
||
| 490 | * |
||
| 491 | * @codeCoverageIgnore dummy event handler |
||
| 492 | */ |
||
| 493 | public function postPersist() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Empty event handler |
||
| 499 | * |
||
| 500 | * Get called after the entity got updated in database. |
||
| 501 | * |
||
| 502 | * @codeCoverageIgnore dummy event handler |
||
| 503 | */ |
||
| 504 | public function postUpdate() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get the primary key |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | * @throws IncompletePrimaryKey |
||
| 513 | */ |
||
| 514 | 53 | public function getPrimaryKey() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Get current data |
||
| 529 | * |
||
| 530 | * @return array |
||
| 531 | * @internal |
||
| 532 | */ |
||
| 533 | 31 | public function getData() |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Set new original data |
||
| 540 | * |
||
| 541 | * @param array $data |
||
| 542 | * @internal |
||
| 543 | */ |
||
| 544 | 38 | public function setOriginalData(array $data) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * String representation of data |
||
| 551 | * |
||
| 552 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | 2 | public function serialize() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Constructs the object |
||
| 562 | * |
||
| 563 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 564 | * @param string $serialized The string representation of data |
||
| 565 | */ |
||
| 566 | 3 | public function unserialize($serialized) |
|
| 572 | |||
| 573 | // DEPRECATED stuff |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @return string |
||
| 577 | * @deprecated use getOption from EntityManager |
||
| 578 | * @codeCoverageIgnore deprecated |
||
| 579 | */ |
||
| 580 | public static function getTableNameTemplate() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param string $tableNameTemplate |
||
| 587 | * @deprecated use setOption from EntityManager |
||
| 588 | * @codeCoverageIgnore deprecated |
||
| 589 | */ |
||
| 590 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return string |
||
| 597 | * @deprecated use getOption from EntityManager |
||
| 598 | * @codeCoverageIgnore deprecated |
||
| 599 | */ |
||
| 600 | public static function getNamingSchemeTable() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param string $namingSchemeTable |
||
| 607 | * @deprecated use setOption from EntityManager |
||
| 608 | * @codeCoverageIgnore deprecated |
||
| 609 | */ |
||
| 610 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @return string |
||
| 617 | * @deprecated use getOption from EntityManager |
||
| 618 | * @codeCoverageIgnore deprecated |
||
| 619 | */ |
||
| 620 | public static function getNamingSchemeColumn() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param string $namingSchemeColumn |
||
| 627 | * @deprecated use setOption from EntityManager |
||
| 628 | * @codeCoverageIgnore deprecated |
||
| 629 | */ |
||
| 630 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return string |
||
| 637 | * @deprecated use getOption from EntityManager |
||
| 638 | * @codeCoverageIgnore deprecated |
||
| 639 | */ |
||
| 640 | public static function getNamingSchemeMethods() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param string $namingSchemeMethods |
||
| 647 | * @deprecated use setOption from EntityManager |
||
| 648 | * @codeCoverageIgnore deprecated |
||
| 649 | */ |
||
| 650 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 654 | } |
||
| 655 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.