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 |
||
| 29 | abstract class Entity implements \Serializable |
||
| 30 | { |
||
| 31 | const OPT_RELATION_CLASS = 'class'; |
||
| 32 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 33 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 34 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 35 | const OPT_RELATION_TABLE = 'table'; |
||
| 36 | |||
| 37 | /** The template to use to calculate the table name. |
||
| 38 | * @var string */ |
||
| 39 | protected static $tableNameTemplate; |
||
| 40 | |||
| 41 | /** The naming scheme to use for table names. |
||
| 42 | * @var string */ |
||
| 43 | protected static $namingSchemeTable; |
||
| 44 | |||
| 45 | /** The naming scheme to use for column names. |
||
| 46 | * @var string */ |
||
| 47 | protected static $namingSchemeColumn; |
||
| 48 | |||
| 49 | /** The naming scheme to use for method names. |
||
| 50 | * @var string */ |
||
| 51 | protected static $namingSchemeMethods; |
||
| 52 | |||
| 53 | /** Fixed table name (ignore other settings) |
||
| 54 | * @var string */ |
||
| 55 | protected static $tableName; |
||
| 56 | |||
| 57 | /** The variable(s) used for primary key. |
||
| 58 | * @var string[]|string */ |
||
| 59 | protected static $primaryKey = ['id']; |
||
| 60 | |||
| 61 | /** Fixed column names (ignore other settings) |
||
| 62 | * @var string[] */ |
||
| 63 | protected static $columnAliases = []; |
||
| 64 | |||
| 65 | /** A prefix for column names. |
||
| 66 | * @var string */ |
||
| 67 | protected static $columnPrefix; |
||
| 68 | |||
| 69 | /** Whether or not the primary key is auto incremented. |
||
| 70 | * @var bool */ |
||
| 71 | protected static $autoIncrement = true; |
||
| 72 | |||
| 73 | /** Whether or not the validator for this class is enabled. |
||
| 74 | * @var bool */ |
||
| 75 | protected static $enableValidator = false; |
||
| 76 | |||
| 77 | /** Whether or not the validator for a class got enabled during runtime. |
||
| 78 | * @var bool[] */ |
||
| 79 | protected static $enabledValidators = []; |
||
| 80 | |||
| 81 | /** Relation definitions |
||
| 82 | * @var array */ |
||
| 83 | protected static $relations = []; |
||
| 84 | |||
| 85 | /** The reflections of the classes. |
||
| 86 | * @internal |
||
| 87 | * @var \ReflectionClass[] */ |
||
| 88 | protected static $reflections = []; |
||
| 89 | |||
| 90 | /** The current data of a row. |
||
| 91 | * @var mixed[] */ |
||
| 92 | protected $data = []; |
||
| 93 | |||
| 94 | /** The original data of the row. |
||
| 95 | * @var mixed[] */ |
||
| 96 | protected $originalData = []; |
||
| 97 | |||
| 98 | /** The entity manager from which this entity got created |
||
| 99 | * @var EM */ |
||
| 100 | protected $entityManager; |
||
| 101 | |||
| 102 | /** Related objects for getRelated |
||
| 103 | * @var array */ |
||
| 104 | protected $relatedObjects = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Constructor |
||
| 108 | * |
||
| 109 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 110 | * |
||
| 111 | * @param mixed[] $data The current data |
||
| 112 | * @param EM $entityManager The EntityManager that created this entity |
||
| 113 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 114 | */ |
||
| 115 | 106 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Get a description for this table. |
||
| 127 | * |
||
| 128 | * @return Table|Column[] |
||
| 129 | * @codeCoverageIgnore This is just a proxy |
||
| 130 | */ |
||
| 131 | public static function describe() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the column name of $field |
||
| 138 | * |
||
| 139 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 140 | * to $namingSchemeColumn. |
||
| 141 | * |
||
| 142 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 143 | * the same as getColumnName($name). |
||
| 144 | * |
||
| 145 | * @param string $field |
||
| 146 | * @return string |
||
| 147 | * @throws InvalidConfiguration |
||
| 148 | */ |
||
| 149 | 138 | public static function getColumnName($field) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Get the primary key vars |
||
| 161 | * |
||
| 162 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 163 | * columns. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | 56 | public static function getPrimaryKeyVars() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get the definition for $relation |
||
| 174 | * |
||
| 175 | * It normalize the short definition form and create a Relation object from it. |
||
| 176 | * |
||
| 177 | * @param string $relation |
||
| 178 | * @return Relation |
||
| 179 | * @throws InvalidConfiguration |
||
| 180 | * @throws UndefinedRelation |
||
| 181 | */ |
||
| 182 | 85 | public static function getRelation($relation) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get the table name |
||
| 199 | * |
||
| 200 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 201 | * $tableName. |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | * @throws InvalidName|InvalidConfiguration |
||
| 205 | */ |
||
| 206 | 140 | public static function getTableName() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Check if the table has a auto increment column |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | 14 | public static function isAutoIncremented() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Check if the validator is enabled |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 4 | public static function isValidatorEnabled() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Enable validator |
||
| 239 | * |
||
| 240 | * @param bool $enable |
||
| 241 | */ |
||
| 242 | 1 | public static function enableValidator($enable = true) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Disable validator |
||
| 249 | * |
||
| 250 | * @param bool $disable |
||
| 251 | */ |
||
| 252 | 1 | public static function disableValidator($disable = true) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Validate $value for $field |
||
| 259 | * |
||
| 260 | * @param string $field |
||
| 261 | * @param mixed $value |
||
| 262 | * @return bool|Error |
||
| 263 | * @throws Exception |
||
| 264 | */ |
||
| 265 | 3 | public static function validate($field, $value) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Validate $fields |
||
| 272 | * |
||
| 273 | * $fields has to be an array of $field => $value |
||
| 274 | * |
||
| 275 | * @param array $fields |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | 1 | public static function validateArray(array $fields) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param EM $entityManager |
||
| 289 | * @return self |
||
| 290 | */ |
||
| 291 | 2 | public function setEntityManager(EM $entityManager) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get the value from $var |
||
| 299 | * |
||
| 300 | * If there is a custom getter this method get called instead. |
||
| 301 | * |
||
| 302 | * @param string $var The variable to get |
||
| 303 | * @return mixed|null |
||
| 304 | * @throws IncompletePrimaryKey |
||
| 305 | * @throws InvalidConfiguration |
||
| 306 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 307 | */ |
||
| 308 | 87 | public function __get($var) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Set $var to $value |
||
| 329 | * |
||
| 330 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 331 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 332 | * written and give wrong results for dirty checking. |
||
| 333 | * |
||
| 334 | * The onChange event is called after something got changed. |
||
| 335 | * |
||
| 336 | * @param string $var The variable to change |
||
| 337 | * @param mixed $value The value to store |
||
| 338 | * @throws IncompletePrimaryKey |
||
| 339 | * @throws InvalidConfiguration |
||
| 340 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 341 | */ |
||
| 342 | 18 | public function __set($var, $value) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Get related objects |
||
| 367 | * |
||
| 368 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 369 | * $refresh to true. |
||
| 370 | * |
||
| 371 | * @param string $relation |
||
| 372 | * @param bool $refresh |
||
| 373 | * @return mixed |
||
| 374 | * @throws Exceptions\NoConnection |
||
| 375 | * @throws Exceptions\NoEntity |
||
| 376 | * @throws IncompletePrimaryKey |
||
| 377 | * @throws InvalidConfiguration |
||
| 378 | * @throws NoEntityManager |
||
| 379 | * @throws UndefinedRelation |
||
| 380 | */ |
||
| 381 | 11 | public function getRelated($relation, $refresh = false) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Set $relation to $entity |
||
| 392 | * |
||
| 393 | * This method is only for the owner of a relation. |
||
| 394 | * |
||
| 395 | * @param string $relation |
||
| 396 | * @param Entity $entity |
||
| 397 | * @throws IncompletePrimaryKey |
||
| 398 | * @throws InvalidRelation |
||
| 399 | */ |
||
| 400 | 7 | public function setRelated($relation, Entity $entity = null) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Add relations for $relation to $entities |
||
| 409 | * |
||
| 410 | * This method is only for many-to-many relations. |
||
| 411 | * |
||
| 412 | * This method does not take care about already existing relations and will fail hard. |
||
| 413 | * |
||
| 414 | * @param string $relation |
||
| 415 | * @param Entity[] $entities |
||
| 416 | * @throws NoEntityManager |
||
| 417 | */ |
||
| 418 | 8 | public function addRelated($relation, array $entities) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Delete relations for $relation to $entities |
||
| 434 | * |
||
| 435 | * This method is only for many-to-many relations. |
||
| 436 | * |
||
| 437 | * @param string $relation |
||
| 438 | * @param Entity[] $entities |
||
| 439 | * @throws NoEntityManager |
||
| 440 | */ |
||
| 441 | 8 | public function deleteRelated($relation, $entities) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Resets the entity or $var to original data |
||
| 457 | * |
||
| 458 | * @param string $var Reset only this variable or all variables |
||
| 459 | * @throws InvalidConfiguration |
||
| 460 | */ |
||
| 461 | 8 | public function reset($var = null) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Save the entity to EntityManager |
||
| 478 | * |
||
| 479 | * @return Entity |
||
| 480 | * @throws Exceptions\NoConnection |
||
| 481 | * @throws Exceptions\NoEntity |
||
| 482 | * @throws Exceptions\NotScalar |
||
| 483 | * @throws Exceptions\UnsupportedDriver |
||
| 484 | * @throws IncompletePrimaryKey |
||
| 485 | * @throws InvalidConfiguration |
||
| 486 | * @throws InvalidName |
||
| 487 | * @throws NoEntityManager |
||
| 488 | */ |
||
| 489 | 12 | public function save() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Checks if entity or $var got changed |
||
| 535 | * |
||
| 536 | * @param string $var Check only this variable or all variables |
||
| 537 | * @return bool |
||
| 538 | * @throws InvalidConfiguration |
||
| 539 | */ |
||
| 540 | 18 | public function isDirty($var = null) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Empty event handler |
||
| 556 | * |
||
| 557 | * Get called when something is changed with magic setter. |
||
| 558 | * |
||
| 559 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
||
| 560 | * @param mixed $oldValue The old value of the variable |
||
| 561 | * @param mixed $value The new value of the variable |
||
| 562 | */ |
||
| 563 | 6 | public function onChange($var, $oldValue, $value) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Empty event handler |
||
| 569 | * |
||
| 570 | * Get called when the entity get initialized. |
||
| 571 | * |
||
| 572 | * @param bool $new Whether or not the entity is new or from database |
||
| 573 | */ |
||
| 574 | 105 | public function onInit($new) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Empty event handler |
||
| 580 | * |
||
| 581 | * Get called before the entity get updated in database. |
||
| 582 | */ |
||
| 583 | 3 | public function preUpdate() |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Empty event handler |
||
| 589 | * |
||
| 590 | * Get called before the entity get inserted in database. |
||
| 591 | */ |
||
| 592 | 3 | public function prePersist() |
|
| 595 | |||
| 596 | |||
| 597 | // DEPRECATED stuff |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Empty event handler |
||
| 601 | * |
||
| 602 | * Get called after the entity got inserted in database. |
||
| 603 | */ |
||
| 604 | 5 | public function postPersist() |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Empty event handler |
||
| 610 | * |
||
| 611 | * Get called after the entity got updated in database. |
||
| 612 | */ |
||
| 613 | 3 | public function postUpdate() |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Fetches related objects |
||
| 619 | * |
||
| 620 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 621 | * |
||
| 622 | * It will throw an error for non owner when the key is incomplete. |
||
| 623 | * |
||
| 624 | * @param string $relation The relation to fetch |
||
| 625 | * @param bool $getAll |
||
| 626 | * @return Entity|Entity[]|EntityFetcher |
||
| 627 | * @throws NoEntityManager |
||
| 628 | */ |
||
| 629 | 19 | public function fetch($relation, $getAll = false) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Get the primary key |
||
| 652 | * |
||
| 653 | * @return array |
||
| 654 | * @throws IncompletePrimaryKey |
||
| 655 | */ |
||
| 656 | 38 | public function getPrimaryKey() |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Get current data |
||
| 671 | * |
||
| 672 | * @return array |
||
| 673 | * @internal |
||
| 674 | */ |
||
| 675 | 20 | public function getData() |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Set new original data |
||
| 682 | * |
||
| 683 | * @param array $data |
||
| 684 | * @internal |
||
| 685 | */ |
||
| 686 | 18 | public function setOriginalData(array $data) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * String representation of data |
||
| 693 | * |
||
| 694 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | 2 | public function serialize() |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Constructs the object |
||
| 704 | * |
||
| 705 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 706 | * @param string $serialized The string representation of data |
||
| 707 | */ |
||
| 708 | 3 | public function unserialize($serialized) |
|
| 714 | |||
| 715 | /** |
||
| 716 | * @return string |
||
| 717 | * @deprecated use getOption from EntityManager |
||
| 718 | * @codeCoverageIgnore deprecated |
||
| 719 | */ |
||
| 720 | public static function getTableNameTemplate() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param string $tableNameTemplate |
||
| 727 | * @deprecated use setOption from EntityManager |
||
| 728 | * @codeCoverageIgnore deprecated |
||
| 729 | */ |
||
| 730 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return string |
||
| 737 | * @deprecated use getOption from EntityManager |
||
| 738 | * @codeCoverageIgnore deprecated |
||
| 739 | */ |
||
| 740 | public static function getNamingSchemeTable() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @param string $namingSchemeTable |
||
| 747 | * @deprecated use setOption from EntityManager |
||
| 748 | * @codeCoverageIgnore deprecated |
||
| 749 | */ |
||
| 750 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @return string |
||
| 757 | * @deprecated use getOption from EntityManager |
||
| 758 | * @codeCoverageIgnore deprecated |
||
| 759 | */ |
||
| 760 | public static function getNamingSchemeColumn() |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @param string $namingSchemeColumn |
||
| 767 | * @deprecated use setOption from EntityManager |
||
| 768 | * @codeCoverageIgnore deprecated |
||
| 769 | */ |
||
| 770 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @return string |
||
| 777 | * @deprecated use getOption from EntityManager |
||
| 778 | * @codeCoverageIgnore deprecated |
||
| 779 | */ |
||
| 780 | public static function getNamingSchemeMethods() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @param string $namingSchemeMethods |
||
| 787 | * @deprecated use setOption from EntityManager |
||
| 788 | * @codeCoverageIgnore deprecated |
||
| 789 | */ |
||
| 790 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 794 | } |
||
| 795 |