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 |
||
| 29 | abstract class Entity implements \Serializable |
||
| 30 | { |
||
| 31 | const OPT_RELATION_CLASS = 'class'; |
||
| 32 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 33 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 34 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 35 | const OPT_RELATION_TABLE = 'table'; |
||
| 36 | |||
| 37 | /** The template to use to calculate the table name. |
||
| 38 | * @var string */ |
||
| 39 | protected static $tableNameTemplate; |
||
| 40 | |||
| 41 | /** The naming scheme to use for table names. |
||
| 42 | * @var string */ |
||
| 43 | protected static $namingSchemeTable; |
||
| 44 | |||
| 45 | /** The naming scheme to use for column names. |
||
| 46 | * @var string */ |
||
| 47 | protected static $namingSchemeColumn; |
||
| 48 | |||
| 49 | /** The naming scheme to use for method names. |
||
| 50 | * @var string */ |
||
| 51 | protected static $namingSchemeMethods; |
||
| 52 | |||
| 53 | /** Fixed table name (ignore other settings) |
||
| 54 | * @var string */ |
||
| 55 | protected static $tableName; |
||
| 56 | |||
| 57 | /** The variable(s) used for primary key. |
||
| 58 | * @var string[]|string */ |
||
| 59 | protected static $primaryKey = ['id']; |
||
| 60 | |||
| 61 | /** Fixed column names (ignore other settings) |
||
| 62 | * @var string[] */ |
||
| 63 | protected static $columnAliases = []; |
||
| 64 | |||
| 65 | /** A prefix for column names. |
||
| 66 | * @var string */ |
||
| 67 | protected static $columnPrefix; |
||
| 68 | |||
| 69 | /** Whether or not the primary key is auto incremented. |
||
| 70 | * @var bool */ |
||
| 71 | protected static $autoIncrement = true; |
||
| 72 | |||
| 73 | /** Relation definitions |
||
| 74 | * @var array */ |
||
| 75 | protected static $relations = []; |
||
| 76 | |||
| 77 | /** The reflections of the classes. |
||
| 78 | * @internal |
||
| 79 | * @var \ReflectionClass[] */ |
||
| 80 | protected static $reflections = []; |
||
| 81 | |||
| 82 | /** Fetched table descriptions |
||
| 83 | * @var Table[] */ |
||
| 84 | protected static $tableDescriptions = []; |
||
| 85 | |||
| 86 | /** The current data of a row. |
||
| 87 | * @var mixed[] */ |
||
| 88 | protected $data = []; |
||
| 89 | |||
| 90 | /** The original data of the row. |
||
| 91 | * @var mixed[] */ |
||
| 92 | protected $originalData = []; |
||
| 93 | |||
| 94 | /** The entity manager from which this entity got created |
||
| 95 | * @var EM */ |
||
| 96 | protected $entityManager; |
||
| 97 | |||
| 98 | /** Related objects for getRelated |
||
| 99 | * @var array */ |
||
| 100 | protected $relatedObjects = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Constructor |
||
| 104 | * |
||
| 105 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 106 | * |
||
| 107 | * @param mixed[] $data The current data |
||
| 108 | * @param EM $entityManager The EntityManager that created this entity |
||
| 109 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 110 | */ |
||
| 111 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
||
| 120 | |||
| 121 | 106 | /** |
|
| 122 | * Get a description for this table. |
||
| 123 | 106 | * |
|
| 124 | 14 | * @return Table|Column[] |
|
| 125 | * @codeCoverageIgnore This is just a proxy |
||
| 126 | 106 | */ |
|
| 127 | 106 | public static function describe() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get the column name of $field |
||
| 140 | * |
||
| 141 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 142 | * to $namingSchemeColumn. |
||
| 143 | * |
||
| 144 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 145 | * the same as getColumnName($name). |
||
| 146 | * |
||
| 147 | * @param string $field |
||
| 148 | * @return string |
||
| 149 | * @throws InvalidConfiguration |
||
| 150 | */ |
||
| 151 | public static function getColumnName($field) |
||
| 160 | |||
| 161 | 139 | /** |
|
| 162 | * Get the primary key vars |
||
| 163 | 139 | * |
|
| 164 | 6 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
|
| 165 | * columns. |
||
| 166 | * |
||
| 167 | 137 | * @return array |
|
| 168 | 137 | */ |
|
| 169 | public static function getPrimaryKeyVars() |
||
| 173 | 137 | ||
| 174 | 22 | /** |
|
| 175 | * Get the definition for $relation |
||
| 176 | * |
||
| 177 | 137 | * It normalize the short definition form and create a Relation object from it. |
|
| 178 | 137 | * |
|
| 179 | * @param string $relation |
||
| 180 | * @return Relation |
||
| 181 | 137 | * @throws InvalidConfiguration |
|
| 182 | * @throws UndefinedRelation |
||
| 183 | */ |
||
| 184 | public static function getRelation($relation) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the table name |
||
| 201 | * |
||
| 202 | 126 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
|
| 203 | * $tableName. |
||
| 204 | 126 | * |
|
| 205 | 126 | * @return string |
|
| 206 | * @throws InvalidName|InvalidConfiguration |
||
| 207 | 126 | */ |
|
| 208 | public static function getTableName() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Initialize the validator for this Entity. |
||
| 220 | 85 | */ |
|
| 221 | public static function initValidator() |
||
| 225 | |||
| 226 | 84 | /** |
|
| 227 | * Check if the table has a auto increment column. |
||
| 228 | 84 | * |
|
| 229 | 15 | * @return bool |
|
| 230 | */ |
||
| 231 | public static function isAutoIncremented() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Validate $fields. |
||
| 238 | * |
||
| 239 | * $fields has to be an array of $field => $value |
||
| 240 | * |
||
| 241 | * @param array $fields |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | 137 | public static function validateArray(array $fields) |
|
| 252 | |||
| 253 | 126 | /** |
|
| 254 | 126 | * Validate $value for $field. |
|
| 255 | 126 | * |
|
| 256 | 126 | * @param string $field |
|
| 257 | * @param mixed $value |
||
| 258 | 124 | * @return bool|Error |
|
| 259 | 2 | * @throws Exception |
|
| 260 | */ |
||
| 261 | public static function validate($field, $value) |
||
| 269 | |||
| 270 | /** |
||
| 271 | 2 | * Check if the validator is initialized. |
|
| 272 | * |
||
| 273 | 2 | * @return bool |
|
| 274 | 2 | */ |
|
| 275 | public static function validatorIsInitialized() |
||
| 279 | |||
| 280 | /** |
||
| 281 | 14 | * Empty event handler |
|
| 282 | * |
||
| 283 | 14 | * Get called when the entity get initialized. |
|
| 284 | * |
||
| 285 | * @param bool $new Whether or not the entity is new or from database |
||
| 286 | */ |
||
| 287 | public function onInit($new) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param EM $entityManager |
||
| 293 | * @return self |
||
| 294 | 1 | */ |
|
| 295 | public function setEntityManager(EM $entityManager) |
||
| 300 | 1 | ||
| 301 | /** |
||
| 302 | * Get the value from $var |
||
| 303 | * |
||
| 304 | * If there is a custom getter this method get called instead. |
||
| 305 | * |
||
| 306 | * @param string $var The variable to get |
||
| 307 | * @return mixed|null |
||
| 308 | * @throws IncompletePrimaryKey |
||
| 309 | * @throws InvalidConfiguration |
||
| 310 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 311 | 5 | */ |
|
| 312 | public function __get($var) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Set $var to $value |
||
| 333 | * |
||
| 334 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 335 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 336 | * written and give wrong results for dirty checking. |
||
| 337 | 105 | * |
|
| 338 | * The onChange event is called after something got changed. |
||
| 339 | 105 | * |
|
| 340 | * @param string $var The variable to change |
||
| 341 | * @param mixed $value The value to store |
||
| 342 | * @throws IncompletePrimaryKey |
||
| 343 | * @throws InvalidConfiguration |
||
| 344 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 345 | 2 | */ |
|
| 346 | public function __set($var, $value) |
||
| 368 | 4 | ||
| 369 | /** |
||
| 370 | 83 | * Empty event handler |
|
| 371 | 83 | * |
|
| 372 | * Get called when something is changed with magic setter. |
||
| 373 | 83 | * |
|
| 374 | 1 | * @param string $var The variable that got changed.merge(node.inheritedProperties) |
|
| 375 | * @param mixed $oldValue The old value of the variable |
||
| 376 | * @param mixed $value The new value of the variable |
||
| 377 | 82 | */ |
|
| 378 | public function onChange($var, $oldValue, $value) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get related objects |
||
| 384 | * |
||
| 385 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 386 | * $refresh to true. |
||
| 387 | * |
||
| 388 | * @param string $relation |
||
| 389 | * @param bool $refresh |
||
| 390 | * @return mixed |
||
| 391 | * @throws Exceptions\NoConnection |
||
| 392 | * @throws Exceptions\NoEntity |
||
| 393 | * @throws IncompletePrimaryKey |
||
| 394 | * @throws InvalidConfiguration |
||
| 395 | * @throws NoEntityManager |
||
| 396 | 18 | * @throws UndefinedRelation |
|
| 397 | */ |
||
| 398 | 18 | public function getRelated($relation, $refresh = false) |
|
| 406 | 3 | ||
| 407 | 3 | /** |
|
| 408 | * Set $relation to $entity |
||
| 409 | 15 | * |
|
| 410 | 15 | * This method is only for the owner of a relation. |
|
| 411 | 15 | * |
|
| 412 | * @param string $relation |
||
| 413 | * @param Entity $entity |
||
| 414 | 18 | * @throws IncompletePrimaryKey |
|
| 415 | 15 | * @throws InvalidRelation |
|
| 416 | */ |
||
| 417 | 18 | public function setRelated($relation, Entity $entity = null) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Add relations for $relation to $entities |
||
| 426 | * |
||
| 427 | * This method is only for many-to-many relations. |
||
| 428 | 6 | * |
|
| 429 | * This method does not take care about already existing relations and will fail hard. |
||
| 430 | 6 | * |
|
| 431 | * @param string $relation |
||
| 432 | * @param Entity[] $entities |
||
| 433 | * @throws NoEntityManager |
||
| 434 | */ |
||
| 435 | public function addRelated($relation, array $entities) |
||
| 448 | 11 | ||
| 449 | /** |
||
| 450 | 11 | * Delete relations for $relation to $entities |
|
| 451 | 9 | * |
|
| 452 | * This method is only for many-to-many relations. |
||
| 453 | * |
||
| 454 | 11 | * @param string $relation |
|
| 455 | * @param Entity[] $entities |
||
| 456 | * @throws NoEntityManager |
||
| 457 | */ |
||
| 458 | public function deleteRelated($relation, $entities) |
||
| 471 | 4 | ||
| 472 | 4 | /** |
|
| 473 | * Resets the entity or $var to original data |
||
| 474 | * |
||
| 475 | * @param string $var Reset only this variable or all variables |
||
| 476 | * @throws InvalidConfiguration |
||
| 477 | */ |
||
| 478 | public function reset($var = null) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Save the entity to EntityManager |
||
| 495 | * |
||
| 496 | 8 | * @return Entity |
|
| 497 | 4 | * @throws Exceptions\NoConnection |
|
| 498 | * @throws Exceptions\NoEntity |
||
| 499 | * @throws Exceptions\NotScalar |
||
| 500 | * @throws Exceptions\UnsupportedDriver |
||
| 501 | * @throws IncompletePrimaryKey |
||
| 502 | * @throws InvalidConfiguration |
||
| 503 | * @throws InvalidName |
||
| 504 | * @throws NoEntityManager |
||
| 505 | */ |
||
| 506 | public function save() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Checks if entity or $var got changed |
||
| 552 | * |
||
| 553 | * @param string $var Check only this variable or all variables |
||
| 554 | * @return bool |
||
| 555 | * @throws InvalidConfiguration |
||
| 556 | 12 | */ |
|
| 557 | public function isDirty($var = null) |
||
| 570 | |||
| 571 | /** |
||
| 572 | 12 | * Empty event handler |
|
| 573 | 2 | * |
|
| 574 | 2 | * Get called before the entity get updated in database. |
|
| 575 | 5 | */ |
|
| 576 | 4 | public function preUpdate() |
|
| 579 | |||
| 580 | 5 | /** |
|
| 581 | 5 | * Empty event handler |
|
| 582 | 4 | * |
|
| 583 | 4 | * Get called before the entity get inserted in database. |
|
| 584 | 4 | */ |
|
| 585 | 4 | public function prePersist() |
|
| 588 | |||
| 589 | |||
| 590 | // DEPRECATED stuff |
||
| 591 | 11 | ||
| 592 | 10 | /** |
|
| 593 | 10 | * Empty event handler |
|
| 594 | 10 | * |
|
| 595 | * Get called after the entity got inserted in database. |
||
| 596 | */ |
||
| 597 | 11 | public function postPersist() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Empty event handler |
||
| 603 | * |
||
| 604 | * Get called after the entity got updated in database. |
||
| 605 | */ |
||
| 606 | public function postUpdate() |
||
| 609 | 18 | ||
| 610 | 4 | /** |
|
| 611 | 4 | * Fetches related objects |
|
| 612 | 4 | * |
|
| 613 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 614 | * |
||
| 615 | 15 | * It will throw an error for non owner when the key is incomplete. |
|
| 616 | 15 | * |
|
| 617 | * @param string $relation The relation to fetch |
||
| 618 | 15 | * @param bool $getAll |
|
| 619 | * @return Entity|Entity[]|EntityFetcher |
||
| 620 | * @throws NoEntityManager |
||
| 621 | */ |
||
| 622 | public function fetch($relation, $getAll = false) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get the primary key |
||
| 645 | * |
||
| 646 | * @return array |
||
| 647 | 5 | * @throws IncompletePrimaryKey |
|
| 648 | */ |
||
| 649 | 5 | public function getPrimaryKey() |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Get current data |
||
| 664 | * |
||
| 665 | * @return array |
||
| 666 | * @internal |
||
| 667 | */ |
||
| 668 | public function getData() |
||
| 672 | 19 | ||
| 673 | /** |
||
| 674 | * Set new original data |
||
| 675 | * |
||
| 676 | * @param array $data |
||
| 677 | * @internal |
||
| 678 | */ |
||
| 679 | public function setOriginalData(array $data) |
||
| 683 | |||
| 684 | 19 | /** |
|
| 685 | * String representation of data |
||
| 686 | 19 | * |
|
| 687 | 4 | * @link http://php.net/manual/en/serializable.serialize.php |
|
| 688 | * @return string |
||
| 689 | 15 | */ |
|
| 690 | public function serialize() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Constructs the object |
||
| 697 | * |
||
| 698 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 699 | 38 | * @param string $serialized The string representation of data |
|
| 700 | */ |
||
| 701 | 38 | public function unserialize($serialized) |
|
| 707 | 36 | ||
| 708 | /** |
||
| 709 | 34 | * @return string |
|
| 710 | * @deprecated use getOption from EntityManager |
||
| 711 | * @codeCoverageIgnore deprecated |
||
| 712 | */ |
||
| 713 | public static function getTableNameTemplate() |
||
| 717 | |||
| 718 | 20 | /** |
|
| 719 | * @param string $tableNameTemplate |
||
| 720 | 20 | * @deprecated use setOption from EntityManager |
|
| 721 | * @codeCoverageIgnore deprecated |
||
| 722 | */ |
||
| 723 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 727 | |||
| 728 | /** |
||
| 729 | 18 | * @return string |
|
| 730 | * @deprecated use getOption from EntityManager |
||
| 731 | 18 | * @codeCoverageIgnore deprecated |
|
| 732 | 18 | */ |
|
| 733 | public static function getNamingSchemeTable() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @param string $namingSchemeTable |
||
| 740 | 2 | * @deprecated use setOption from EntityManager |
|
| 741 | * @codeCoverageIgnore deprecated |
||
| 742 | 2 | */ |
|
| 743 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @return string |
||
| 750 | * @deprecated use getOption from EntityManager |
||
| 751 | 3 | * @codeCoverageIgnore deprecated |
|
| 752 | */ |
||
| 753 | 3 | public static function getNamingSchemeColumn() |
|
| 757 | |||
| 758 | /** |
||
| 759 | * @param string $namingSchemeColumn |
||
| 760 | * @deprecated use setOption from EntityManager |
||
| 761 | * @codeCoverageIgnore deprecated |
||
| 762 | */ |
||
| 763 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @return string |
||
| 770 | * @deprecated use getOption from EntityManager |
||
| 771 | * @codeCoverageIgnore deprecated |
||
| 772 | */ |
||
| 773 | public static function getNamingSchemeMethods() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @param string $namingSchemeMethods |
||
| 780 | * @deprecated use setOption from EntityManager |
||
| 781 | * @codeCoverageIgnore deprecated |
||
| 782 | */ |
||
| 783 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 787 | } |
||
| 788 |