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 | 113 | 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 | 145 | 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 | 56 | 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 | 85 | 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 | 146 | public static function getTableName() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Check if the table has a auto increment column |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 14 | public static function isAutoIncremented() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Check if the validator is enabled |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | 26 | public static function isValidatorEnabled() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Enable validator |
||
| 246 | * |
||
| 247 | * @param bool $enable |
||
| 248 | */ |
||
| 249 | 7 | public static function enableValidator($enable = true) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Disable validator |
||
| 256 | * |
||
| 257 | * @param bool $disable |
||
| 258 | */ |
||
| 259 | 1 | 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 | 9 | 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 | 2 | 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 | 90 | public function __get($attribute) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set $attribute to $value |
||
| 336 | * |
||
| 337 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 338 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 339 | * written and give wrong results for dirty checking. |
||
| 340 | * |
||
| 341 | * The onChange event is called after something got changed. |
||
| 342 | * |
||
| 343 | * @param string $attribute The variable to change |
||
| 344 | * @param mixed $value The value to store |
||
| 345 | * @throws NotValid |
||
| 346 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 347 | */ |
||
| 348 | 25 | public function __set($attribute, $value) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Fill the entity with $data |
||
| 380 | * |
||
| 381 | * @param array $data |
||
| 382 | * @param bool $ignoreUnknown |
||
| 383 | * @throws UnknownColumn |
||
| 384 | */ |
||
| 385 | 3 | public function fill(array $data, $ignoreUnknown = false) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get related objects |
||
| 400 | * |
||
| 401 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 402 | * $refresh to true. |
||
| 403 | * |
||
| 404 | * @param string $relation |
||
| 405 | * @param bool $refresh |
||
| 406 | * @return mixed |
||
| 407 | * @throws Exception\NoConnection |
||
| 408 | * @throws Exception\NoEntity |
||
| 409 | * @throws IncompletePrimaryKey |
||
| 410 | * @throws InvalidConfiguration |
||
| 411 | * @throws NoEntityManager |
||
| 412 | * @throws UndefinedRelation |
||
| 413 | */ |
||
| 414 | 11 | public function getRelated($relation, $refresh = false) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Set $relation to $entity |
||
| 425 | * |
||
| 426 | * This method is only for the owner of a relation. |
||
| 427 | * |
||
| 428 | * @param string $relation |
||
| 429 | * @param Entity $entity |
||
| 430 | * @throws IncompletePrimaryKey |
||
| 431 | * @throws InvalidRelation |
||
| 432 | */ |
||
| 433 | 7 | public function setRelated($relation, Entity $entity = null) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Add relations for $relation to $entities |
||
| 442 | * |
||
| 443 | * This method is only for many-to-many relations. |
||
| 444 | * |
||
| 445 | * This method does not take care about already existing relations and will fail hard. |
||
| 446 | * |
||
| 447 | * @param string $relation |
||
| 448 | * @param Entity[] $entities |
||
| 449 | * @throws NoEntityManager |
||
| 450 | */ |
||
| 451 | 8 | public function addRelated($relation, array $entities) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Delete relations for $relation to $entities |
||
| 467 | * |
||
| 468 | * This method is only for many-to-many relations. |
||
| 469 | * |
||
| 470 | * @param string $relation |
||
| 471 | * @param Entity[] $entities |
||
| 472 | * @throws NoEntityManager |
||
| 473 | */ |
||
| 474 | 8 | public function deleteRelated($relation, $entities) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Resets the entity or $attribute to original data |
||
| 490 | * |
||
| 491 | * @param string $attribute Reset only this variable or all variables |
||
| 492 | * @throws InvalidConfiguration |
||
| 493 | */ |
||
| 494 | 8 | public function reset($attribute = null) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Save the entity to EntityManager |
||
| 511 | * |
||
| 512 | * @return Entity |
||
| 513 | * @throws Exception\NoConnection |
||
| 514 | * @throws Exception\NoEntity |
||
| 515 | * @throws Exception\NotScalar |
||
| 516 | * @throws Exception\UnsupportedDriver |
||
| 517 | * @throws IncompletePrimaryKey |
||
| 518 | * @throws InvalidConfiguration |
||
| 519 | * @throws InvalidName |
||
| 520 | * @throws NoEntityManager |
||
| 521 | */ |
||
| 522 | 12 | public function save() |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Checks if entity or $attribute got changed |
||
| 568 | * |
||
| 569 | * @param string $attribute Check only this variable or all variables |
||
| 570 | * @return bool |
||
| 571 | * @throws InvalidConfiguration |
||
| 572 | */ |
||
| 573 | 18 | public function isDirty($attribute = null) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Empty event handler |
||
| 589 | * |
||
| 590 | * Get called when something is changed with magic setter. |
||
| 591 | * |
||
| 592 | * @param string $attribute The variable that got changed.merge(node.inheritedProperties) |
||
| 593 | * @param mixed $oldValue The old value of the variable |
||
| 594 | * @param mixed $value The new value of the variable |
||
| 595 | */ |
||
| 596 | 6 | public function onChange($attribute, $oldValue, $value) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Empty event handler |
||
| 602 | * |
||
| 603 | * Get called when the entity get initialized. |
||
| 604 | * |
||
| 605 | * @param bool $new Whether or not the entity is new or from database |
||
| 606 | */ |
||
| 607 | 112 | public function onInit($new) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Empty event handler |
||
| 613 | * |
||
| 614 | * Get called before the entity get updated in database. |
||
| 615 | */ |
||
| 616 | 3 | public function preUpdate() |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Empty event handler |
||
| 622 | * |
||
| 623 | * Get called before the entity get inserted in database. |
||
| 624 | */ |
||
| 625 | 3 | public function prePersist() |
|
| 628 | |||
| 629 | |||
| 630 | // DEPRECATED stuff |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Empty event handler |
||
| 634 | * |
||
| 635 | * Get called after the entity got inserted in database. |
||
| 636 | */ |
||
| 637 | 5 | public function postPersist() |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Empty event handler |
||
| 643 | * |
||
| 644 | * Get called after the entity got updated in database. |
||
| 645 | */ |
||
| 646 | 3 | public function postUpdate() |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Fetches related objects |
||
| 652 | * |
||
| 653 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 654 | * |
||
| 655 | * It will throw an error for non owner when the key is incomplete. |
||
| 656 | * |
||
| 657 | * @param string $relation The relation to fetch |
||
| 658 | * @param bool $getAll |
||
| 659 | * @return Entity|Entity[]|EntityFetcher |
||
| 660 | * @throws NoEntityManager |
||
| 661 | */ |
||
| 662 | 19 | public function fetch($relation, $getAll = false) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Get the primary key |
||
| 685 | * |
||
| 686 | * @return array |
||
| 687 | * @throws IncompletePrimaryKey |
||
| 688 | */ |
||
| 689 | 38 | public function getPrimaryKey() |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Get current data |
||
| 704 | * |
||
| 705 | * @return array |
||
| 706 | * @internal |
||
| 707 | */ |
||
| 708 | 20 | public function getData() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Set new original data |
||
| 715 | * |
||
| 716 | * @param array $data |
||
| 717 | * @internal |
||
| 718 | */ |
||
| 719 | 18 | public function setOriginalData(array $data) |
|
| 723 | |||
| 724 | /** |
||
| 725 | * String representation of data |
||
| 726 | * |
||
| 727 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | 2 | public function serialize() |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Constructs the object |
||
| 737 | * |
||
| 738 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 739 | * @param string $serialized The string representation of data |
||
| 740 | */ |
||
| 741 | 3 | public function unserialize($serialized) |
|
| 747 | |||
| 748 | /** |
||
| 749 | * @return string |
||
| 750 | * @deprecated use getOption from EntityManager |
||
| 751 | * @codeCoverageIgnore deprecated |
||
| 752 | */ |
||
| 753 | public static function getTableNameTemplate() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @param string $tableNameTemplate |
||
| 760 | * @deprecated use setOption from EntityManager |
||
| 761 | * @codeCoverageIgnore deprecated |
||
| 762 | */ |
||
| 763 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @return string |
||
| 770 | * @deprecated use getOption from EntityManager |
||
| 771 | * @codeCoverageIgnore deprecated |
||
| 772 | */ |
||
| 773 | public static function getNamingSchemeTable() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @param string $namingSchemeTable |
||
| 780 | * @deprecated use setOption from EntityManager |
||
| 781 | * @codeCoverageIgnore deprecated |
||
| 782 | */ |
||
| 783 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @return string |
||
| 790 | * @deprecated use getOption from EntityManager |
||
| 791 | * @codeCoverageIgnore deprecated |
||
| 792 | */ |
||
| 793 | public static function getNamingSchemeColumn() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @param string $namingSchemeColumn |
||
| 800 | * @deprecated use setOption from EntityManager |
||
| 801 | * @codeCoverageIgnore deprecated |
||
| 802 | */ |
||
| 803 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @return string |
||
| 810 | * @deprecated use getOption from EntityManager |
||
| 811 | * @codeCoverageIgnore deprecated |
||
| 812 | */ |
||
| 813 | public static function getNamingSchemeMethods() |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @param string $namingSchemeMethods |
||
| 820 | * @deprecated use setOption from EntityManager |
||
| 821 | * @codeCoverageIgnore deprecated |
||
| 822 | */ |
||
| 823 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 827 | } |
||
| 828 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.