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 |
||
| 28 | abstract class Entity implements \Serializable |
||
| 29 | { |
||
| 30 | const OPT_RELATION_CLASS = 'class'; |
||
| 31 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 32 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 33 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 34 | const OPT_RELATION_TABLE = 'table'; |
||
| 35 | |||
| 36 | /** The template to use to calculate the table name. |
||
| 37 | * @var string */ |
||
| 38 | protected static $tableNameTemplate; |
||
| 39 | |||
| 40 | /** The naming scheme to use for table names. |
||
| 41 | * @var string */ |
||
| 42 | protected static $namingSchemeTable; |
||
| 43 | |||
| 44 | /** The naming scheme to use for column names. |
||
| 45 | * @var string */ |
||
| 46 | protected static $namingSchemeColumn; |
||
| 47 | |||
| 48 | /** The naming scheme to use for method names. |
||
| 49 | * @var string */ |
||
| 50 | protected static $namingSchemeMethods; |
||
| 51 | |||
| 52 | /** Whether or not the naming got used |
||
| 53 | * @var bool */ |
||
| 54 | protected static $namingUsed = false; |
||
| 55 | |||
| 56 | /** Fixed table name (ignore other settings) |
||
| 57 | * @var string */ |
||
| 58 | protected static $tableName; |
||
| 59 | |||
| 60 | /** The variable(s) used for primary key. |
||
| 61 | * @var string[]|string */ |
||
| 62 | protected static $primaryKey = ['id']; |
||
| 63 | |||
| 64 | /** Fixed column names (ignore other settings) |
||
| 65 | * @var string[] */ |
||
| 66 | protected static $columnAliases = []; |
||
| 67 | |||
| 68 | /** A prefix for column names. |
||
| 69 | * @var string */ |
||
| 70 | protected static $columnPrefix; |
||
| 71 | |||
| 72 | /** Whether or not the primary key is auto incremented. |
||
| 73 | * @var bool */ |
||
| 74 | protected static $autoIncrement = true; |
||
| 75 | |||
| 76 | /** Relation definitions |
||
| 77 | * @var array */ |
||
| 78 | protected static $relations = []; |
||
| 79 | |||
| 80 | /** The current data of a row. |
||
| 81 | * @var mixed[] */ |
||
| 82 | protected $data = []; |
||
| 83 | |||
| 84 | /** The original data of the row. |
||
| 85 | * @var mixed[] */ |
||
| 86 | protected $originalData = []; |
||
| 87 | |||
| 88 | /** The entity manager from which this entity got created |
||
| 89 | * @var EntityManager*/ |
||
| 90 | protected $entityManager; |
||
| 91 | |||
| 92 | /** Related objects for getRelated |
||
| 93 | * @var array */ |
||
| 94 | protected $relatedObjects = []; |
||
| 95 | |||
| 96 | /** Calculated table names. |
||
| 97 | * @internal |
||
| 98 | * @var string[] */ |
||
| 99 | protected static $calculatedTableNames = []; |
||
| 100 | |||
| 101 | /** Calculated column names. |
||
| 102 | * @internal |
||
| 103 | * @var string[][] */ |
||
| 104 | protected static $calculatedColumnNames = []; |
||
| 105 | |||
| 106 | /** The reflections of the classes. |
||
| 107 | * @internal |
||
| 108 | * @var \ReflectionClass[] */ |
||
| 109 | protected static $reflections = []; |
||
| 110 | |||
| 111 | /** Fetched table descriptions |
||
| 112 | * @var Table[] */ |
||
| 113 | protected static $tableDescriptions = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the table name |
||
| 117 | * |
||
| 118 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 119 | * $tableName. |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | * @throws InvalidName|InvalidConfiguration |
||
| 123 | */ |
||
| 124 | 136 | public static function getTableName() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get the column name of $field |
||
| 151 | * |
||
| 152 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 153 | * to $namingSchemeColumn. |
||
| 154 | * |
||
| 155 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 156 | * the same as getColumnName($name). |
||
| 157 | * |
||
| 158 | * @param string $field |
||
| 159 | * @return string |
||
| 160 | * @throws InvalidConfiguration |
||
| 161 | */ |
||
| 162 | 138 | public static function getColumnName($field) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get the definition for $relation |
||
| 188 | * |
||
| 189 | * It normalize the short definition form and create a Relation object from it. |
||
| 190 | * |
||
| 191 | * @param string $relation |
||
| 192 | * @return Relation |
||
| 193 | * @throws InvalidConfiguration |
||
| 194 | * @throws UndefinedRelation |
||
| 195 | */ |
||
| 196 | 84 | public static function getRelation($relation) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Initialize the validator for this Entity. |
||
| 213 | * |
||
| 214 | * @param EntityManager $entityManager |
||
| 215 | */ |
||
| 216 | 2 | public static function initValidator(EntityManager $entityManager) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Check if the validator is initialized. |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 2 | public static function validatorIsInitialized() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Validate $value for $field. |
||
| 237 | * |
||
| 238 | * @param string $field |
||
| 239 | * @param mixed $value |
||
| 240 | * @return bool|Error |
||
| 241 | * @throws Exception |
||
| 242 | */ |
||
| 243 | 5 | public static function validate($field, $value) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Validate $fields. |
||
| 256 | * |
||
| 257 | * $fields has to be an array of $field => $value |
||
| 258 | * |
||
| 259 | * @param array $fields |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | 1 | public static function validateArray(array $fields) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Get the primary key vars |
||
| 273 | * |
||
| 274 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 275 | * columns. |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 56 | public static function getPrimaryKeyVars() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Check if the table has a auto increment column. |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | 14 | public static function isAutoIncremented() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Get an array of Columns for this table. |
||
| 296 | * |
||
| 297 | * @param EntityManager $entityManager |
||
| 298 | * @return Column[] |
||
| 299 | * @codeCoverageIgnore This is just a proxy |
||
| 300 | */ |
||
| 301 | public static function describe(EntityManager $entityManager) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get reflection of the entity |
||
| 308 | * |
||
| 309 | * @return \ReflectionClass |
||
| 310 | */ |
||
| 311 | 125 | protected static function getReflection() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Constructor |
||
| 321 | * |
||
| 322 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 323 | * |
||
| 324 | * @param mixed[] $data The current data |
||
| 325 | * @param EntityManager $entityManager The EntityManager that created this entity |
||
| 326 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 327 | */ |
||
| 328 | 109 | final public function __construct(array $data = [], EntityManager $entityManager = null, $fromDatabase = false) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param EntityManager $entityManager |
||
| 340 | * @return self |
||
| 341 | */ |
||
| 342 | 1 | public function setEntityManager(EntityManager $entityManager) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Set $var to $value |
||
| 350 | * |
||
| 351 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 352 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 353 | * written and give wrong results for dirty checking. |
||
| 354 | * |
||
| 355 | * The onChange event is called after something got changed. |
||
| 356 | * |
||
| 357 | * @param string $var The variable to change |
||
| 358 | * @param mixed $value The value to store |
||
| 359 | * @throws IncompletePrimaryKey |
||
| 360 | * @throws InvalidConfiguration |
||
| 361 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 362 | */ |
||
| 363 | 18 | public function __set($var, $value) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get the value from $var |
||
| 387 | * |
||
| 388 | * If there is a custom getter this method get called instead. |
||
| 389 | * |
||
| 390 | * @param string $var The variable to get |
||
| 391 | * @return mixed|null |
||
| 392 | * @throws IncompletePrimaryKey |
||
| 393 | * @throws InvalidConfiguration |
||
| 394 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 395 | */ |
||
| 396 | 86 | public function __get($var) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Get related objects |
||
| 416 | * |
||
| 417 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 418 | * $refresh to true. |
||
| 419 | * |
||
| 420 | * @param string $relation |
||
| 421 | * @param bool $refresh |
||
| 422 | * @return mixed |
||
| 423 | * @throws Exceptions\NoConnection |
||
| 424 | * @throws Exceptions\NoEntity |
||
| 425 | * @throws IncompletePrimaryKey |
||
| 426 | * @throws InvalidConfiguration |
||
| 427 | * @throws NoEntityManager |
||
| 428 | * @throws UndefinedRelation |
||
| 429 | */ |
||
| 430 | 11 | public function getRelated($relation, $refresh = false) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Set $relation to $entity |
||
| 441 | * |
||
| 442 | * This method is only for the owner of a relation. |
||
| 443 | * |
||
| 444 | * @param string $relation |
||
| 445 | * @param Entity $entity |
||
| 446 | * @throws IncompletePrimaryKey |
||
| 447 | * @throws InvalidRelation |
||
| 448 | */ |
||
| 449 | 7 | public function setRelated($relation, Entity $entity = null) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Add relations for $relation to $entities |
||
| 458 | * |
||
| 459 | * This method is only for many-to-many relations. |
||
| 460 | * |
||
| 461 | * This method does not take care about already existing relations and will fail hard. |
||
| 462 | * |
||
| 463 | * @param string $relation |
||
| 464 | * @param Entity[] $entities |
||
| 465 | * @param EntityManager $entityManager |
||
| 466 | * @throws NoEntityManager |
||
| 467 | */ |
||
| 468 | 9 | public function addRelated($relation, array $entities, EntityManager $entityManager = null) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Delete relations for $relation to $entities |
||
| 481 | * |
||
| 482 | * This method is only for many-to-many relations. |
||
| 483 | * |
||
| 484 | * @param string $relation |
||
| 485 | * @param Entity[] $entities |
||
| 486 | * @param EntityManager $entityManager |
||
| 487 | * @throws NoEntityManager |
||
| 488 | */ |
||
| 489 | 9 | public function deleteRelated($relation, $entities, EntityManager $entityManager = null) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Checks if entity or $var got changed |
||
| 502 | * |
||
| 503 | * @param string $var Check only this variable or all variables |
||
| 504 | * @return bool |
||
| 505 | * @throws InvalidConfiguration |
||
| 506 | */ |
||
| 507 | 18 | public function isDirty($var = null) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Resets the entity or $var to original data |
||
| 523 | * |
||
| 524 | * @param string $var Reset only this variable or all variables |
||
| 525 | * @throws InvalidConfiguration |
||
| 526 | */ |
||
| 527 | 8 | public function reset($var = null) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Save the entity to $entityManager |
||
| 544 | * |
||
| 545 | * @param EntityManager $entityManager |
||
| 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 | 13 | public function save(EntityManager $entityManager = null) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Fetches related objects |
||
| 599 | * |
||
| 600 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 601 | * |
||
| 602 | * It will throw an error for non owner when the key is incomplete. |
||
| 603 | * |
||
| 604 | * @param string $relation The relation to fetch |
||
| 605 | * @param EntityManager $entityManager The EntityManager to use |
||
| 606 | * @param bool $getAll |
||
| 607 | * @return Entity|Entity[]|EntityFetcher |
||
| 608 | * @throws NoEntityManager |
||
| 609 | */ |
||
| 610 | 19 | public function fetch($relation, EntityManager $entityManager = null, $getAll = false) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Get the primary key |
||
| 629 | * |
||
| 630 | * @return array |
||
| 631 | * @throws IncompletePrimaryKey |
||
| 632 | */ |
||
| 633 | 38 | public function getPrimaryKey() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get current data |
||
| 648 | * |
||
| 649 | * @return array |
||
| 650 | * @internal |
||
| 651 | */ |
||
| 652 | 20 | public function getData() |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Set new original data |
||
| 659 | * |
||
| 660 | * @param array $data |
||
| 661 | * @internal |
||
| 662 | */ |
||
| 663 | 18 | public function setOriginalData(array $data) |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Empty event handler |
||
| 670 | * |
||
| 671 | * Get called when something is changed with magic setter. |
||
| 672 | * |
||
| 673 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
||
| 674 | * @param mixed $oldValue The old value of the variable |
||
| 675 | * @param mixed $value The new value of the variable |
||
| 676 | */ |
||
| 677 | 6 | public function onChange($var, $oldValue, $value) |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Empty event handler |
||
| 683 | * |
||
| 684 | * Get called when the entity get initialized. |
||
| 685 | * |
||
| 686 | * @param bool $new Whether or not the entity is new or from database |
||
| 687 | */ |
||
| 688 | 108 | public function onInit($new) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Empty event handler |
||
| 694 | * |
||
| 695 | * Get called before the entity get inserted in database. |
||
| 696 | */ |
||
| 697 | 3 | public function prePersist() |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Empty event handler |
||
| 703 | * |
||
| 704 | * Get called after the entity got inserted in database. |
||
| 705 | */ |
||
| 706 | 5 | public function postPersist() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Empty event handler |
||
| 712 | * |
||
| 713 | * Get called before the entity get updated in database. |
||
| 714 | */ |
||
| 715 | 3 | public function preUpdate() |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Empty event handler |
||
| 721 | * |
||
| 722 | * Get called after the entity got updated in database. |
||
| 723 | */ |
||
| 724 | 3 | public function postUpdate() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * String representation of data |
||
| 730 | * |
||
| 731 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | 2 | public function serialize() |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Constructs the object |
||
| 741 | * |
||
| 742 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 743 | * @param string $serialized The string representation of data |
||
| 744 | */ |
||
| 745 | 3 | public function unserialize($serialized) |
|
| 750 | |||
| 751 | |||
| 752 | // DEPRECATED stuff |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @return string |
||
| 756 | * @deprecated use getOption from EntityManager |
||
| 757 | * @codeCoverageIgnore deprecated |
||
| 758 | */ |
||
| 759 | public static function getTableNameTemplate() |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param string $tableNameTemplate |
||
| 766 | * @deprecated use setOption from EntityManager |
||
| 767 | * @codeCoverageIgnore deprecated |
||
| 768 | */ |
||
| 769 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * @return string |
||
| 776 | * @deprecated use getOption from EntityManager |
||
| 777 | * @codeCoverageIgnore deprecated |
||
| 778 | */ |
||
| 779 | public static function getNamingSchemeTable() |
||
| 783 | |||
| 784 | /** |
||
| 785 | * @param string $namingSchemeTable |
||
| 786 | * @deprecated use setOption from EntityManager |
||
| 787 | * @codeCoverageIgnore deprecated |
||
| 788 | */ |
||
| 789 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @return string |
||
| 796 | * @deprecated use getOption from EntityManager |
||
| 797 | * @codeCoverageIgnore deprecated |
||
| 798 | */ |
||
| 799 | public static function getNamingSchemeColumn() |
||
| 803 | |||
| 804 | /** |
||
| 805 | * @param string $namingSchemeColumn |
||
| 806 | * @deprecated use setOption from EntityManager |
||
| 807 | * @codeCoverageIgnore deprecated |
||
| 808 | */ |
||
| 809 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @return string |
||
| 816 | * @deprecated use getOption from EntityManager |
||
| 817 | * @codeCoverageIgnore deprecated |
||
| 818 | */ |
||
| 819 | public static function getNamingSchemeMethods() |
||
| 823 | |||
| 824 | /** |
||
| 825 | * @param string $namingSchemeMethods |
||
| 826 | * @deprecated use setOption from EntityManager |
||
| 827 | * @codeCoverageIgnore deprecated |
||
| 828 | */ |
||
| 829 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 833 | } |
||
| 834 |