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 | const CARDINALITY_ONE = 'one'; |
||
| 35 | const CARDINALITY_MANY = 'many'; |
||
| 36 | |||
| 37 | /** The template to use to calculate the table name. |
||
| 38 | * @var string */ |
||
| 39 | protected static $tableNameTemplate = '%short%'; |
||
| 40 | |||
| 41 | /** The naming scheme to use for table names. |
||
| 42 | * @var string */ |
||
| 43 | protected static $namingSchemeTable = 'snake_lower'; |
||
| 44 | |||
| 45 | /** The naming scheme to use for column names. |
||
| 46 | * @var string */ |
||
| 47 | protected static $namingSchemeColumn = 'snake_lower'; |
||
| 48 | |||
| 49 | /** The naming scheme to use for method names. |
||
| 50 | * @var string */ |
||
| 51 | protected static $namingSchemeMethods = 'camelCase'; |
||
| 52 | |||
| 53 | /** Whether or not the naming got used |
||
| 54 | * @var bool */ |
||
| 55 | protected static $namingUsed = false; |
||
| 56 | |||
| 57 | /** Fixed table name (ignore other settings) |
||
| 58 | * @var string */ |
||
| 59 | protected static $tableName; |
||
| 60 | |||
| 61 | /** The variable(s) used for primary key. |
||
| 62 | * @var string[]|string */ |
||
| 63 | protected static $primaryKey = ['id']; |
||
| 64 | |||
| 65 | /** Fixed column names (ignore other settings) |
||
| 66 | * @var string[] */ |
||
| 67 | protected static $columnAliases = []; |
||
| 68 | |||
| 69 | /** A prefix for column names. |
||
| 70 | * @var string */ |
||
| 71 | protected static $columnPrefix; |
||
| 72 | |||
| 73 | /** Whether or not the primary key is auto incremented. |
||
| 74 | * @var bool */ |
||
| 75 | protected static $autoIncrement = true; |
||
| 76 | |||
| 77 | /** Relation definitions |
||
| 78 | * @var array */ |
||
| 79 | protected static $relations = []; |
||
| 80 | |||
| 81 | /** The current data of a row. |
||
| 82 | * @var mixed[] */ |
||
| 83 | protected $data = []; |
||
| 84 | |||
| 85 | /** The original data of the row. |
||
| 86 | * @var mixed[] */ |
||
| 87 | protected $originalData = []; |
||
| 88 | |||
| 89 | /** The entity manager from which this entity got created |
||
| 90 | * @var EntityManager*/ |
||
| 91 | protected $entityManager; |
||
| 92 | |||
| 93 | /** Related objects for getRelated |
||
| 94 | * @var array */ |
||
| 95 | protected $relatedObjects = []; |
||
| 96 | |||
| 97 | /** Calculated table names. |
||
| 98 | * @internal |
||
| 99 | * @var string[] */ |
||
| 100 | protected static $calculatedTableNames = []; |
||
| 101 | |||
| 102 | /** Calculated column names. |
||
| 103 | * @internal |
||
| 104 | * @var string[][] */ |
||
| 105 | protected static $calculatedColumnNames = []; |
||
| 106 | |||
| 107 | /** The reflections of the classes. |
||
| 108 | * @internal |
||
| 109 | * @var \ReflectionClass[] */ |
||
| 110 | protected static $reflections = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the table name |
||
| 114 | * |
||
| 115 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 116 | * $tableName. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | * @throws InvalidName|InvalidConfiguration |
||
| 120 | */ |
||
| 121 | 120 | public static function getTableName() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get the column name of $name |
||
| 175 | * |
||
| 176 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 177 | * to $namingSchemeColumn. |
||
| 178 | * |
||
| 179 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to exactly |
||
| 180 | * the same as getColumnName($name). |
||
| 181 | * |
||
| 182 | * @param string $var |
||
| 183 | * @return string |
||
| 184 | * @throws InvalidConfiguration |
||
| 185 | */ |
||
| 186 | 122 | public static function getColumnName($var) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get the definition for $relation |
||
| 213 | * |
||
| 214 | * It will normalize the definition before. |
||
| 215 | * |
||
| 216 | * The resulting array will have at least `class` and `cardinality`. It may also have the following keys: |
||
| 217 | * `class`, `cardinality`, `reference`, `opponent` and `table` |
||
| 218 | * |
||
| 219 | * @param string $relation |
||
| 220 | * @return array |
||
| 221 | * @throws InvalidConfiguration |
||
| 222 | * @throws UndefinedRelation |
||
| 223 | */ |
||
| 224 | 56 | public static function getRelationDefinition($relation) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 110 | public static function getTableNameTemplate() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $tableNameTemplate |
||
| 293 | * @throws InvalidConfiguration |
||
| 294 | */ |
||
| 295 | 52 | public static function setTableNameTemplate($tableNameTemplate) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | 107 | public static function getNamingSchemeTable() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $namingSchemeTable |
||
| 314 | * @throws InvalidConfiguration |
||
| 315 | */ |
||
| 316 | 52 | public static function setNamingSchemeTable($namingSchemeTable) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 120 | public static function getNamingSchemeColumn() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $namingSchemeColumn |
||
| 335 | * @throws InvalidConfiguration |
||
| 336 | */ |
||
| 337 | 27 | public static function setNamingSchemeColumn($namingSchemeColumn) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | 84 | public static function getNamingSchemeMethods() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $namingSchemeMethods |
||
| 356 | * @throws InvalidConfiguration |
||
| 357 | */ |
||
| 358 | 3 | public static function setNamingSchemeMethods($namingSchemeMethods) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Get the primary key vars |
||
| 369 | * |
||
| 370 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 371 | * columns. |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | 56 | public static function getPrimaryKeyVars() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Check if the table has a auto increment column. |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 14 | public static function isAutoIncremented() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Enforce $namingScheme to $name |
||
| 392 | * |
||
| 393 | * Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
||
| 394 | * and UPPER. |
||
| 395 | * |
||
| 396 | * @param string $name The name of the var / column |
||
| 397 | * @param string $namingScheme The naming scheme to use |
||
| 398 | * @return string |
||
| 399 | * @throws InvalidConfiguration |
||
| 400 | */ |
||
| 401 | 193 | protected static function forceNamingScheme($name, $namingScheme) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Get reflection of the entity |
||
| 453 | * |
||
| 454 | * @return \ReflectionClass |
||
| 455 | */ |
||
| 456 | 109 | protected static function getReflection() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Constructor |
||
| 466 | * |
||
| 467 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 468 | * |
||
| 469 | * @param mixed[] $data The current data |
||
| 470 | * @param EntityManager $entityManager The EntityManager that created this entity |
||
| 471 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 472 | */ |
||
| 473 | 99 | final public function __construct(array $data = [], EntityManager $entityManager = null, $fromDatabase = false) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @param EntityManager $entityManager |
||
| 485 | * @return self |
||
| 486 | */ |
||
| 487 | 1 | public function setEntityManager(EntityManager $entityManager) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Set $var to $value |
||
| 495 | * |
||
| 496 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 497 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 498 | * written and give wrong results for dirty checking. |
||
| 499 | * |
||
| 500 | * The onChange event is called after something got changed. |
||
| 501 | * |
||
| 502 | * @param string $var The variable to change |
||
| 503 | * @param mixed $value The value to store |
||
| 504 | * @throws IncompletePrimaryKey |
||
| 505 | * @throws InvalidConfiguration |
||
| 506 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 507 | */ |
||
| 508 | 18 | public function __set($var, $value) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Get the value from $var |
||
| 532 | * |
||
| 533 | * If there is a custom getter this method get called instead. |
||
| 534 | * |
||
| 535 | * @param string $var The variable to get |
||
| 536 | * @return mixed|null |
||
| 537 | * @throws IncompletePrimaryKey |
||
| 538 | * @throws InvalidConfiguration |
||
| 539 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 540 | */ |
||
| 541 | 83 | public function __get($var) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Get related objects |
||
| 560 | * |
||
| 561 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 562 | * $refresh to true. |
||
| 563 | * |
||
| 564 | * @param string $relation |
||
| 565 | * @param bool $refresh |
||
| 566 | * @return mixed |
||
| 567 | * @throws Exceptions\NoConnection |
||
| 568 | * @throws Exceptions\NoEntity |
||
| 569 | * @throws IncompletePrimaryKey |
||
| 570 | * @throws InvalidConfiguration |
||
| 571 | * @throws NoEntityManager |
||
| 572 | * @throws UndefinedRelation |
||
| 573 | */ |
||
| 574 | 10 | public function getRelated($relation, $refresh = false) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Set $relation to $entity |
||
| 585 | * |
||
| 586 | * This method is only for the owner of a relation. |
||
| 587 | * |
||
| 588 | * @param string $relation |
||
| 589 | * @param Entity $entity |
||
| 590 | * @throws IncompletePrimaryKey |
||
| 591 | * @throws InvalidRelation |
||
| 592 | */ |
||
| 593 | 6 | public function setRelation($relation, Entity $entity = null) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Add relations for $relation to $entities |
||
| 628 | * |
||
| 629 | * This method is only for many-to-many relations. |
||
| 630 | * |
||
| 631 | * This method does not take care about already existing relations and will fail hard. |
||
| 632 | * |
||
| 633 | * @param string $relation |
||
| 634 | * @param Entity[] $entities |
||
| 635 | * @throws IncompletePrimaryKey |
||
| 636 | * @throws InvalidRelation |
||
| 637 | */ |
||
| 638 | 7 | public function addRelations($relation, $entities) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Delete relations for $relation to $entities |
||
| 698 | * |
||
| 699 | * This method is only for many-to-many relations. |
||
| 700 | * |
||
| 701 | * @param string $relation |
||
| 702 | * @param Entity[] $entities |
||
| 703 | * @throws IncompletePrimaryKey |
||
| 704 | * @throws InvalidRelation |
||
| 705 | */ |
||
| 706 | 7 | public function deleteRelations($relation, $entities) |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Checks if entity or $var got changed |
||
| 762 | * |
||
| 763 | * @param string $var Check only this variable or all variables |
||
| 764 | * @return bool |
||
| 765 | * @throws InvalidConfiguration |
||
| 766 | */ |
||
| 767 | 18 | public function isDirty($var = null) |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Resets the entity or $var to original data |
||
| 782 | * |
||
| 783 | * @param string $var Reset only this variable or all variables |
||
| 784 | * @throws InvalidConfiguration |
||
| 785 | */ |
||
| 786 | 8 | public function reset($var = null) |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Save the entity to $entityManager |
||
| 803 | * |
||
| 804 | * @param EntityManager $entityManager |
||
| 805 | * @return Entity |
||
| 806 | * @throws Exceptions\NoConnection |
||
| 807 | * @throws Exceptions\NoEntity |
||
| 808 | * @throws Exceptions\NotScalar |
||
| 809 | * @throws Exceptions\UnsupportedDriver |
||
| 810 | * @throws IncompletePrimaryKey |
||
| 811 | * @throws InvalidConfiguration |
||
| 812 | * @throws InvalidName |
||
| 813 | * @throws NoEntityManager |
||
| 814 | */ |
||
| 815 | 13 | public function save(EntityManager $entityManager = null) |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Fetches related objects |
||
| 858 | * |
||
| 859 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 860 | * |
||
| 861 | * It will throw an error for non owner when the key is incomplete. |
||
| 862 | * |
||
| 863 | * @param string $relation The relation to fetch |
||
| 864 | * @param EntityManager $entityManager The EntityManager to use |
||
| 865 | * @return Entity|EntityFetcher|Entity[] |
||
| 866 | * @throws Exceptions\NoConnection |
||
| 867 | * @throws Exceptions\NoEntity |
||
| 868 | * @throws IncompletePrimaryKey |
||
| 869 | * @throws InvalidConfiguration |
||
| 870 | * @throws NoEntityManager |
||
| 871 | * @throws UndefinedRelation |
||
| 872 | */ |
||
| 873 | 16 | public function fetch($relation, EntityManager $entityManager = null, $getAll = false) |
|
| 965 | |||
| 966 | /** |
||
| 967 | * Get the foreign key for the given reference |
||
| 968 | * |
||
| 969 | * @param array $reference |
||
| 970 | * @return array |
||
| 971 | * @throws IncompletePrimaryKey |
||
| 972 | */ |
||
| 973 | 10 | private function getForeignKey($reference) |
|
| 989 | |||
| 990 | /** |
||
| 991 | * Get the primary key |
||
| 992 | * |
||
| 993 | * @return array |
||
| 994 | * @throws IncompletePrimaryKey |
||
| 995 | */ |
||
| 996 | 38 | public function getPrimaryKey() |
|
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Get current data |
||
| 1011 | * |
||
| 1012 | * @return array |
||
| 1013 | * @internal |
||
| 1014 | */ |
||
| 1015 | 17 | public function getData() |
|
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Set new original data |
||
| 1022 | * |
||
| 1023 | * @param array $data |
||
| 1024 | * @internal |
||
| 1025 | */ |
||
| 1026 | 18 | public function setOriginalData(array $data) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Empty event handler |
||
| 1033 | * |
||
| 1034 | * Get called when something is changed with magic setter. |
||
| 1035 | * |
||
| 1036 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
||
| 1037 | * @param mixed $oldValue The old value of the variable |
||
| 1038 | * @param mixed $value The new value of the variable |
||
| 1039 | */ |
||
| 1040 | 5 | public function onChange($var, $oldValue, $value) |
|
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Empty event handler |
||
| 1046 | * |
||
| 1047 | * Get called when the entity get initialized. |
||
| 1048 | * |
||
| 1049 | * @param bool $new Whether or not the entity is new or from database |
||
| 1050 | */ |
||
| 1051 | 98 | public function onInit($new) |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Empty event handler |
||
| 1057 | * |
||
| 1058 | * Get called before the entity get inserted in database. |
||
| 1059 | */ |
||
| 1060 | 3 | public function prePersist() |
|
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Empty event handler |
||
| 1066 | * |
||
| 1067 | * Get called after the entity got inserted in database. |
||
| 1068 | */ |
||
| 1069 | 5 | public function postPersist() |
|
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Empty event handler |
||
| 1075 | * |
||
| 1076 | * Get called before the entity get updated in database. |
||
| 1077 | */ |
||
| 1078 | 3 | public function preUpdate() |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Empty event handler |
||
| 1084 | * |
||
| 1085 | * Get called after the entity got updated in database. |
||
| 1086 | */ |
||
| 1087 | 3 | public function postUpdate() |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * String representation of data |
||
| 1093 | * |
||
| 1094 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 1095 | * @return string |
||
| 1096 | */ |
||
| 1097 | 1 | public function serialize() |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Constructs the object |
||
| 1104 | * |
||
| 1105 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 1106 | * @param string $serialized The string representation of data |
||
| 1107 | */ |
||
| 1108 | 2 | public function unserialize($serialized) |
|
| 1113 | } |
||
| 1114 |