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 | const OPT_RELATION_CLASS = 'class'; |
||
| 33 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 34 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 35 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 36 | const OPT_RELATION_TABLE = 'table'; |
||
| 37 | |||
| 38 | const CARDINALITY_ONE = 'one'; |
||
| 39 | const CARDINALITY_MANY = 'many'; |
||
| 40 | |||
| 41 | /** The template to use to calculate the table name. |
||
| 42 | * @var string */ |
||
| 43 | protected static $tableNameTemplate = '%short%'; |
||
| 44 | |||
| 45 | /** The naming scheme to use for table names. |
||
| 46 | * @var string */ |
||
| 47 | protected static $namingSchemeTable = 'snake_lower'; |
||
| 48 | |||
| 49 | /** The naming scheme to use for column names. |
||
| 50 | * @var string */ |
||
| 51 | protected static $namingSchemeColumn = 'snake_lower'; |
||
| 52 | |||
| 53 | /** The naming scheme to use for method names. |
||
| 54 | * @var string */ |
||
| 55 | protected static $namingSchemeMethods = 'camelCase'; |
||
| 56 | |||
| 57 | /** Whether or not the naming got used |
||
| 58 | * @var bool */ |
||
| 59 | protected static $namingUsed = false; |
||
| 60 | |||
| 61 | /** Fixed table name (ignore other settings) |
||
| 62 | * @var string */ |
||
| 63 | protected static $tableName; |
||
| 64 | |||
| 65 | /** The variable(s) used for primary key. |
||
| 66 | * @var string[]|string */ |
||
| 67 | protected static $primaryKey = ['id']; |
||
| 68 | |||
| 69 | /** Fixed column names (ignore other settings) |
||
| 70 | * @var string[] */ |
||
| 71 | protected static $columnAliases = []; |
||
| 72 | |||
| 73 | /** A prefix for column names. |
||
| 74 | * @var string */ |
||
| 75 | protected static $columnPrefix; |
||
| 76 | |||
| 77 | /** Whether or not the primary key is auto incremented. |
||
| 78 | * @var bool */ |
||
| 79 | protected static $autoIncrement = true; |
||
| 80 | |||
| 81 | /** Relation definitions |
||
| 82 | * @var array */ |
||
| 83 | protected static $relations = []; |
||
| 84 | |||
| 85 | /** The current data of a row. |
||
| 86 | * @var mixed[] */ |
||
| 87 | protected $data = []; |
||
| 88 | |||
| 89 | /** The original data of the row. |
||
| 90 | * @var mixed[] */ |
||
| 91 | protected $originalData = []; |
||
| 92 | |||
| 93 | /** The entity manager from which this entity got created |
||
| 94 | * @var EntityManager*/ |
||
| 95 | protected $entityManager; |
||
| 96 | |||
| 97 | /** Related objects for getRelated |
||
| 98 | * @var array */ |
||
| 99 | protected $relatedObjects = []; |
||
| 100 | |||
| 101 | /** Calculated table names. |
||
| 102 | * @internal |
||
| 103 | * @var string[] */ |
||
| 104 | protected static $calculatedTableNames = []; |
||
| 105 | |||
| 106 | /** Calculated column names. |
||
| 107 | * @internal |
||
| 108 | * @var string[][] */ |
||
| 109 | protected static $calculatedColumnNames = []; |
||
| 110 | |||
| 111 | /** The reflections of the classes. |
||
| 112 | * @internal |
||
| 113 | * @var \ReflectionClass[] */ |
||
| 114 | protected static $reflections = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the table name |
||
| 118 | * |
||
| 119 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 120 | * $tableName. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | * @throws InvalidName|InvalidConfiguration |
||
| 124 | */ |
||
| 125 | 128 | public static function getTableName() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Get the column name of $name |
||
| 178 | * |
||
| 179 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 180 | * to $namingSchemeColumn. |
||
| 181 | * |
||
| 182 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to exactly |
||
| 183 | * the same as getColumnName($name). |
||
| 184 | * |
||
| 185 | * @param string $var |
||
| 186 | * @return string |
||
| 187 | * @throws InvalidConfiguration |
||
| 188 | */ |
||
| 189 | 131 | public static function getColumnName($var) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Get the definition for $relation |
||
| 216 | * |
||
| 217 | * It normalize the short definition form and create a Relation object from it. |
||
| 218 | * |
||
| 219 | * @param string $relation |
||
| 220 | * @return Relation |
||
| 221 | * @throws InvalidConfiguration |
||
| 222 | * @throws UndefinedRelation |
||
| 223 | */ |
||
| 224 | 79 | public static function getRelation($relation) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | 118 | public static function getTableNameTemplate() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $tableNameTemplate |
||
| 315 | * @throws InvalidConfiguration |
||
| 316 | */ |
||
| 317 | 52 | public static function setTableNameTemplate($tableNameTemplate) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 115 | public static function getNamingSchemeTable() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $namingSchemeTable |
||
| 336 | * @throws InvalidConfiguration |
||
| 337 | */ |
||
| 338 | 52 | public static function setNamingSchemeTable($namingSchemeTable) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 129 | public static function getNamingSchemeColumn() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $namingSchemeColumn |
||
| 357 | * @throws InvalidConfiguration |
||
| 358 | */ |
||
| 359 | 27 | public static function setNamingSchemeColumn($namingSchemeColumn) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 85 | public static function getNamingSchemeMethods() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $namingSchemeMethods |
||
| 378 | * @throws InvalidConfiguration |
||
| 379 | */ |
||
| 380 | 3 | public static function setNamingSchemeMethods($namingSchemeMethods) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get the primary key vars |
||
| 391 | * |
||
| 392 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 393 | * columns. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | 56 | public static function getPrimaryKeyVars() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Check if the table has a auto increment column. |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | 14 | public static function isAutoIncremented() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Enforce $namingScheme to $name |
||
| 414 | * |
||
| 415 | * Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
||
| 416 | * and UPPER. |
||
| 417 | * |
||
| 418 | * @param string $name The name of the var / column |
||
| 419 | * @param string $namingScheme The naming scheme to use |
||
| 420 | * @return string |
||
| 421 | * @throws InvalidConfiguration |
||
| 422 | */ |
||
| 423 | 202 | protected static function forceNamingScheme($name, $namingScheme) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Get reflection of the entity |
||
| 475 | * |
||
| 476 | * @return \ReflectionClass |
||
| 477 | */ |
||
| 478 | 117 | protected static function getReflection() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Constructor |
||
| 488 | * |
||
| 489 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 490 | * |
||
| 491 | * @param mixed[] $data The current data |
||
| 492 | * @param EntityManager $entityManager The EntityManager that created this entity |
||
| 493 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 494 | */ |
||
| 495 | 102 | final public function __construct(array $data = [], EntityManager $entityManager = null, $fromDatabase = false) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param EntityManager $entityManager |
||
| 507 | * @return self |
||
| 508 | */ |
||
| 509 | 1 | public function setEntityManager(EntityManager $entityManager) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Set $var to $value |
||
| 517 | * |
||
| 518 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 519 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 520 | * written and give wrong results for dirty checking. |
||
| 521 | * |
||
| 522 | * The onChange event is called after something got changed. |
||
| 523 | * |
||
| 524 | * @param string $var The variable to change |
||
| 525 | * @param mixed $value The value to store |
||
| 526 | * @throws IncompletePrimaryKey |
||
| 527 | * @throws InvalidConfiguration |
||
| 528 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 529 | */ |
||
| 530 | 18 | public function __set($var, $value) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Get the value from $var |
||
| 554 | * |
||
| 555 | * If there is a custom getter this method get called instead. |
||
| 556 | * |
||
| 557 | * @param string $var The variable to get |
||
| 558 | * @return mixed|null |
||
| 559 | * @throws IncompletePrimaryKey |
||
| 560 | * @throws InvalidConfiguration |
||
| 561 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 562 | */ |
||
| 563 | 84 | public function __get($var) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Get related objects |
||
| 582 | * |
||
| 583 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 584 | * $refresh to true. |
||
| 585 | * |
||
| 586 | * @param string $relation |
||
| 587 | * @param bool $refresh |
||
| 588 | * @return mixed |
||
| 589 | * @throws Exceptions\NoConnection |
||
| 590 | * @throws Exceptions\NoEntity |
||
| 591 | * @throws IncompletePrimaryKey |
||
| 592 | * @throws InvalidConfiguration |
||
| 593 | * @throws NoEntityManager |
||
| 594 | * @throws UndefinedRelation |
||
| 595 | */ |
||
| 596 | 10 | public function getRelated($relation, $refresh = false) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Set $relation to $entity |
||
| 607 | * |
||
| 608 | * This method is only for the owner of a relation. |
||
| 609 | * |
||
| 610 | * @param string $relation |
||
| 611 | * @param Entity $entity |
||
| 612 | * @throws IncompletePrimaryKey |
||
| 613 | * @throws InvalidRelation |
||
| 614 | */ |
||
| 615 | 6 | public function setRelated($relation, Entity $entity = null) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Add relations for $relation to $entities |
||
| 624 | * |
||
| 625 | * This method is only for many-to-many relations. |
||
| 626 | * |
||
| 627 | * This method does not take care about already existing relations and will fail hard. |
||
| 628 | * |
||
| 629 | * @param string $relation |
||
| 630 | * @param Entity[] $entities |
||
| 631 | * @throws IncompletePrimaryKey |
||
| 632 | * @throws InvalidRelation |
||
| 633 | */ |
||
| 634 | 7 | public function addRelated($relation, array $entities) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Delete relations for $relation to $entities |
||
| 641 | * |
||
| 642 | * This method is only for many-to-many relations. |
||
| 643 | * |
||
| 644 | * @param string $relation |
||
| 645 | * @param Entity[] $entities |
||
| 646 | * @throws IncompletePrimaryKey |
||
| 647 | * @throws InvalidRelation |
||
| 648 | */ |
||
| 649 | 7 | public function deleteRelations($relation, $entities) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Checks if entity or $var got changed |
||
| 656 | * |
||
| 657 | * @param string $var Check only this variable or all variables |
||
| 658 | * @return bool |
||
| 659 | * @throws InvalidConfiguration |
||
| 660 | */ |
||
| 661 | 18 | public function isDirty($var = null) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Resets the entity or $var to original data |
||
| 676 | * |
||
| 677 | * @param string $var Reset only this variable or all variables |
||
| 678 | * @throws InvalidConfiguration |
||
| 679 | */ |
||
| 680 | 8 | public function reset($var = null) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Save the entity to $entityManager |
||
| 697 | * |
||
| 698 | * @param EntityManager $entityManager |
||
| 699 | * @return Entity |
||
| 700 | * @throws Exceptions\NoConnection |
||
| 701 | * @throws Exceptions\NoEntity |
||
| 702 | * @throws Exceptions\NotScalar |
||
| 703 | * @throws Exceptions\UnsupportedDriver |
||
| 704 | * @throws IncompletePrimaryKey |
||
| 705 | * @throws InvalidConfiguration |
||
| 706 | * @throws InvalidName |
||
| 707 | * @throws NoEntityManager |
||
| 708 | */ |
||
| 709 | 13 | public function save(EntityManager $entityManager = null) |
|
| 749 | |||
| 750 | /** |
||
| 751 | * Fetches related objects |
||
| 752 | * |
||
| 753 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 754 | * |
||
| 755 | * It will throw an error for non owner when the key is incomplete. |
||
| 756 | * |
||
| 757 | * @param string $relation The relation to fetch |
||
| 758 | * @param EntityManager $entityManager The EntityManager to use |
||
| 759 | * @return Entity|EntityFetcher|Entity[] |
||
| 760 | * @throws Exceptions\NoConnection |
||
| 761 | * @throws Exceptions\NoEntity |
||
| 762 | * @throws IncompletePrimaryKey |
||
| 763 | * @throws InvalidConfiguration |
||
| 764 | * @throws NoEntityManager |
||
| 765 | * @throws UndefinedRelation |
||
| 766 | */ |
||
| 767 | 19 | public function fetch($relation, EntityManager $entityManager = null, $getAll = false) |
|
| 783 | |||
| 784 | /** |
||
| 785 | * Get the primary key |
||
| 786 | * |
||
| 787 | * @return array |
||
| 788 | * @throws IncompletePrimaryKey |
||
| 789 | */ |
||
| 790 | 38 | public function getPrimaryKey() |
|
| 802 | |||
| 803 | /** |
||
| 804 | * Get current data |
||
| 805 | * |
||
| 806 | * @return array |
||
| 807 | * @internal |
||
| 808 | */ |
||
| 809 | 17 | public function getData() |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Set new original data |
||
| 816 | * |
||
| 817 | * @param array $data |
||
| 818 | * @internal |
||
| 819 | */ |
||
| 820 | 18 | public function setOriginalData(array $data) |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Empty event handler |
||
| 827 | * |
||
| 828 | * Get called when something is changed with magic setter. |
||
| 829 | * |
||
| 830 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
||
| 831 | * @param mixed $oldValue The old value of the variable |
||
| 832 | * @param mixed $value The new value of the variable |
||
| 833 | */ |
||
| 834 | 5 | public function onChange($var, $oldValue, $value) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Empty event handler |
||
| 840 | * |
||
| 841 | * Get called when the entity get initialized. |
||
| 842 | * |
||
| 843 | * @param bool $new Whether or not the entity is new or from database |
||
| 844 | */ |
||
| 845 | 101 | public function onInit($new) |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Empty event handler |
||
| 851 | * |
||
| 852 | * Get called before the entity get inserted in database. |
||
| 853 | */ |
||
| 854 | 3 | public function prePersist() |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Empty event handler |
||
| 860 | * |
||
| 861 | * Get called after the entity got inserted in database. |
||
| 862 | */ |
||
| 863 | 5 | public function postPersist() |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Empty event handler |
||
| 869 | * |
||
| 870 | * Get called before the entity get updated in database. |
||
| 871 | */ |
||
| 872 | 3 | public function preUpdate() |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Empty event handler |
||
| 878 | * |
||
| 879 | * Get called after the entity got updated in database. |
||
| 880 | */ |
||
| 881 | 3 | public function postUpdate() |
|
| 884 | |||
| 885 | /** |
||
| 886 | * String representation of data |
||
| 887 | * |
||
| 888 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 889 | * @return string |
||
| 890 | */ |
||
| 891 | 1 | public function serialize() |
|
| 895 | |||
| 896 | /** |
||
| 897 | * Constructs the object |
||
| 898 | * |
||
| 899 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 900 | * @param string $serialized The string representation of data |
||
| 901 | */ |
||
| 902 | 2 | public function unserialize($serialized) |
|
| 907 | } |
||
| 908 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: