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 |
||
| 43 | abstract class Model implements ArrayAccess |
||
| 44 | { |
||
| 45 | /** @deprecated */ |
||
| 46 | const IMMUTABLE = 'immutable'; |
||
| 47 | /** @deprecated */ |
||
| 48 | const MUTABLE_CREATE_ONLY = 'mutable_create_only'; |
||
| 49 | /** @deprecated */ |
||
| 50 | const MUTABLE = 'mutable'; |
||
| 51 | |||
| 52 | /** @deprecated */ |
||
| 53 | const TYPE_STRING = 'string'; |
||
| 54 | /** @deprecated */ |
||
| 55 | const TYPE_INTEGER = 'integer'; |
||
| 56 | /** @deprecated */ |
||
| 57 | const TYPE_FLOAT = 'float'; |
||
| 58 | /** @deprecated */ |
||
| 59 | const TYPE_BOOLEAN = 'boolean'; |
||
| 60 | /** @deprecated */ |
||
| 61 | const TYPE_DATE = 'date'; |
||
| 62 | /** @deprecated */ |
||
| 63 | const TYPE_OBJECT = 'object'; |
||
| 64 | /** @deprecated */ |
||
| 65 | const TYPE_ARRAY = 'array'; |
||
| 66 | |||
| 67 | /** @deprecated */ |
||
| 68 | const RELATIONSHIP_HAS_ONE = 'has_one'; |
||
| 69 | /** @deprecated */ |
||
| 70 | const RELATIONSHIP_HAS_MANY = 'has_many'; |
||
| 71 | /** @deprecated */ |
||
| 72 | const RELATIONSHIP_BELONGS_TO = 'belongs_to'; |
||
| 73 | /** @deprecated */ |
||
| 74 | const RELATIONSHIP_BELONGS_TO_MANY = 'belongs_to_many'; |
||
| 75 | |||
| 76 | const DEFAULT_ID_NAME = 'id'; |
||
| 77 | |||
| 78 | ///////////////////////////// |
||
| 79 | // Model visible variables |
||
| 80 | ///////////////////////////// |
||
| 81 | |||
| 82 | /** |
||
| 83 | * List of model ID property names. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected static $ids = [self::DEFAULT_ID_NAME]; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Property definitions expressed as a key-value map with |
||
| 91 | * property names as the keys. |
||
| 92 | * i.e. ['enabled' => ['type' => Type::BOOLEAN]]. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected static $properties = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $_values = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $_unsaved = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | protected $_persisted = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $_relationships = []; |
||
| 117 | |||
| 118 | ///////////////////////////// |
||
| 119 | // Base model variables |
||
| 120 | ///////////////////////////// |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | private static $initialized = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var DriverInterface |
||
| 129 | */ |
||
| 130 | private static $driver; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private static $accessors = []; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private static $mutators = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | private static $dispatchers = []; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var bool |
||
| 149 | */ |
||
| 150 | private $hasId; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | private $idValues; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var bool |
||
| 159 | */ |
||
| 160 | private $loaded = false; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var Errors |
||
| 164 | */ |
||
| 165 | private $errors; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var bool |
||
| 169 | */ |
||
| 170 | private $ignoreUnsaved; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Creates a new model object. |
||
| 174 | * |
||
| 175 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 176 | * @param array $values optional key-value map to pre-seed model |
||
| 177 | */ |
||
| 178 | public function __construct($id = false, array $values = []) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Performs initialization on this model. |
||
| 194 | */ |
||
| 195 | private function init() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * The initialize() method is called once per model. This is a great |
||
| 207 | * place to install event listeners. |
||
| 208 | */ |
||
| 209 | protected function initialize() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Parses the given ID, which can be a single or composite primary key. |
||
| 226 | * |
||
| 227 | * @param mixed $id |
||
| 228 | */ |
||
| 229 | private function parseId($id) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Sets the driver for all models. |
||
| 277 | */ |
||
| 278 | public static function setDriver(DriverInterface $driver) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Gets the driver for all models. |
||
| 285 | * |
||
| 286 | * @throws DriverMissingException when a driver has not been set yet |
||
| 287 | */ |
||
| 288 | public static function getDriver(): DriverInterface |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Clears the driver for all models. |
||
| 299 | */ |
||
| 300 | public static function clearDriver() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Gets the name of the model, i.e. User. |
||
| 307 | */ |
||
| 308 | public static function modelName(): string |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Gets the model ID. |
||
| 318 | * |
||
| 319 | * @return string|number|false ID |
||
| 320 | */ |
||
| 321 | public function id() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Gets a key-value map of the model ID. |
||
| 341 | * |
||
| 342 | * @return array ID map |
||
| 343 | */ |
||
| 344 | public function ids(): array |
||
| 348 | |||
| 349 | ///////////////////////////// |
||
| 350 | // Magic Methods |
||
| 351 | ///////////////////////////// |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Converts the model into a string. |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function __toString() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Shortcut to a get() call for a given property. |
||
| 368 | * |
||
| 369 | * @param string $name |
||
| 370 | * |
||
| 371 | * @return mixed |
||
| 372 | */ |
||
| 373 | public function __get($name) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Sets an unsaved value. |
||
| 382 | * |
||
| 383 | * @param string $name |
||
| 384 | * @param mixed $value |
||
| 385 | */ |
||
| 386 | public function __set($name, $value) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Checks if an unsaved value or property exists by this name. |
||
| 412 | * |
||
| 413 | * @param string $name |
||
| 414 | * |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function __isset($name) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Unsets an unsaved value. |
||
| 424 | * |
||
| 425 | * @param string $name |
||
| 426 | */ |
||
| 427 | public function __unset($name) |
||
| 438 | |||
| 439 | ///////////////////////////// |
||
| 440 | // ArrayAccess Interface |
||
| 441 | ///////////////////////////// |
||
| 442 | |||
| 443 | public function offsetExists($offset) |
||
| 447 | |||
| 448 | public function offsetGet($offset) |
||
| 452 | |||
| 453 | public function offsetSet($offset, $value) |
||
| 457 | |||
| 458 | public function offsetUnset($offset) |
||
| 462 | |||
| 463 | public static function __callStatic($name, $parameters) |
||
| 470 | |||
| 471 | ///////////////////////////// |
||
| 472 | // Property Definitions |
||
| 473 | ///////////////////////////// |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Gets the definition of all model properties. |
||
| 477 | */ |
||
| 478 | public static function getProperties(): Definition |
||
| 482 | |||
| 483 | /** |
||
| 484 | * The buildDefinition() method is called once per model. It's used |
||
| 485 | * to generate the model definition. This is a great place to add any |
||
| 486 | * dynamic model properties. |
||
| 487 | */ |
||
| 488 | public static function buildDefinition(): Definition |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Gets the definition of a specific property. |
||
| 498 | * |
||
| 499 | * @param string $property property to lookup |
||
| 500 | */ |
||
| 501 | public static function getProperty(string $property): ?Property |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Gets the names of the model ID properties. |
||
| 508 | */ |
||
| 509 | public static function getIDProperties(): array |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Checks if the model has a property. |
||
| 516 | * |
||
| 517 | * @param string $property property |
||
| 518 | * |
||
| 519 | * @return bool has property |
||
| 520 | */ |
||
| 521 | public static function hasProperty(string $property): bool |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Gets the mutator method name for a given proeprty name. |
||
| 528 | * Looks for methods in the form of `setPropertyValue`. |
||
| 529 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 530 | * |
||
| 531 | * @param string $property property |
||
| 532 | * |
||
| 533 | * @return string|null method name if it exists |
||
| 534 | */ |
||
| 535 | public static function getMutator(string $property): ?string |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Gets the accessor method name for a given proeprty name. |
||
| 556 | * Looks for methods in the form of `getPropertyValue`. |
||
| 557 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 558 | * |
||
| 559 | * @param string $property property |
||
| 560 | * |
||
| 561 | * @return string|null method name if it exists |
||
| 562 | */ |
||
| 563 | public static function getAccessor(string $property): ?string |
||
| 581 | |||
| 582 | ///////////////////////////// |
||
| 583 | // CRUD Operations |
||
| 584 | ///////////////////////////// |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Gets the tablename for storing this model. |
||
| 588 | */ |
||
| 589 | public function getTablename(): string |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Gets the ID of the connection in the connection manager |
||
| 598 | * that stores this model. |
||
| 599 | */ |
||
| 600 | public function getConnection(): ?string |
||
| 604 | |||
| 605 | protected function usesTransactions(): bool |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Saves the model. |
||
| 612 | * |
||
| 613 | * @return bool true when the operation was successful |
||
| 614 | */ |
||
| 615 | public function save(): bool |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Saves the model. Throws an exception when the operation fails. |
||
| 626 | * |
||
| 627 | * @throws ModelException when the model cannot be saved |
||
| 628 | */ |
||
| 629 | public function saveOrFail() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Creates a new model. |
||
| 643 | * |
||
| 644 | * @param array $data optional key-value properties to set |
||
| 645 | * |
||
| 646 | * @return bool true when the operation was successful |
||
| 647 | * |
||
| 648 | * @throws BadMethodCallException when called on an existing model |
||
| 649 | */ |
||
| 650 | public function create(array $data = []): bool |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Ignores unsaved values when fetching the next value. |
||
| 762 | * |
||
| 763 | * @return $this |
||
| 764 | */ |
||
| 765 | public function ignoreUnsaved() |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Fetches property values from the model. |
||
| 774 | * |
||
| 775 | * This method looks up values in this order: |
||
| 776 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 777 | * |
||
| 778 | * @param array $properties list of property names to fetch values of |
||
| 779 | */ |
||
| 780 | public function get(array $properties): array |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Gets a property value from the model. |
||
| 821 | * |
||
| 822 | * Values are looked up in this order: |
||
| 823 | * 1. unsaved values |
||
| 824 | * 2. local values |
||
| 825 | * 3. default value |
||
| 826 | * 4. null |
||
| 827 | * |
||
| 828 | * @param string $property |
||
| 829 | * |
||
| 830 | * @return mixed |
||
| 831 | */ |
||
| 832 | protected function getValue($property, array $values) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Populates a newly created model with its ID. |
||
| 852 | */ |
||
| 853 | protected function getNewID() |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Sets a collection values on the model from an untrusted input. |
||
| 876 | * |
||
| 877 | * @param array $values |
||
| 878 | * |
||
| 879 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 880 | * |
||
| 881 | * @return $this |
||
| 882 | */ |
||
| 883 | public function setValues($values) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Converts the model to an array. |
||
| 906 | */ |
||
| 907 | public function toArray(): array |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Updates the model. |
||
| 935 | * |
||
| 936 | * @param array $data optional key-value properties to set |
||
| 937 | * |
||
| 938 | * @return bool true when the operation was successful |
||
| 939 | * |
||
| 940 | * @throws BadMethodCallException when not called on an existing model |
||
| 941 | */ |
||
| 942 | public function set(array $data = []): bool |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Delete the model. |
||
| 1029 | * |
||
| 1030 | * @return bool true when the operation was successful |
||
| 1031 | */ |
||
| 1032 | public function delete(): bool |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Restores a soft-deleted model. |
||
| 1085 | */ |
||
| 1086 | public function restore(): bool |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Checks if the model has been deleted. |
||
| 1123 | */ |
||
| 1124 | public function isDeleted(): bool |
||
| 1132 | |||
| 1133 | ///////////////////////////// |
||
| 1134 | // Queries |
||
| 1135 | ///////////////////////////// |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Generates a new query instance. |
||
| 1139 | */ |
||
| 1140 | public static function query(): Query |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * Generates a new query instance that includes soft-deleted models. |
||
| 1158 | */ |
||
| 1159 | public static function withDeleted(): Query |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Finds a single instance of a model given it's ID. |
||
| 1171 | * |
||
| 1172 | * @param mixed $id |
||
| 1173 | * |
||
| 1174 | * @return static|null |
||
| 1175 | */ |
||
| 1176 | public static function find($id): ?self |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1196 | * |
||
| 1197 | * @param mixed $id |
||
| 1198 | * |
||
| 1199 | * @return static |
||
| 1200 | * |
||
| 1201 | * @throws ModelNotFoundException when a model could not be found |
||
| 1202 | */ |
||
| 1203 | public static function findOrFail($id): self |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Tells if this model instance has been persisted to the data layer. |
||
| 1215 | * |
||
| 1216 | * NOTE: this does not actually perform a check with the data layer |
||
| 1217 | */ |
||
| 1218 | public function persisted(): bool |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Loads the model from the storage layer. |
||
| 1225 | * |
||
| 1226 | * @return $this |
||
| 1227 | */ |
||
| 1228 | public function refresh() |
||
| 1245 | |||
| 1246 | /** |
||
| 1247 | * Loads values into the model. |
||
| 1248 | * |
||
| 1249 | * @param array $values values |
||
| 1250 | * |
||
| 1251 | * @return $this |
||
| 1252 | */ |
||
| 1253 | public function refreshWith(array $values) |
||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * Clears the cache for this model. |
||
| 1271 | * |
||
| 1272 | * @return $this |
||
| 1273 | */ |
||
| 1274 | public function clearCache() |
||
| 1283 | |||
| 1284 | ///////////////////////////// |
||
| 1285 | // Relationships |
||
| 1286 | ///////////////////////////// |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * @deprecated |
||
| 1290 | * |
||
| 1291 | * Gets the model(s) for a relationship |
||
| 1292 | * |
||
| 1293 | * @param string $k property |
||
| 1294 | * |
||
| 1295 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1296 | * |
||
| 1297 | * @return Model|array|null |
||
| 1298 | */ |
||
| 1299 | public function relation(string $k) |
||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * @deprecated |
||
| 1311 | * |
||
| 1312 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1313 | * |
||
| 1314 | * @return $this |
||
| 1315 | */ |
||
| 1316 | public function setRelation(string $k, Model $model) |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * @deprecated |
||
| 1326 | * |
||
| 1327 | * Sets the model for a one-to-many relationship |
||
| 1328 | * |
||
| 1329 | * @return $this |
||
| 1330 | */ |
||
| 1331 | public function setRelationCollection(string $k, iterable $models) |
||
| 1337 | |||
| 1338 | /** |
||
| 1339 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null. |
||
| 1340 | * |
||
| 1341 | * @return $this |
||
| 1342 | */ |
||
| 1343 | public function clearRelation(string $k) |
||
| 1350 | |||
| 1351 | ///////////////////////////// |
||
| 1352 | // Events |
||
| 1353 | ///////////////////////////// |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Gets the event dispatcher. |
||
| 1357 | */ |
||
| 1358 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * Subscribes to a listener to an event. |
||
| 1370 | * |
||
| 1371 | * @param string $event event name |
||
| 1372 | * @param int $priority optional priority, higher #s get called first |
||
| 1373 | */ |
||
| 1374 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
| 1378 | |||
| 1379 | /** |
||
| 1380 | * Adds a listener to the model.creating and model.updating events. |
||
| 1381 | */ |
||
| 1382 | public static function saving(callable $listener, int $priority = 0) |
||
| 1387 | |||
| 1388 | /** |
||
| 1389 | * Adds a listener to the model.created and model.updated events. |
||
| 1390 | */ |
||
| 1391 | public static function saved(callable $listener, int $priority = 0) |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * Adds a listener to the model.creating event. |
||
| 1399 | */ |
||
| 1400 | public static function creating(callable $listener, int $priority = 0) |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * Adds a listener to the model.created event. |
||
| 1407 | */ |
||
| 1408 | public static function created(callable $listener, int $priority = 0) |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Adds a listener to the model.updating event. |
||
| 1415 | */ |
||
| 1416 | public static function updating(callable $listener, int $priority = 0) |
||
| 1420 | |||
| 1421 | /** |
||
| 1422 | * Adds a listener to the model.updated event. |
||
| 1423 | */ |
||
| 1424 | public static function updated(callable $listener, int $priority = 0) |
||
| 1428 | |||
| 1429 | /** |
||
| 1430 | * Adds a listener to the model.deleting event. |
||
| 1431 | */ |
||
| 1432 | public static function deleting(callable $listener, int $priority = 0) |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Adds a listener to the model.deleted event. |
||
| 1439 | */ |
||
| 1440 | public static function deleted(callable $listener, int $priority = 0) |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Dispatches the given event and checks if it was successful. |
||
| 1447 | * |
||
| 1448 | * @return bool true if the events were successfully propagated |
||
| 1449 | */ |
||
| 1450 | private function performDispatch(string $eventName, bool $usesTransactions): bool |
||
| 1466 | |||
| 1467 | ///////////////////////////// |
||
| 1468 | // Validation |
||
| 1469 | ///////////////////////////// |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * Gets the error stack for this model. |
||
| 1473 | */ |
||
| 1474 | public function getErrors(): Errors |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Checks if the model in its current state is valid. |
||
| 1485 | */ |
||
| 1486 | public function valid(): bool |
||
| 1507 | |||
| 1508 | /** |
||
| 1509 | * Validates and marshals a value to storage. |
||
| 1510 | * |
||
| 1511 | * @param Property $property property definition |
||
| 1512 | * @param mixed $value |
||
| 1513 | */ |
||
| 1514 | private function filterAndValidate(Property $property, &$value): bool |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Validates a value for a property. |
||
| 1532 | * |
||
| 1533 | * @param Property $property property definition |
||
| 1534 | * @param mixed $value |
||
| 1535 | */ |
||
| 1536 | private function validateValue(Property $property, $value): array |
||
| 1570 | } |
||
| 1571 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.