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 |
||
| 31 | abstract class Entity implements \Serializable |
||
| 32 | { |
||
| 33 | /** @deprecated Use Relation::OPT_CLASS instead */ |
||
| 34 | const OPT_RELATION_CLASS = 'class'; |
||
| 35 | /** @deprecated Use Relation::OPT_CARDINALITY instead */ |
||
| 36 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 37 | /** @deprecated Use Relation::OPT_REFERENCE instead */ |
||
| 38 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 39 | /** @deprecated Use Relation::OPT_OPPONENT instead */ |
||
| 40 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 41 | /** @deprecated Use Relation::OPT_TABLE instead */ |
||
| 42 | const OPT_RELATION_TABLE = 'table'; |
||
| 43 | |||
| 44 | /** The template to use to calculate the table name. |
||
| 45 | * @var string */ |
||
| 46 | protected static $tableNameTemplate; |
||
| 47 | |||
| 48 | /** The naming scheme to use for table names. |
||
| 49 | * @var string */ |
||
| 50 | protected static $namingSchemeTable; |
||
| 51 | |||
| 52 | /** The naming scheme to use for column names. |
||
| 53 | * @var string */ |
||
| 54 | protected static $namingSchemeColumn; |
||
| 55 | |||
| 56 | /** The naming scheme to use for method names. |
||
| 57 | * @var string */ |
||
| 58 | protected static $namingSchemeMethods; |
||
| 59 | |||
| 60 | /** Fixed table name (ignore other settings) |
||
| 61 | * @var string */ |
||
| 62 | protected static $tableName; |
||
| 63 | |||
| 64 | /** The variable(s) used for primary key. |
||
| 65 | * @var string[]|string */ |
||
| 66 | protected static $primaryKey = ['id']; |
||
| 67 | |||
| 68 | /** Fixed column names (ignore other settings) |
||
| 69 | * @var string[] */ |
||
| 70 | protected static $columnAliases = []; |
||
| 71 | |||
| 72 | /** A prefix for column names. |
||
| 73 | * @var string */ |
||
| 74 | protected static $columnPrefix; |
||
| 75 | |||
| 76 | /** Whether or not the primary key is auto incremented. |
||
| 77 | * @var bool */ |
||
| 78 | protected static $autoIncrement = true; |
||
| 79 | |||
| 80 | /** Whether or not the validator for this class is enabled. |
||
| 81 | * @var bool */ |
||
| 82 | protected static $enableValidator = false; |
||
| 83 | |||
| 84 | /** Whether or not the validator for a class got enabled during runtime. |
||
| 85 | * @var bool[] */ |
||
| 86 | protected static $enabledValidators = []; |
||
| 87 | |||
| 88 | /** Relation definitions |
||
| 89 | * @var array */ |
||
| 90 | protected static $relations = []; |
||
| 91 | |||
| 92 | /** The reflections of the classes. |
||
| 93 | * @internal |
||
| 94 | * @var \ReflectionClass[] */ |
||
| 95 | protected static $reflections = []; |
||
| 96 | |||
| 97 | /** The current data of a row. |
||
| 98 | * @var mixed[] */ |
||
| 99 | protected $data = []; |
||
| 100 | |||
| 101 | /** The original data of the row. |
||
| 102 | * @var mixed[] */ |
||
| 103 | protected $originalData = []; |
||
| 104 | |||
| 105 | /** The entity manager from which this entity got created |
||
| 106 | * @var EM */ |
||
| 107 | protected $entityManager; |
||
| 108 | |||
| 109 | /** Related objects for getRelated |
||
| 110 | * @var array */ |
||
| 111 | protected $relatedObjects = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor |
||
| 115 | * |
||
| 116 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 117 | * |
||
| 118 | * @param mixed[] $data The current data |
||
| 119 | * @param EM $entityManager The EntityManager that created this entity |
||
| 120 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 121 | */ |
||
| 122 | 122 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Get a description for this table. |
||
| 134 | * |
||
| 135 | * @return Table|Column[] |
||
| 136 | * @codeCoverageIgnore This is just a proxy |
||
| 137 | */ |
||
| 138 | public static function describe() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the column name of $attribute |
||
| 145 | * |
||
| 146 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 147 | * to $namingSchemeColumn. |
||
| 148 | * |
||
| 149 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 150 | * the same as getColumnName($name). |
||
| 151 | * |
||
| 152 | * @param string $attribute |
||
| 153 | * @return string |
||
| 154 | * @throws InvalidConfiguration |
||
| 155 | */ |
||
| 156 | 166 | public static function getColumnName($attribute) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Get the primary key vars |
||
| 168 | * |
||
| 169 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 170 | * columns. |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 72 | public static function getPrimaryKeyVars() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get the definition for $relation |
||
| 181 | * |
||
| 182 | * It normalize the short definition form and create a Relation object from it. |
||
| 183 | * |
||
| 184 | * @param string $relation |
||
| 185 | * @return Relation |
||
| 186 | * @throws InvalidConfiguration |
||
| 187 | * @throws UndefinedRelation |
||
| 188 | */ |
||
| 189 | 83 | public static function getRelation($relation) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get the table name |
||
| 206 | * |
||
| 207 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 208 | * $tableName. |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | * @throws InvalidName|InvalidConfiguration |
||
| 212 | */ |
||
| 213 | 154 | public static function getTableName() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Check if the table has a auto increment column |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 19 | public static function isAutoIncremented() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Check if the validator is enabled |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | 39 | public static function isValidatorEnabled() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Enable validator |
||
| 246 | * |
||
| 247 | * @param bool $enable |
||
| 248 | */ |
||
| 249 | 8 | public static function enableValidator($enable = true) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Disable validator |
||
| 256 | * |
||
| 257 | * @param bool $disable |
||
| 258 | */ |
||
| 259 | 33 | public static function disableValidator($disable = true) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Validate $value for $attribute |
||
| 266 | * |
||
| 267 | * @param string $attribute |
||
| 268 | * @param mixed $value |
||
| 269 | * @return bool|Error |
||
| 270 | * @throws Exception |
||
| 271 | */ |
||
| 272 | 10 | public static function validate($attribute, $value) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Validate $data |
||
| 279 | * |
||
| 280 | * $data has to be an array of $attribute => $value |
||
| 281 | * |
||
| 282 | * @param array $data |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | 1 | public static function validateArray(array $data) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param EM $entityManager |
||
| 296 | * @return self |
||
| 297 | */ |
||
| 298 | 12 | public function setEntityManager(EM $entityManager) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Get the value from $attribute |
||
| 306 | * |
||
| 307 | * If there is a custom getter this method get called instead. |
||
| 308 | * |
||
| 309 | * @param string $attribute The variable to get |
||
| 310 | * @return mixed|null |
||
| 311 | * @throws IncompletePrimaryKey |
||
| 312 | * @throws InvalidConfiguration |
||
| 313 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 314 | */ |
||
| 315 | 112 | public function __get($attribute) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Check if a column is defined |
||
| 336 | * |
||
| 337 | * @param $attribute |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 3 | public function __isset($attribute) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Set $attribute to $value |
||
| 361 | * |
||
| 362 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 363 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 364 | * written and give wrong results for dirty checking. |
||
| 365 | * |
||
| 366 | * The onChange event is called after something got changed. |
||
| 367 | * |
||
| 368 | * The method throws an error when the validation fails (also when the column does not exist). |
||
| 369 | * |
||
| 370 | * @param string $attribute The variable to change |
||
| 371 | * @param mixed $value The value to store |
||
| 372 | * @throws Error |
||
| 373 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 374 | */ |
||
| 375 | 38 | public function __set($attribute, $value) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Fill the entity with $data |
||
| 406 | * |
||
| 407 | * When $checkMissing is set to true it also proves that the absent columns are nullable. |
||
| 408 | * |
||
| 409 | * @param array $data |
||
| 410 | * @param bool $ignoreUnknown |
||
| 411 | * @param bool $checkMissing |
||
| 412 | * @throws Error |
||
| 413 | * @throws UnknownColumn |
||
| 414 | */ |
||
| 415 | 8 | public function fill(array $data, $ignoreUnknown = false, $checkMissing = false) |
|
| 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 Exception\NoConnection |
||
| 442 | * @throws Exception\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 $attribute to original data |
||
| 524 | * |
||
| 525 | * @param string $attribute Reset only this variable or all variables |
||
| 526 | * @throws InvalidConfiguration |
||
| 527 | */ |
||
| 528 | 22 | public function reset($attribute = null) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Save the entity to EntityManager |
||
| 545 | * |
||
| 546 | * @return Entity |
||
| 547 | * @throws Exception\NoConnection |
||
| 548 | * @throws Exception\NoEntity |
||
| 549 | * @throws Exception\NotScalar |
||
| 550 | * @throws Exception\UnsupportedDriver |
||
| 551 | * @throws IncompletePrimaryKey |
||
| 552 | * @throws InvalidConfiguration |
||
| 553 | * @throws InvalidName |
||
| 554 | * @throws NoEntityManager |
||
| 555 | */ |
||
| 556 | 16 | public function save() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Generates a primary key |
||
| 600 | * |
||
| 601 | * This method should only be executed from save method. |
||
| 602 | */ |
||
| 603 | protected function generatePrimaryKey() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Checks if entity or $attribute got changed |
||
| 610 | * |
||
| 611 | * @param string $attribute Check only this variable or all variables |
||
| 612 | * @return bool |
||
| 613 | * @throws InvalidConfiguration |
||
| 614 | */ |
||
| 615 | 20 | public function isDirty($attribute = null) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Check if the current data is valid |
||
| 631 | * |
||
| 632 | * Returns boolean true when valid otherwise an array of Errors. |
||
| 633 | * |
||
| 634 | * @return bool|Error[] |
||
| 635 | */ |
||
| 636 | 1 | public function isValid() |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Empty event handler |
||
| 661 | * |
||
| 662 | * Get called when something is changed with magic setter. |
||
| 663 | * |
||
| 664 | * @param string $attribute The variable that got changed.merge(node.inheritedProperties) |
||
| 665 | * @param mixed $oldValue The old value of the variable |
||
| 666 | * @param mixed $value The new value of the variable |
||
| 667 | * @codeCoverageIgnore dummy event handler |
||
| 668 | */ |
||
| 669 | public function onChange($attribute, $oldValue, $value) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Empty event handler |
||
| 675 | * |
||
| 676 | * Get called when the entity get initialized. |
||
| 677 | * |
||
| 678 | * @param bool $new Whether or not the entity is new or from database |
||
| 679 | * @codeCoverageIgnore dummy event handler |
||
| 680 | */ |
||
| 681 | public function onInit($new) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Empty event handler |
||
| 687 | * |
||
| 688 | * Get called before the entity get updated in database. |
||
| 689 | * |
||
| 690 | * @codeCoverageIgnore dummy event handler |
||
| 691 | */ |
||
| 692 | public function preUpdate() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Empty event handler |
||
| 698 | * |
||
| 699 | * Get called before the entity get inserted in database. |
||
| 700 | * |
||
| 701 | * @codeCoverageIgnore dummy event handler |
||
| 702 | */ |
||
| 703 | public function prePersist() |
||
| 706 | |||
| 707 | |||
| 708 | /** |
||
| 709 | * Empty event handler |
||
| 710 | * |
||
| 711 | * Get called after the entity got inserted in database. |
||
| 712 | * |
||
| 713 | * @codeCoverageIgnore dummy event handler |
||
| 714 | */ |
||
| 715 | public function postPersist() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Empty event handler |
||
| 721 | * |
||
| 722 | * Get called after the entity got updated in database. |
||
| 723 | * |
||
| 724 | * @codeCoverageIgnore dummy event handler |
||
| 725 | */ |
||
| 726 | public function postUpdate() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Fetches related objects |
||
| 732 | * |
||
| 733 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 734 | * |
||
| 735 | * It will throw an error for non owner when the key is incomplete. |
||
| 736 | * |
||
| 737 | * @param string $relation The relation to fetch |
||
| 738 | * @param bool $getAll |
||
| 739 | * @return Entity|Entity[]|EntityFetcher |
||
| 740 | * @throws NoEntityManager |
||
| 741 | */ |
||
| 742 | 20 | public function fetch($relation, $getAll = false) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Get the primary key |
||
| 765 | * |
||
| 766 | * @return array |
||
| 767 | * @throws IncompletePrimaryKey |
||
| 768 | */ |
||
| 769 | 53 | public function getPrimaryKey() |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Get current data |
||
| 784 | * |
||
| 785 | * @return array |
||
| 786 | * @internal |
||
| 787 | */ |
||
| 788 | 31 | public function getData() |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Set new original data |
||
| 795 | * |
||
| 796 | * @param array $data |
||
| 797 | * @internal |
||
| 798 | */ |
||
| 799 | 38 | public function setOriginalData(array $data) |
|
| 803 | |||
| 804 | /** |
||
| 805 | * String representation of data |
||
| 806 | * |
||
| 807 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 808 | * @return string |
||
| 809 | */ |
||
| 810 | 2 | public function serialize() |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Constructs the object |
||
| 817 | * |
||
| 818 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 819 | * @param string $serialized The string representation of data |
||
| 820 | */ |
||
| 821 | 3 | public function unserialize($serialized) |
|
| 827 | |||
| 828 | // DEPRECATED stuff |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @return string |
||
| 832 | * @deprecated use getOption from EntityManager |
||
| 833 | * @codeCoverageIgnore deprecated |
||
| 834 | */ |
||
| 835 | public static function getTableNameTemplate() |
||
| 839 | |||
| 840 | /** |
||
| 841 | * @param string $tableNameTemplate |
||
| 842 | * @deprecated use setOption from EntityManager |
||
| 843 | * @codeCoverageIgnore deprecated |
||
| 844 | */ |
||
| 845 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @return string |
||
| 852 | * @deprecated use getOption from EntityManager |
||
| 853 | * @codeCoverageIgnore deprecated |
||
| 854 | */ |
||
| 855 | public static function getNamingSchemeTable() |
||
| 859 | |||
| 860 | /** |
||
| 861 | * @param string $namingSchemeTable |
||
| 862 | * @deprecated use setOption from EntityManager |
||
| 863 | * @codeCoverageIgnore deprecated |
||
| 864 | */ |
||
| 865 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @return string |
||
| 872 | * @deprecated use getOption from EntityManager |
||
| 873 | * @codeCoverageIgnore deprecated |
||
| 874 | */ |
||
| 875 | public static function getNamingSchemeColumn() |
||
| 879 | |||
| 880 | /** |
||
| 881 | * @param string $namingSchemeColumn |
||
| 882 | * @deprecated use setOption from EntityManager |
||
| 883 | * @codeCoverageIgnore deprecated |
||
| 884 | */ |
||
| 885 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * @return string |
||
| 892 | * @deprecated use getOption from EntityManager |
||
| 893 | * @codeCoverageIgnore deprecated |
||
| 894 | */ |
||
| 895 | public static function getNamingSchemeMethods() |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @param string $namingSchemeMethods |
||
| 902 | * @deprecated use setOption from EntityManager |
||
| 903 | * @codeCoverageIgnore deprecated |
||
| 904 | */ |
||
| 905 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 909 | } |
||
| 910 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.