Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class Model implements ArrayAccess |
||
| 45 | { |
||
| 46 | const DEFAULT_ID_NAME = 'id'; |
||
| 47 | |||
| 48 | ///////////////////////////// |
||
| 49 | // Model visible variables |
||
| 50 | ///////////////////////////// |
||
| 51 | |||
| 52 | /** |
||
| 53 | * List of model ID property names. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected static $ids = [self::DEFAULT_ID_NAME]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Property definitions expressed as a key-value map with |
||
| 61 | * property names as the keys. |
||
| 62 | * i.e. ['enabled' => ['type' => Type::BOOLEAN]]. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected static $properties = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $_values = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $_unsaved = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | protected $_persisted = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $_relationships = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var AbstractRelation[] |
||
| 90 | */ |
||
| 91 | private $relationships = []; |
||
| 92 | |||
| 93 | ///////////////////////////// |
||
| 94 | // Base model variables |
||
| 95 | ///////////////////////////// |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private static $initialized = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var DriverInterface |
||
| 104 | */ |
||
| 105 | private static $driver; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private static $accessors = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | private static $mutators = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private static $dispatchers = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $tablename; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | private $hasId; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private $idValues; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var bool |
||
| 139 | */ |
||
| 140 | private $loaded = false; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var Errors |
||
| 144 | */ |
||
| 145 | private $errors; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var bool |
||
| 149 | */ |
||
| 150 | private $ignoreUnsaved; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Creates a new model object. |
||
| 154 | * |
||
| 155 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
|
|
|||
| 156 | * @param array $values optional key-value map to pre-seed model |
||
| 157 | */ |
||
| 158 | public function __construct(array $values = []) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Performs initialization on this model. |
||
| 188 | */ |
||
| 189 | private function init() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * The initialize() method is called once per model. This is a great |
||
| 201 | * place to install event listeners. |
||
| 202 | */ |
||
| 203 | protected function initialize() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the driver for all models. |
||
| 220 | */ |
||
| 221 | public static function setDriver(DriverInterface $driver) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets the driver for all models. |
||
| 228 | * |
||
| 229 | * @throws DriverMissingException when a driver has not been set yet |
||
| 230 | */ |
||
| 231 | public static function getDriver(): DriverInterface |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Clears the driver for all models. |
||
| 242 | */ |
||
| 243 | public static function clearDriver() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Gets the name of the model, i.e. User. |
||
| 250 | */ |
||
| 251 | public static function modelName(): string |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the model ID. |
||
| 261 | * |
||
| 262 | * @return string|number|false ID |
||
| 263 | */ |
||
| 264 | public function id() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets a key-value map of the model ID. |
||
| 284 | * |
||
| 285 | * @return array ID map |
||
| 286 | */ |
||
| 287 | public function ids(): array |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks if the model has an identifier present. |
||
| 294 | * This does not indicate whether the model has been |
||
| 295 | * persisted to the database or loaded from the database. |
||
| 296 | */ |
||
| 297 | public function hasId(): bool |
||
| 301 | |||
| 302 | ///////////////////////////// |
||
| 303 | // Magic Methods |
||
| 304 | ///////////////////////////// |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Converts the model into a string. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function __toString() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Shortcut to a get() call for a given property. |
||
| 321 | * |
||
| 322 | * @param string $name |
||
| 323 | * |
||
| 324 | * @return mixed |
||
| 325 | */ |
||
| 326 | public function __get($name) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Sets an unsaved value. |
||
| 335 | * |
||
| 336 | * @param string $name |
||
| 337 | * @param mixed $value |
||
| 338 | */ |
||
| 339 | public function __set($name, $value) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Checks if an unsaved value or property exists by this name. |
||
| 369 | * |
||
| 370 | * @param string $name |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function __isset($name) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Unsets an unsaved value. |
||
| 385 | * |
||
| 386 | * @param string $name |
||
| 387 | */ |
||
| 388 | public function __unset($name) |
||
| 399 | |||
| 400 | ///////////////////////////// |
||
| 401 | // ArrayAccess Interface |
||
| 402 | ///////////////////////////// |
||
| 403 | |||
| 404 | public function offsetExists($offset) |
||
| 408 | |||
| 409 | public function offsetGet($offset) |
||
| 413 | |||
| 414 | public function offsetSet($offset, $value) |
||
| 418 | |||
| 419 | public function offsetUnset($offset) |
||
| 423 | |||
| 424 | public static function __callStatic($name, $parameters) |
||
| 431 | |||
| 432 | ///////////////////////////// |
||
| 433 | // Property Definitions |
||
| 434 | ///////////////////////////// |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Gets the definition of all model properties. |
||
| 438 | */ |
||
| 439 | public static function getProperties(): Definition |
||
| 443 | |||
| 444 | /** |
||
| 445 | * The buildDefinition() method is called once per model. It's used |
||
| 446 | * to generate the model definition. This is a great place to add any |
||
| 447 | * dynamic model properties. |
||
| 448 | */ |
||
| 449 | public static function buildDefinition(): Definition |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Gets the definition of a specific property. |
||
| 459 | * |
||
| 460 | * @param string $property property to lookup |
||
| 461 | */ |
||
| 462 | public static function getProperty(string $property): ?Property |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Gets the names of the model ID properties. |
||
| 469 | */ |
||
| 470 | public static function getIDProperties(): array |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Checks if the model has a property. |
||
| 477 | * |
||
| 478 | * @param string $property property |
||
| 479 | * |
||
| 480 | * @return bool has property |
||
| 481 | */ |
||
| 482 | public static function hasProperty(string $property): bool |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Gets the mutator method name for a given property name. |
||
| 489 | * Looks for methods in the form of `setPropertyValue`. |
||
| 490 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 491 | * |
||
| 492 | * @param string $property property |
||
| 493 | * |
||
| 494 | * @return string|null method name if it exists |
||
| 495 | */ |
||
| 496 | public static function getMutator(string $property): ?string |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Gets the accessor method name for a given property name. |
||
| 517 | * Looks for methods in the form of `getPropertyValue`. |
||
| 518 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 519 | * |
||
| 520 | * @param string $property property |
||
| 521 | * |
||
| 522 | * @return string|null method name if it exists |
||
| 523 | */ |
||
| 524 | public static function getAccessor(string $property): ?string |
||
| 542 | |||
| 543 | ///////////////////////////// |
||
| 544 | // CRUD Operations |
||
| 545 | ///////////////////////////// |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Gets the table name for storing this model. |
||
| 549 | */ |
||
| 550 | public function getTablename(): string |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Gets the ID of the connection in the connection manager |
||
| 563 | * that stores this model. |
||
| 564 | */ |
||
| 565 | public function getConnection(): ?string |
||
| 569 | |||
| 570 | protected function usesTransactions(): bool |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Saves the model. |
||
| 577 | * |
||
| 578 | * @return bool true when the operation was successful |
||
| 579 | */ |
||
| 580 | public function save(): bool |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Saves the model. Throws an exception when the operation fails. |
||
| 591 | * |
||
| 592 | * @throws ModelException when the model cannot be saved |
||
| 593 | */ |
||
| 594 | public function saveOrFail() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Creates a new model. |
||
| 608 | * |
||
| 609 | * @param array $data optional key-value properties to set |
||
| 610 | * |
||
| 611 | * @return bool true when the operation was successful |
||
| 612 | * |
||
| 613 | * @throws BadMethodCallException when called on an existing model |
||
| 614 | */ |
||
| 615 | public function create(array $data = []): bool |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Ignores unsaved values when fetching the next value. |
||
| 732 | * |
||
| 733 | * @return $this |
||
| 734 | */ |
||
| 735 | public function ignoreUnsaved() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Fetches property values from the model. |
||
| 744 | * |
||
| 745 | * This method looks up values in this order: |
||
| 746 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 747 | * |
||
| 748 | * @param array $properties list of property names to fetch values of |
||
| 749 | */ |
||
| 750 | public function get(array $properties): array |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Gets a property value from the model. |
||
| 791 | * |
||
| 792 | * Values are looked up in this order: |
||
| 793 | * 1. unsaved values |
||
| 794 | * 2. local values |
||
| 795 | * 3. default value |
||
| 796 | * 4. null |
||
| 797 | * |
||
| 798 | * @return mixed |
||
| 799 | */ |
||
| 800 | private function getValue(string $name, array $values) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Populates a newly created model with its ID. |
||
| 825 | */ |
||
| 826 | private function getNewID() |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Sets a collection values on the model from an untrusted input. |
||
| 849 | * |
||
| 850 | * @param array $values |
||
| 851 | * |
||
| 852 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 853 | * |
||
| 854 | * @return $this |
||
| 855 | */ |
||
| 856 | public function setValues($values) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Converts the model to an array. |
||
| 879 | */ |
||
| 880 | public function toArray(): array |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Checks if the unsaved value for a property is present and |
||
| 926 | * is different from the original value. |
||
| 927 | * |
||
| 928 | * @property string $name |
||
| 929 | * @property bool $hasChanged when true, checks if the unsaved value is different from the saved value |
||
| 930 | */ |
||
| 931 | public function dirty(string $name, bool $hasChanged = false): bool |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Updates the model. |
||
| 946 | * |
||
| 947 | * @param array $data optional key-value properties to set |
||
| 948 | * |
||
| 949 | * @return bool true when the operation was successful |
||
| 950 | * |
||
| 951 | * @throws BadMethodCallException when not called on an existing model |
||
| 952 | */ |
||
| 953 | public function set(array $data = []): bool |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Delete the model. |
||
| 1045 | * |
||
| 1046 | * @return bool true when the operation was successful |
||
| 1047 | */ |
||
| 1048 | public function delete(): bool |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Restores a soft-deleted model. |
||
| 1101 | */ |
||
| 1102 | public function restore(): bool |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Checks if the model has been deleted. |
||
| 1139 | */ |
||
| 1140 | public function isDeleted(): bool |
||
| 1148 | |||
| 1149 | ///////////////////////////// |
||
| 1150 | // Queries |
||
| 1151 | ///////////////////////////// |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Generates a new query instance. |
||
| 1155 | */ |
||
| 1156 | public static function query(): Query |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Generates a new query instance that includes soft-deleted models. |
||
| 1174 | */ |
||
| 1175 | public static function withDeleted(): Query |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Finds a single instance of a model given it's ID. |
||
| 1187 | * |
||
| 1188 | * @param mixed $id |
||
| 1189 | * |
||
| 1190 | * @return static|null |
||
| 1191 | */ |
||
| 1192 | public static function find($id): ?self |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1212 | * |
||
| 1213 | * @param mixed $id |
||
| 1214 | * |
||
| 1215 | * @return static |
||
| 1216 | * |
||
| 1217 | * @throws ModelNotFoundException when a model could not be found |
||
| 1218 | */ |
||
| 1219 | public static function findOrFail($id): self |
||
| 1228 | |||
| 1229 | /** |
||
| 1230 | * Tells if this model instance has been persisted to the data layer. |
||
| 1231 | * |
||
| 1232 | * NOTE: this does not actually perform a check with the data layer |
||
| 1233 | */ |
||
| 1234 | public function persisted(): bool |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * Loads the model from the storage layer. |
||
| 1241 | * |
||
| 1242 | * @return $this |
||
| 1243 | */ |
||
| 1244 | public function refresh() |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Loads values into the model. |
||
| 1264 | * |
||
| 1265 | * @param array $values values |
||
| 1266 | * |
||
| 1267 | * @return $this |
||
| 1268 | */ |
||
| 1269 | public function refreshWith(array $values) |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Clears the cache for this model. |
||
| 1287 | * |
||
| 1288 | * @return $this |
||
| 1289 | */ |
||
| 1290 | public function clearCache() |
||
| 1299 | |||
| 1300 | ///////////////////////////// |
||
| 1301 | // Relationships |
||
| 1302 | ///////////////////////////// |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * Gets the relationship manager for a property. |
||
| 1306 | * |
||
| 1307 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1308 | */ |
||
| 1309 | private function getRelationship(Property $property): AbstractRelation |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * Saves any unsaved models attached through a relationship. This will only |
||
| 1321 | * save attached models that have not been saved yet. |
||
| 1322 | */ |
||
| 1323 | private function saveRelationships(bool $usesTransactions): bool |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * This hydrates an individual property in the model. It can be a |
||
| 1356 | * scalar value or relationship. |
||
| 1357 | * |
||
| 1358 | * @internal |
||
| 1359 | * |
||
| 1360 | * @param $value |
||
| 1361 | */ |
||
| 1362 | public function hydrateValue(string $name, $value): void |
||
| 1370 | |||
| 1371 | /** |
||
| 1372 | * @deprecated |
||
| 1373 | * |
||
| 1374 | * Gets the model(s) for a relationship |
||
| 1375 | * |
||
| 1376 | * @param string $k property |
||
| 1377 | * |
||
| 1378 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1379 | * |
||
| 1380 | * @return Model|array|null |
||
| 1381 | */ |
||
| 1382 | public function relation(string $k) |
||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * @deprecated |
||
| 1394 | * |
||
| 1395 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1396 | * |
||
| 1397 | * @return $this |
||
| 1398 | */ |
||
| 1399 | public function setRelation(string $k, Model $model) |
||
| 1406 | |||
| 1407 | /** |
||
| 1408 | * @deprecated |
||
| 1409 | * |
||
| 1410 | * Sets the model for a one-to-many relationship |
||
| 1411 | * |
||
| 1412 | * @return $this |
||
| 1413 | */ |
||
| 1414 | public function setRelationCollection(string $k, iterable $models) |
||
| 1420 | |||
| 1421 | /** |
||
| 1422 | * @deprecated |
||
| 1423 | * |
||
| 1424 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null |
||
| 1425 | * |
||
| 1426 | * @return $this |
||
| 1427 | */ |
||
| 1428 | public function clearRelation(string $k) |
||
| 1435 | |||
| 1436 | ///////////////////////////// |
||
| 1437 | // Events |
||
| 1438 | ///////////////////////////// |
||
| 1439 | |||
| 1440 | /** |
||
| 1441 | * Gets the event dispatcher. |
||
| 1442 | */ |
||
| 1443 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Subscribes to a listener to an event. |
||
| 1455 | * |
||
| 1456 | * @param string $event event name |
||
| 1457 | * @param int $priority optional priority, higher #s get called first |
||
| 1458 | */ |
||
| 1459 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * Adds a listener to the model.creating and model.updating events. |
||
| 1466 | */ |
||
| 1467 | public static function saving(callable $listener, int $priority = 0) |
||
| 1472 | |||
| 1473 | /** |
||
| 1474 | * Adds a listener to the model.created and model.updated events. |
||
| 1475 | */ |
||
| 1476 | public static function saved(callable $listener, int $priority = 0) |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * Adds a listener to the model.creating event. |
||
| 1484 | */ |
||
| 1485 | public static function creating(callable $listener, int $priority = 0) |
||
| 1489 | |||
| 1490 | /** |
||
| 1491 | * Adds a listener to the model.created event. |
||
| 1492 | */ |
||
| 1493 | public static function created(callable $listener, int $priority = 0) |
||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * Adds a listener to the model.updating event. |
||
| 1500 | */ |
||
| 1501 | public static function updating(callable $listener, int $priority = 0) |
||
| 1505 | |||
| 1506 | /** |
||
| 1507 | * Adds a listener to the model.updated event. |
||
| 1508 | */ |
||
| 1509 | public static function updated(callable $listener, int $priority = 0) |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Adds a listener to the model.deleting event. |
||
| 1516 | */ |
||
| 1517 | public static function deleting(callable $listener, int $priority = 0) |
||
| 1521 | |||
| 1522 | /** |
||
| 1523 | * Adds a listener to the model.deleted event. |
||
| 1524 | */ |
||
| 1525 | public static function deleted(callable $listener, int $priority = 0) |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Dispatches the given event and checks if it was successful. |
||
| 1532 | * |
||
| 1533 | * @return bool true if the events were successfully propagated |
||
| 1534 | */ |
||
| 1535 | private function performDispatch(string $eventName, bool $usesTransactions): bool |
||
| 1551 | |||
| 1552 | ///////////////////////////// |
||
| 1553 | // Validation |
||
| 1554 | ///////////////////////////// |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * Gets the error stack for this model. |
||
| 1558 | */ |
||
| 1559 | public function getErrors(): Errors |
||
| 1567 | |||
| 1568 | /** |
||
| 1569 | * Checks if the model in its current state is valid. |
||
| 1570 | */ |
||
| 1571 | public function valid(): bool |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Validates and marshals a property value prior to saving. |
||
| 1595 | * |
||
| 1596 | * @param Property $property property definition |
||
| 1597 | * @param mixed $value |
||
| 1598 | */ |
||
| 1599 | private function filterAndValidate(Property $property, &$value): bool |
||
| 1629 | } |
||
| 1630 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.