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 | /** Relation definitions |
||
| 74 | * @var array */ |
||
| 75 | protected static $relations = []; |
||
| 76 | |||
| 77 | /** Calculated table names. |
||
| 78 | * @internal |
||
| 79 | * @var string[] */ |
||
| 80 | protected static $calculatedTableNames = []; |
||
| 81 | |||
| 82 | /** Calculated column names. |
||
| 83 | * @internal |
||
| 84 | * @var string[][] */ |
||
| 85 | protected static $calculatedColumnNames = []; |
||
| 86 | |||
| 87 | /** The reflections of the classes. |
||
| 88 | * @internal |
||
| 89 | * @var \ReflectionClass[] */ |
||
| 90 | protected static $reflections = []; |
||
| 91 | |||
| 92 | /** Fetched table descriptions |
||
| 93 | * @var Table[] */ |
||
| 94 | protected static $tableDescriptions = []; |
||
| 95 | |||
| 96 | /** The current data of a row. |
||
| 97 | * @var mixed[] */ |
||
| 98 | protected $data = []; |
||
| 99 | |||
| 100 | /** The original data of the row. |
||
| 101 | * @var mixed[] */ |
||
| 102 | protected $originalData = []; |
||
| 103 | |||
| 104 | /** The entity manager from which this entity got created |
||
| 105 | * @var EM */ |
||
| 106 | protected $entityManager; |
||
| 107 | |||
| 108 | /** Related objects for getRelated |
||
| 109 | * @var array */ |
||
| 110 | protected $relatedObjects = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Constructor |
||
| 114 | * |
||
| 115 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 116 | * |
||
| 117 | * @param mixed[] $data The current data |
||
| 118 | * @param EM $entityManager The EntityManager that created this entity |
||
| 119 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 120 | */ |
||
| 121 | 106 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get a description for this table. |
||
| 133 | * |
||
| 134 | * @return Table|Column[] |
||
| 135 | * @codeCoverageIgnore This is just a proxy |
||
| 136 | */ |
||
| 137 | public static function describe() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the column name of $field |
||
| 150 | * |
||
| 151 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 152 | * to $namingSchemeColumn. |
||
| 153 | * |
||
| 154 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 155 | * the same as getColumnName($name). |
||
| 156 | * |
||
| 157 | * @param string $field |
||
| 158 | * @return string |
||
| 159 | * @throws InvalidConfiguration |
||
| 160 | */ |
||
| 161 | 139 | public static function getColumnName($field) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get the primary key vars |
||
| 186 | * |
||
| 187 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 188 | * columns. |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | 56 | public static function getPrimaryKeyVars() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get reflection of the entity |
||
| 199 | * |
||
| 200 | * @return \ReflectionClass |
||
| 201 | */ |
||
| 202 | 126 | protected static function getReflection() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get the definition for $relation |
||
| 212 | * |
||
| 213 | * It normalize the short definition form and create a Relation object from it. |
||
| 214 | * |
||
| 215 | * @param string $relation |
||
| 216 | * @return Relation |
||
| 217 | * @throws InvalidConfiguration |
||
| 218 | * @throws UndefinedRelation |
||
| 219 | */ |
||
| 220 | 85 | public static function getRelation($relation) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get the table name |
||
| 237 | * |
||
| 238 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 239 | * $tableName. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * @throws InvalidName|InvalidConfiguration |
||
| 243 | */ |
||
| 244 | 137 | public static function getTableName() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Initialize the validator for this Entity. |
||
| 270 | */ |
||
| 271 | 2 | public static function initValidator() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Check if the table has a auto increment column. |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 14 | public static function isAutoIncremented() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Validate $fields. |
||
| 288 | * |
||
| 289 | * $fields has to be an array of $field => $value |
||
| 290 | * |
||
| 291 | * @param array $fields |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | 1 | public static function validateArray(array $fields) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Validate $value for $field. |
||
| 305 | * |
||
| 306 | * @param string $field |
||
| 307 | * @param mixed $value |
||
| 308 | * @return bool|Error |
||
| 309 | * @throws Exception |
||
| 310 | */ |
||
| 311 | 5 | public static function validate($field, $value) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Check if the validator is initialized. |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | 7 | public static function validatorIsInitialized() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Empty event handler |
||
| 332 | * |
||
| 333 | * Get called when the entity get initialized. |
||
| 334 | * |
||
| 335 | * @param bool $new Whether or not the entity is new or from database |
||
| 336 | */ |
||
| 337 | 105 | public function onInit($new) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param EM $entityManager |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | 2 | public function setEntityManager(EM $entityManager) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get the value from $var |
||
| 353 | * |
||
| 354 | * If there is a custom getter this method get called instead. |
||
| 355 | * |
||
| 356 | * @param string $var The variable to get |
||
| 357 | * @return mixed|null |
||
| 358 | * @throws IncompletePrimaryKey |
||
| 359 | * @throws InvalidConfiguration |
||
| 360 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 361 | */ |
||
| 362 | 87 | public function __get($var) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Set $var to $value |
||
| 383 | * |
||
| 384 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 385 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 386 | * written and give wrong results for dirty checking. |
||
| 387 | * |
||
| 388 | * The onChange event is called after something got changed. |
||
| 389 | * |
||
| 390 | * @param string $var The variable to change |
||
| 391 | * @param mixed $value The value to store |
||
| 392 | * @throws IncompletePrimaryKey |
||
| 393 | * @throws InvalidConfiguration |
||
| 394 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 395 | */ |
||
| 396 | 18 | public function __set($var, $value) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Empty event handler |
||
| 421 | * |
||
| 422 | * Get called when something is changed with magic setter. |
||
| 423 | * |
||
| 424 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
||
| 425 | * @param mixed $oldValue The old value of the variable |
||
| 426 | * @param mixed $value The new value of the variable |
||
| 427 | */ |
||
| 428 | 6 | public function onChange($var, $oldValue, $value) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Get related objects |
||
| 434 | * |
||
| 435 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 436 | * $refresh to true. |
||
| 437 | * |
||
| 438 | * @param string $relation |
||
| 439 | * @param bool $refresh |
||
| 440 | * @return mixed |
||
| 441 | * @throws Exceptions\NoConnection |
||
| 442 | * @throws Exceptions\NoEntity |
||
| 443 | * @throws IncompletePrimaryKey |
||
| 444 | * @throws InvalidConfiguration |
||
| 445 | * @throws NoEntityManager |
||
| 446 | * @throws UndefinedRelation |
||
| 447 | */ |
||
| 448 | 11 | public function getRelated($relation, $refresh = false) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Set $relation to $entity |
||
| 459 | * |
||
| 460 | * This method is only for the owner of a relation. |
||
| 461 | * |
||
| 462 | * @param string $relation |
||
| 463 | * @param Entity $entity |
||
| 464 | * @throws IncompletePrimaryKey |
||
| 465 | * @throws InvalidRelation |
||
| 466 | */ |
||
| 467 | 7 | public function setRelated($relation, Entity $entity = null) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Add relations for $relation to $entities |
||
| 476 | * |
||
| 477 | * This method is only for many-to-many relations. |
||
| 478 | * |
||
| 479 | * This method does not take care about already existing relations and will fail hard. |
||
| 480 | * |
||
| 481 | * @param string $relation |
||
| 482 | * @param Entity[] $entities |
||
| 483 | * @throws NoEntityManager |
||
| 484 | */ |
||
| 485 | 8 | public function addRelated($relation, array $entities) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Delete relations for $relation to $entities |
||
| 501 | * |
||
| 502 | * This method is only for many-to-many relations. |
||
| 503 | * |
||
| 504 | * @param string $relation |
||
| 505 | * @param Entity[] $entities |
||
| 506 | * @throws NoEntityManager |
||
| 507 | */ |
||
| 508 | 8 | public function deleteRelated($relation, $entities) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Resets the entity or $var to original data |
||
| 524 | * |
||
| 525 | * @param string $var Reset only this variable or all variables |
||
| 526 | * @throws InvalidConfiguration |
||
| 527 | */ |
||
| 528 | 8 | public function reset($var = null) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Save the entity to EntityManager |
||
| 545 | * |
||
| 546 | * @return Entity |
||
| 547 | * @throws Exceptions\NoConnection |
||
| 548 | * @throws Exceptions\NoEntity |
||
| 549 | * @throws Exceptions\NotScalar |
||
| 550 | * @throws Exceptions\UnsupportedDriver |
||
| 551 | * @throws IncompletePrimaryKey |
||
| 552 | * @throws InvalidConfiguration |
||
| 553 | * @throws InvalidName |
||
| 554 | * @throws NoEntityManager |
||
| 555 | */ |
||
| 556 | 12 | public function save() |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Checks if entity or $var got changed |
||
| 602 | * |
||
| 603 | * @param string $var Check only this variable or all variables |
||
| 604 | * @return bool |
||
| 605 | * @throws InvalidConfiguration |
||
| 606 | */ |
||
| 607 | 18 | public function isDirty($var = null) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Empty event handler |
||
| 623 | * |
||
| 624 | * Get called before the entity get updated in database. |
||
| 625 | */ |
||
| 626 | 3 | public function preUpdate() |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Empty event handler |
||
| 632 | * |
||
| 633 | * Get called before the entity get inserted in database. |
||
| 634 | */ |
||
| 635 | 3 | public function prePersist() |
|
| 638 | |||
| 639 | |||
| 640 | // DEPRECATED stuff |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Empty event handler |
||
| 644 | * |
||
| 645 | * Get called after the entity got inserted in database. |
||
| 646 | */ |
||
| 647 | 5 | public function postPersist() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Empty event handler |
||
| 653 | * |
||
| 654 | * Get called after the entity got updated in database. |
||
| 655 | */ |
||
| 656 | 3 | public function postUpdate() |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Fetches related objects |
||
| 662 | * |
||
| 663 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 664 | * |
||
| 665 | * It will throw an error for non owner when the key is incomplete. |
||
| 666 | * |
||
| 667 | * @param string $relation The relation to fetch |
||
| 668 | * @param bool $getAll |
||
| 669 | * @return Entity|Entity[]|EntityFetcher |
||
| 670 | * @throws NoEntityManager |
||
| 671 | */ |
||
| 672 | 19 | public function fetch($relation, $getAll = false) |
|
| 692 | |||
| 693 | /** |
||
| 694 | * Get the primary key |
||
| 695 | * |
||
| 696 | * @return array |
||
| 697 | * @throws IncompletePrimaryKey |
||
| 698 | */ |
||
| 699 | 38 | public function getPrimaryKey() |
|
| 711 | |||
| 712 | /** |
||
| 713 | * Get current data |
||
| 714 | * |
||
| 715 | * @return array |
||
| 716 | * @internal |
||
| 717 | */ |
||
| 718 | 20 | public function getData() |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Set new original data |
||
| 725 | * |
||
| 726 | * @param array $data |
||
| 727 | * @internal |
||
| 728 | */ |
||
| 729 | 18 | public function setOriginalData(array $data) |
|
| 733 | |||
| 734 | /** |
||
| 735 | * String representation of data |
||
| 736 | * |
||
| 737 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 738 | * @return string |
||
| 739 | */ |
||
| 740 | 2 | public function serialize() |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Constructs the object |
||
| 747 | * |
||
| 748 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 749 | * @param string $serialized The string representation of data |
||
| 750 | */ |
||
| 751 | 3 | public function unserialize($serialized) |
|
| 757 | |||
| 758 | /** |
||
| 759 | * @return string |
||
| 760 | * @deprecated use getOption from EntityManager |
||
| 761 | * @codeCoverageIgnore deprecated |
||
| 762 | */ |
||
| 763 | public static function getTableNameTemplate() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @param string $tableNameTemplate |
||
| 770 | * @deprecated use setOption from EntityManager |
||
| 771 | * @codeCoverageIgnore deprecated |
||
| 772 | */ |
||
| 773 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @return string |
||
| 780 | * @deprecated use getOption from EntityManager |
||
| 781 | * @codeCoverageIgnore deprecated |
||
| 782 | */ |
||
| 783 | public static function getNamingSchemeTable() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @param string $namingSchemeTable |
||
| 790 | * @deprecated use setOption from EntityManager |
||
| 791 | * @codeCoverageIgnore deprecated |
||
| 792 | */ |
||
| 793 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @return string |
||
| 800 | * @deprecated use getOption from EntityManager |
||
| 801 | * @codeCoverageIgnore deprecated |
||
| 802 | */ |
||
| 803 | public static function getNamingSchemeColumn() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @param string $namingSchemeColumn |
||
| 810 | * @deprecated use setOption from EntityManager |
||
| 811 | * @codeCoverageIgnore deprecated |
||
| 812 | */ |
||
| 813 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @return string |
||
| 820 | * @deprecated use getOption from EntityManager |
||
| 821 | * @codeCoverageIgnore deprecated |
||
| 822 | */ |
||
| 823 | public static function getNamingSchemeMethods() |
||
| 827 | |||
| 828 | /** |
||
| 829 | * @param string $namingSchemeMethods |
||
| 830 | * @deprecated use setOption from EntityManager |
||
| 831 | * @codeCoverageIgnore deprecated |
||
| 832 | */ |
||
| 833 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 837 | } |
||
| 838 |