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 | abstract class Entity implements \Serializable |
||
| 27 | { |
||
| 28 | const OPT_RELATION_CLASS = 'class'; |
||
| 29 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 30 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 31 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 32 | const OPT_RELATION_TABLE = 'table'; |
||
| 33 | |||
| 34 | /** The template to use to calculate the table name. |
||
| 35 | * @var string */ |
||
| 36 | protected static $tableNameTemplate = '%short%'; |
||
| 37 | |||
| 38 | /** The naming scheme to use for table names. |
||
| 39 | * @var string */ |
||
| 40 | protected static $namingSchemeTable = 'snake_lower'; |
||
| 41 | |||
| 42 | /** The naming scheme to use for column names. |
||
| 43 | * @var string */ |
||
| 44 | protected static $namingSchemeColumn = 'snake_lower'; |
||
| 45 | |||
| 46 | /** The naming scheme to use for method names. |
||
| 47 | * @var string */ |
||
| 48 | protected static $namingSchemeMethods = 'camelCase'; |
||
| 49 | |||
| 50 | /** Whether or not the naming got used |
||
| 51 | * @var bool */ |
||
| 52 | protected static $namingUsed = false; |
||
| 53 | |||
| 54 | /** Fixed table name (ignore other settings) |
||
| 55 | * @var string */ |
||
| 56 | protected static $tableName; |
||
| 57 | |||
| 58 | /** The variable(s) used for primary key. |
||
| 59 | * @var string[]|string */ |
||
| 60 | protected static $primaryKey = ['id']; |
||
| 61 | |||
| 62 | /** Fixed column names (ignore other settings) |
||
| 63 | * @var string[] */ |
||
| 64 | protected static $columnAliases = []; |
||
| 65 | |||
| 66 | /** A prefix for column names. |
||
| 67 | * @var string */ |
||
| 68 | protected static $columnPrefix; |
||
| 69 | |||
| 70 | /** Whether or not the primary key is auto incremented. |
||
| 71 | * @var bool */ |
||
| 72 | protected static $autoIncrement = true; |
||
| 73 | |||
| 74 | /** Relation definitions |
||
| 75 | * @var array */ |
||
| 76 | protected static $relations = []; |
||
| 77 | |||
| 78 | /** The current data of a row. |
||
| 79 | * @var mixed[] */ |
||
| 80 | protected $data = []; |
||
| 81 | |||
| 82 | /** The original data of the row. |
||
| 83 | * @var mixed[] */ |
||
| 84 | protected $originalData = []; |
||
| 85 | |||
| 86 | /** The entity manager from which this entity got created |
||
| 87 | * @var EntityManager*/ |
||
| 88 | protected $entityManager; |
||
| 89 | |||
| 90 | /** Related objects for getRelated |
||
| 91 | * @var array */ |
||
| 92 | protected $relatedObjects = []; |
||
| 93 | |||
| 94 | /** Calculated table names. |
||
| 95 | * @internal |
||
| 96 | * @var string[] */ |
||
| 97 | protected static $calculatedTableNames = []; |
||
| 98 | |||
| 99 | /** Calculated column names. |
||
| 100 | * @internal |
||
| 101 | * @var string[][] */ |
||
| 102 | protected static $calculatedColumnNames = []; |
||
| 103 | |||
| 104 | /** The reflections of the classes. |
||
| 105 | * @internal |
||
| 106 | * @var \ReflectionClass[] */ |
||
| 107 | protected static $reflections = []; |
||
| 108 | |||
| 109 | /** The fetched column descriptions for the table. |
||
| 110 | * @internal |
||
| 111 | * @var Column[][] */ |
||
| 112 | protected static $descriptions; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the table name |
||
| 116 | * |
||
| 117 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 118 | * $tableName. |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | * @throws InvalidName|InvalidConfiguration |
||
| 122 | */ |
||
| 123 | 137 | public static function getTableName() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get the column name of $name |
||
| 176 | * |
||
| 177 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 178 | * to $namingSchemeColumn. |
||
| 179 | * |
||
| 180 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to exactly |
||
| 181 | * the same as getColumnName($name). |
||
| 182 | * |
||
| 183 | * @param string $var |
||
| 184 | * @return string |
||
| 185 | * @throws InvalidConfiguration |
||
| 186 | */ |
||
| 187 | 136 | public static function getColumnName($var) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get the definition for $relation |
||
| 214 | * |
||
| 215 | * It normalize the short definition form and create a Relation object from it. |
||
| 216 | * |
||
| 217 | * @param string $relation |
||
| 218 | * @return Relation |
||
| 219 | * @throws InvalidConfiguration |
||
| 220 | * @throws UndefinedRelation |
||
| 221 | */ |
||
| 222 | 84 | public static function getRelation($relation) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 127 | public static function getTableNameTemplate() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $tableNameTemplate |
||
| 247 | * @throws InvalidConfiguration |
||
| 248 | */ |
||
| 249 | 52 | public static function setTableNameTemplate($tableNameTemplate) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | 124 | public static function getNamingSchemeTable() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $namingSchemeTable |
||
| 268 | * @throws InvalidConfiguration |
||
| 269 | */ |
||
| 270 | 52 | public static function setNamingSchemeTable($namingSchemeTable) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 134 | public static function getNamingSchemeColumn() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $namingSchemeColumn |
||
| 289 | * @throws InvalidConfiguration |
||
| 290 | */ |
||
| 291 | 27 | public static function setNamingSchemeColumn($namingSchemeColumn) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 88 | public static function getNamingSchemeMethods() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $namingSchemeMethods |
||
| 310 | * @throws InvalidConfiguration |
||
| 311 | */ |
||
| 312 | 3 | public static function setNamingSchemeMethods($namingSchemeMethods) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get the primary key vars |
||
| 323 | * |
||
| 324 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 325 | * columns. |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | 56 | public static function getPrimaryKeyVars() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Check if the table has a auto increment column. |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | 14 | public static function isAutoIncremented() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get an array of Columns for this table. |
||
| 346 | * |
||
| 347 | * @param EntityManager $em |
||
| 348 | * @return mixed |
||
| 349 | * @codeCoverageIgnore This is just a proxy |
||
| 350 | */ |
||
| 351 | public static function describe(EntityManager $em) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Enforce $namingScheme to $name |
||
| 358 | * |
||
| 359 | * Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
||
| 360 | * and UPPER. |
||
| 361 | * |
||
| 362 | * @param string $name The name of the var / column |
||
| 363 | * @param string $namingScheme The naming scheme to use |
||
| 364 | * @return string |
||
| 365 | * @throws InvalidConfiguration |
||
| 366 | */ |
||
| 367 | 214 | protected static function forceNamingScheme($name, $namingScheme) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get reflection of the entity |
||
| 419 | * |
||
| 420 | * @return \ReflectionClass |
||
| 421 | */ |
||
| 422 | 126 | protected static function getReflection() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Constructor |
||
| 432 | * |
||
| 433 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 434 | * |
||
| 435 | * @param mixed[] $data The current data |
||
| 436 | * @param EntityManager $entityManager The EntityManager that created this entity |
||
| 437 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 438 | */ |
||
| 439 | 110 | final public function __construct(array $data = [], EntityManager $entityManager = null, $fromDatabase = false) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param EntityManager $entityManager |
||
| 451 | * @return self |
||
| 452 | */ |
||
| 453 | 1 | public function setEntityManager(EntityManager $entityManager) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Set $var to $value |
||
| 461 | * |
||
| 462 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 463 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 464 | * written and give wrong results for dirty checking. |
||
| 465 | * |
||
| 466 | * The onChange event is called after something got changed. |
||
| 467 | * |
||
| 468 | * @param string $var The variable to change |
||
| 469 | * @param mixed $value The value to store |
||
| 470 | * @throws IncompletePrimaryKey |
||
| 471 | * @throws InvalidConfiguration |
||
| 472 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 473 | */ |
||
| 474 | 19 | public function __set($var, $value) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get the value from $var |
||
| 498 | * |
||
| 499 | * If there is a custom getter this method get called instead. |
||
| 500 | * |
||
| 501 | * @param string $var The variable to get |
||
| 502 | * @return mixed|null |
||
| 503 | * @throws IncompletePrimaryKey |
||
| 504 | * @throws InvalidConfiguration |
||
| 505 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 506 | */ |
||
| 507 | 87 | public function __get($var) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Get related objects |
||
| 526 | * |
||
| 527 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 528 | * $refresh to true. |
||
| 529 | * |
||
| 530 | * @param string $relation |
||
| 531 | * @param bool $refresh |
||
| 532 | * @return mixed |
||
| 533 | * @throws Exceptions\NoConnection |
||
| 534 | * @throws Exceptions\NoEntity |
||
| 535 | * @throws IncompletePrimaryKey |
||
| 536 | * @throws InvalidConfiguration |
||
| 537 | * @throws NoEntityManager |
||
| 538 | * @throws UndefinedRelation |
||
| 539 | */ |
||
| 540 | 11 | public function getRelated($relation, $refresh = false) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Set $relation to $entity |
||
| 551 | * |
||
| 552 | * This method is only for the owner of a relation. |
||
| 553 | * |
||
| 554 | * @param string $relation |
||
| 555 | * @param Entity $entity |
||
| 556 | * @throws IncompletePrimaryKey |
||
| 557 | * @throws InvalidRelation |
||
| 558 | */ |
||
| 559 | 7 | public function setRelated($relation, Entity $entity = null) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Add relations for $relation to $entities |
||
| 568 | * |
||
| 569 | * This method is only for many-to-many relations. |
||
| 570 | * |
||
| 571 | * This method does not take care about already existing relations and will fail hard. |
||
| 572 | * |
||
| 573 | * @param string $relation |
||
| 574 | * @param Entity[] $entities |
||
| 575 | * @param EntityManager $entityManager |
||
| 576 | * @throws NoEntityManager |
||
| 577 | */ |
||
| 578 | 9 | public function addRelated($relation, array $entities, EntityManager $entityManager = null) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Delete relations for $relation to $entities |
||
| 591 | * |
||
| 592 | * This method is only for many-to-many relations. |
||
| 593 | * |
||
| 594 | * @param string $relation |
||
| 595 | * @param Entity[] $entities |
||
| 596 | * @param EntityManager $entityManager |
||
| 597 | * @throws NoEntityManager |
||
| 598 | */ |
||
| 599 | 9 | public function deleteRelated($relation, $entities, EntityManager $entityManager = null) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Checks if entity or $var got changed |
||
| 612 | * |
||
| 613 | * @param string $var Check only this variable or all variables |
||
| 614 | * @return bool |
||
| 615 | * @throws InvalidConfiguration |
||
| 616 | */ |
||
| 617 | 18 | public function isDirty($var = null) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Resets the entity or $var to original data |
||
| 633 | * |
||
| 634 | * @param string $var Reset only this variable or all variables |
||
| 635 | * @throws InvalidConfiguration |
||
| 636 | */ |
||
| 637 | 8 | public function reset($var = null) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Save the entity to $entityManager |
||
| 654 | * |
||
| 655 | * @param EntityManager $entityManager |
||
| 656 | * @return Entity |
||
| 657 | * @throws Exceptions\NoConnection |
||
| 658 | * @throws Exceptions\NoEntity |
||
| 659 | * @throws Exceptions\NotScalar |
||
| 660 | * @throws Exceptions\UnsupportedDriver |
||
| 661 | * @throws IncompletePrimaryKey |
||
| 662 | * @throws InvalidConfiguration |
||
| 663 | * @throws InvalidName |
||
| 664 | * @throws NoEntityManager |
||
| 665 | */ |
||
| 666 | 13 | public function save(EntityManager $entityManager = null) |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Fetches related objects |
||
| 709 | * |
||
| 710 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 711 | * |
||
| 712 | * It will throw an error for non owner when the key is incomplete. |
||
| 713 | * |
||
| 714 | * @param string $relation The relation to fetch |
||
| 715 | * @param EntityManager $entityManager The EntityManager to use |
||
| 716 | * @param bool $getAll |
||
| 717 | * @return Entity|Entity[]|EntityFetcher |
||
| 718 | * @throws NoEntityManager |
||
| 719 | */ |
||
| 720 | 19 | public function fetch($relation, EntityManager $entityManager = null, $getAll = false) |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Get the primary key |
||
| 739 | * |
||
| 740 | * @return array |
||
| 741 | * @throws IncompletePrimaryKey |
||
| 742 | */ |
||
| 743 | 38 | public function getPrimaryKey() |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Get current data |
||
| 758 | * |
||
| 759 | * @return array |
||
| 760 | * @internal |
||
| 761 | */ |
||
| 762 | 20 | public function getData() |
|
| 766 | |||
| 767 | /** |
||
| 768 | * Set new original data |
||
| 769 | * |
||
| 770 | * @param array $data |
||
| 771 | * @internal |
||
| 772 | */ |
||
| 773 | 18 | public function setOriginalData(array $data) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Empty event handler |
||
| 780 | * |
||
| 781 | * Get called when something is changed with magic setter. |
||
| 782 | * |
||
| 783 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
||
| 784 | * @param mixed $oldValue The old value of the variable |
||
| 785 | * @param mixed $value The new value of the variable |
||
| 786 | */ |
||
| 787 | 6 | public function onChange($var, $oldValue, $value) |
|
| 790 | |||
| 791 | /** |
||
| 792 | * Empty event handler |
||
| 793 | * |
||
| 794 | * Get called when the entity get initialized. |
||
| 795 | * |
||
| 796 | * @param bool $new Whether or not the entity is new or from database |
||
| 797 | */ |
||
| 798 | 109 | public function onInit($new) |
|
| 801 | |||
| 802 | /** |
||
| 803 | * Empty event handler |
||
| 804 | * |
||
| 805 | * Get called before the entity get inserted in database. |
||
| 806 | */ |
||
| 807 | 3 | public function prePersist() |
|
| 810 | |||
| 811 | /** |
||
| 812 | * Empty event handler |
||
| 813 | * |
||
| 814 | * Get called after the entity got inserted in database. |
||
| 815 | */ |
||
| 816 | 5 | public function postPersist() |
|
| 819 | |||
| 820 | /** |
||
| 821 | * Empty event handler |
||
| 822 | * |
||
| 823 | * Get called before the entity get updated in database. |
||
| 824 | */ |
||
| 825 | 3 | public function preUpdate() |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Empty event handler |
||
| 831 | * |
||
| 832 | * Get called after the entity got updated in database. |
||
| 833 | */ |
||
| 834 | 3 | public function postUpdate() |
|
| 837 | |||
| 838 | /** |
||
| 839 | * String representation of data |
||
| 840 | * |
||
| 841 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 842 | * @return string |
||
| 843 | */ |
||
| 844 | 2 | public function serialize() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Constructs the object |
||
| 851 | * |
||
| 852 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 853 | * @param string $serialized The string representation of data |
||
| 854 | */ |
||
| 855 | 3 | public function unserialize($serialized) |
|
| 860 | } |
||
| 861 |