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 |
||
51 | abstract class Model implements ArrayAccess |
||
52 | { |
||
53 | const DEFAULT_ID_NAME = 'id'; |
||
54 | |||
55 | ///////////////////////////// |
||
56 | // Model visible variables |
||
57 | ///////////////////////////// |
||
58 | |||
59 | /** |
||
60 | * List of model ID property names. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected static $ids = [self::DEFAULT_ID_NAME]; |
||
65 | |||
66 | /** |
||
67 | * Property definitions expressed as a key-value map with |
||
68 | * property names as the keys. |
||
69 | * i.e. ['enabled' => ['type' => Type::BOOLEAN]]. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected static $properties = []; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $_values = []; |
||
79 | |||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | private $_unsaved = []; |
||
84 | |||
85 | /** |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $_persisted = false; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $_relationships = []; |
||
94 | |||
95 | /** |
||
96 | * @var AbstractRelation[] |
||
97 | */ |
||
98 | private $relationships = []; |
||
99 | |||
100 | ///////////////////////////// |
||
101 | // Base model variables |
||
102 | ///////////////////////////// |
||
103 | |||
104 | /** |
||
105 | * @var array |
||
106 | */ |
||
107 | private static $initialized = []; |
||
108 | |||
109 | /** |
||
110 | * @var DriverInterface |
||
111 | */ |
||
112 | private static $driver; |
||
113 | |||
114 | /** |
||
115 | * @var array |
||
116 | */ |
||
117 | private static $accessors = []; |
||
118 | |||
119 | /** |
||
120 | * @var array |
||
121 | */ |
||
122 | private static $mutators = []; |
||
123 | |||
124 | /** |
||
125 | * @var array |
||
126 | */ |
||
127 | private static $dispatchers = []; |
||
128 | |||
129 | /** |
||
130 | * @var string |
||
131 | */ |
||
132 | private $tablename; |
||
133 | |||
134 | /** |
||
135 | * @var bool |
||
136 | */ |
||
137 | private $hasId; |
||
138 | |||
139 | /** |
||
140 | * @var array |
||
141 | */ |
||
142 | private $idValues; |
||
143 | |||
144 | /** |
||
145 | * @var bool |
||
146 | */ |
||
147 | private $loaded = false; |
||
148 | |||
149 | /** |
||
150 | * @var Errors |
||
151 | */ |
||
152 | private $errors; |
||
153 | |||
154 | /** |
||
155 | * @var bool |
||
156 | */ |
||
157 | private $ignoreUnsaved; |
||
158 | |||
159 | /** |
||
160 | * Creates a new model object. |
||
161 | * |
||
162 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
|
|||
163 | * @param array $values optional key-value map to pre-seed model |
||
164 | */ |
||
165 | public function __construct(array $values = []) |
||
192 | |||
193 | /** |
||
194 | * Performs initialization on this model. |
||
195 | */ |
||
196 | private function init() |
||
205 | |||
206 | /** |
||
207 | * The initialize() method is called once per model. This is a great |
||
208 | * place to install event listeners. |
||
209 | */ |
||
210 | protected function initialize() |
||
216 | |||
217 | /** |
||
218 | * Sets the driver for all models. |
||
219 | */ |
||
220 | public static function setDriver(DriverInterface $driver) |
||
224 | |||
225 | /** |
||
226 | * Gets the driver for all models. |
||
227 | * |
||
228 | * @throws DriverMissingException when a driver has not been set yet |
||
229 | */ |
||
230 | public static function getDriver(): DriverInterface |
||
238 | |||
239 | /** |
||
240 | * Clears the driver for all models. |
||
241 | */ |
||
242 | public static function clearDriver() |
||
246 | |||
247 | /** |
||
248 | * Gets the name of the model, i.e. User. |
||
249 | */ |
||
250 | public static function modelName(): string |
||
257 | |||
258 | /** |
||
259 | * Gets the model ID. |
||
260 | * |
||
261 | * @return string|number|false ID |
||
262 | */ |
||
263 | public function id() |
||
280 | |||
281 | /** |
||
282 | * Gets a key-value map of the model ID. |
||
283 | * |
||
284 | * @return array ID map |
||
285 | */ |
||
286 | public function ids(): array |
||
290 | |||
291 | /** |
||
292 | * Checks if the model has an identifier present. |
||
293 | * This does not indicate whether the model has been |
||
294 | * persisted to the database or loaded from the database. |
||
295 | */ |
||
296 | public function hasId(): bool |
||
300 | |||
301 | ///////////////////////////// |
||
302 | // Magic Methods |
||
303 | ///////////////////////////// |
||
304 | |||
305 | /** |
||
306 | * Converts the model into a string. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | public function __toString() |
||
317 | |||
318 | /** |
||
319 | * Shortcut to a get() call for a given property. |
||
320 | * |
||
321 | * @param string $name |
||
322 | * |
||
323 | * @return mixed |
||
324 | */ |
||
325 | public function __get($name) |
||
331 | |||
332 | /** |
||
333 | * Sets an unsaved value. |
||
334 | * |
||
335 | * @param string $name |
||
336 | * @param mixed $value |
||
337 | */ |
||
338 | public function __set($name, $value) |
||
367 | |||
368 | /** |
||
369 | * Checks if an unsaved value or property exists by this name. |
||
370 | * |
||
371 | * @param string $name |
||
372 | * |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function __isset($name) |
||
383 | |||
384 | /** |
||
385 | * Unsets an unsaved value. |
||
386 | * |
||
387 | * @param string $name |
||
388 | */ |
||
389 | public function __unset($name) |
||
400 | |||
401 | ///////////////////////////// |
||
402 | // ArrayAccess Interface |
||
403 | ///////////////////////////// |
||
404 | |||
405 | public function offsetExists($offset) |
||
409 | |||
410 | public function offsetGet($offset) |
||
414 | |||
415 | public function offsetSet($offset, $value) |
||
419 | |||
420 | public function offsetUnset($offset) |
||
424 | |||
425 | public static function __callStatic($name, $parameters) |
||
432 | |||
433 | ///////////////////////////// |
||
434 | // Property Definitions |
||
435 | ///////////////////////////// |
||
436 | |||
437 | /** |
||
438 | * Gets the model definition. |
||
439 | */ |
||
440 | public static function definition(): Definition |
||
444 | |||
445 | /** |
||
446 | * The buildDefinition() method is called once per model. It's used |
||
447 | * to generate the model definition. This is a great place to add any |
||
448 | * dynamic model properties. |
||
449 | */ |
||
450 | public static function buildDefinition(): Definition |
||
457 | |||
458 | /** |
||
459 | * Gets the names of the model ID properties. |
||
460 | */ |
||
461 | public static function getIDProperties(): array |
||
465 | |||
466 | /** |
||
467 | * Gets the mutator method name for a given property name. |
||
468 | * Looks for methods in the form of `setPropertyValue`. |
||
469 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
470 | * |
||
471 | * @param string $property property |
||
472 | * |
||
473 | * @return string|null method name if it exists |
||
474 | */ |
||
475 | public static function getMutator(string $property): ?string |
||
493 | |||
494 | /** |
||
495 | * Gets the accessor method name for a given property name. |
||
496 | * Looks for methods in the form of `getPropertyValue`. |
||
497 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
498 | * |
||
499 | * @param string $property property |
||
500 | * |
||
501 | * @return string|null method name if it exists |
||
502 | */ |
||
503 | public static function getAccessor(string $property): ?string |
||
521 | |||
522 | ///////////////////////////// |
||
523 | // CRUD Operations |
||
524 | ///////////////////////////// |
||
525 | |||
526 | /** |
||
527 | * Gets the table name for storing this model. |
||
528 | */ |
||
529 | public function getTablename(): string |
||
539 | |||
540 | /** |
||
541 | * Gets the ID of the connection in the connection manager |
||
542 | * that stores this model. |
||
543 | */ |
||
544 | public function getConnection(): ?string |
||
548 | |||
549 | protected function usesTransactions(): bool |
||
553 | |||
554 | /** |
||
555 | * Saves the model. |
||
556 | * |
||
557 | * @return bool true when the operation was successful |
||
558 | */ |
||
559 | public function save(): bool |
||
567 | |||
568 | /** |
||
569 | * Saves the model. Throws an exception when the operation fails. |
||
570 | * |
||
571 | * @throws ModelException when the model cannot be saved |
||
572 | */ |
||
573 | public function saveOrFail() |
||
584 | |||
585 | /** |
||
586 | * Creates a new model. |
||
587 | * |
||
588 | * @param array $data optional key-value properties to set |
||
589 | * |
||
590 | * @return bool true when the operation was successful |
||
591 | * |
||
592 | * @throws BadMethodCallException when called on an existing model |
||
593 | */ |
||
594 | public function create(array $data = []): bool |
||
718 | |||
719 | /** |
||
720 | * Ignores unsaved values when fetching the next value. |
||
721 | * |
||
722 | * @return $this |
||
723 | */ |
||
724 | public function ignoreUnsaved() |
||
730 | |||
731 | /** |
||
732 | * Fetches property values from the model. |
||
733 | * |
||
734 | * This method looks up values in this order: |
||
735 | * IDs, local cache, unsaved values, storage layer, defaults |
||
736 | * |
||
737 | * @param array $properties list of property names to fetch values of |
||
738 | */ |
||
739 | public function get(array $properties): array |
||
777 | |||
778 | /** |
||
779 | * Gets a property value from the model. |
||
780 | * |
||
781 | * Values are looked up in this order: |
||
782 | * 1. unsaved values |
||
783 | * 2. local values |
||
784 | * 3. default value |
||
785 | * 4. null |
||
786 | * |
||
787 | * @return mixed |
||
788 | */ |
||
789 | private function getValue(string $name, array $values) |
||
811 | |||
812 | /** |
||
813 | * Populates a newly created model with its ID. |
||
814 | */ |
||
815 | private function getNewId() |
||
836 | |||
837 | /** |
||
838 | * Sets a collection values on the model from an untrusted input. |
||
839 | * |
||
840 | * @param array $values |
||
841 | * |
||
842 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
843 | * |
||
844 | * @return $this |
||
845 | */ |
||
846 | public function setValues($values) |
||
866 | |||
867 | /** |
||
868 | * Converts the model to an array. |
||
869 | */ |
||
870 | public function toArray(): array |
||
913 | |||
914 | /** |
||
915 | * Checks if the unsaved value for a property is present and |
||
916 | * is different from the original value. |
||
917 | * |
||
918 | * @property string|null $name |
||
919 | * @property bool $hasChanged when true, checks if the unsaved value is different from the saved value |
||
920 | */ |
||
921 | public function dirty(?string $name = null, bool $hasChanged = false): bool |
||
941 | |||
942 | /** |
||
943 | * Updates the model. |
||
944 | * |
||
945 | * @param array $data optional key-value properties to set |
||
946 | * |
||
947 | * @return bool true when the operation was successful |
||
948 | * |
||
949 | * @throws BadMethodCallException when not called on an existing model |
||
950 | */ |
||
951 | public function set(array $data = []): bool |
||
1050 | |||
1051 | /** |
||
1052 | * Delete the model. |
||
1053 | * |
||
1054 | * @return bool true when the operation was successful |
||
1055 | */ |
||
1056 | public function delete(): bool |
||
1106 | |||
1107 | /** |
||
1108 | * Restores a soft-deleted model. |
||
1109 | */ |
||
1110 | public function restore(): bool |
||
1144 | |||
1145 | /** |
||
1146 | * Checks if the model has been deleted. |
||
1147 | */ |
||
1148 | public function isDeleted(): bool |
||
1156 | |||
1157 | ///////////////////////////// |
||
1158 | // Queries |
||
1159 | ///////////////////////////// |
||
1160 | |||
1161 | /** |
||
1162 | * Generates a new query instance. |
||
1163 | */ |
||
1164 | public static function query(): Query |
||
1179 | |||
1180 | /** |
||
1181 | * Generates a new query instance that includes soft-deleted models. |
||
1182 | */ |
||
1183 | public static function withDeleted(): Query |
||
1192 | |||
1193 | /** |
||
1194 | * Finds a single instance of a model given it's ID. |
||
1195 | * |
||
1196 | * @param mixed $id |
||
1197 | * |
||
1198 | * @return static|null |
||
1199 | */ |
||
1200 | public static function find($id): ?self |
||
1217 | |||
1218 | /** |
||
1219 | * Finds a single instance of a model given it's ID or throws an exception. |
||
1220 | * |
||
1221 | * @param mixed $id |
||
1222 | * |
||
1223 | * @return static |
||
1224 | * |
||
1225 | * @throws ModelNotFoundException when a model could not be found |
||
1226 | */ |
||
1227 | public static function findOrFail($id): self |
||
1236 | |||
1237 | /** |
||
1238 | * Tells if this model instance has been persisted to the data layer. |
||
1239 | * |
||
1240 | * NOTE: this does not actually perform a check with the data layer |
||
1241 | */ |
||
1242 | public function persisted(): bool |
||
1246 | |||
1247 | /** |
||
1248 | * Loads the model from the storage layer. |
||
1249 | * |
||
1250 | * @return $this |
||
1251 | */ |
||
1252 | public function refresh() |
||
1276 | |||
1277 | /** |
||
1278 | * Loads values into the model. |
||
1279 | * |
||
1280 | * @param array $values values |
||
1281 | * |
||
1282 | * @return $this |
||
1283 | */ |
||
1284 | public function refreshWith(array $values) |
||
1292 | |||
1293 | /** |
||
1294 | * Clears the cache for this model. |
||
1295 | * |
||
1296 | * @return $this |
||
1297 | */ |
||
1298 | public function clearCache() |
||
1307 | |||
1308 | ///////////////////////////// |
||
1309 | // Relationships |
||
1310 | ///////////////////////////// |
||
1311 | |||
1312 | /** |
||
1313 | * Gets the relationship manager for a property. |
||
1314 | * |
||
1315 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
1316 | */ |
||
1317 | private function getRelationship(Property $property): AbstractRelation |
||
1326 | |||
1327 | /** |
||
1328 | * Saves any unsaved models attached through a relationship. This will only |
||
1329 | * save attached models that have not been saved yet. |
||
1330 | */ |
||
1331 | private function saveRelationships(bool $usesTransactions): bool |
||
1365 | |||
1366 | /** |
||
1367 | * This hydrates an individual property in the model. It can be a |
||
1368 | * scalar value or relationship. |
||
1369 | * |
||
1370 | * @internal |
||
1371 | * |
||
1372 | * @param $value |
||
1373 | */ |
||
1374 | public function hydrateValue(string $name, $value): void |
||
1383 | |||
1384 | /** |
||
1385 | * @deprecated |
||
1386 | * |
||
1387 | * Gets the model(s) for a relationship |
||
1388 | * |
||
1389 | * @param string $k property |
||
1390 | * |
||
1391 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
1392 | * |
||
1393 | * @return Model|array|null |
||
1394 | */ |
||
1395 | public function relation(string $k) |
||
1404 | |||
1405 | /** |
||
1406 | * @deprecated |
||
1407 | * |
||
1408 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
1409 | * |
||
1410 | * @return $this |
||
1411 | */ |
||
1412 | public function setRelation(string $k, Model $model) |
||
1419 | |||
1420 | /** |
||
1421 | * @deprecated |
||
1422 | * |
||
1423 | * Sets the model for a one-to-many relationship |
||
1424 | * |
||
1425 | * @return $this |
||
1426 | */ |
||
1427 | public function setRelationCollection(string $k, iterable $models) |
||
1433 | |||
1434 | /** |
||
1435 | * @deprecated |
||
1436 | * |
||
1437 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null |
||
1438 | * |
||
1439 | * @return $this |
||
1440 | */ |
||
1441 | public function clearRelation(string $k) |
||
1448 | |||
1449 | ///////////////////////////// |
||
1450 | // Events |
||
1451 | ///////////////////////////// |
||
1452 | |||
1453 | /** |
||
1454 | * Gets the event dispatcher. |
||
1455 | */ |
||
1456 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
1465 | |||
1466 | /** |
||
1467 | * Subscribes to a listener to an event. |
||
1468 | * |
||
1469 | * @param string $event event name |
||
1470 | * @param int $priority optional priority, higher #s get called first |
||
1471 | */ |
||
1472 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
1476 | |||
1477 | /** |
||
1478 | * Adds a listener to the model.creating and model.updating events. |
||
1479 | */ |
||
1480 | public static function saving(callable $listener, int $priority = 0) |
||
1485 | |||
1486 | /** |
||
1487 | * Adds a listener to the model.created and model.updated events. |
||
1488 | */ |
||
1489 | public static function saved(callable $listener, int $priority = 0) |
||
1494 | |||
1495 | /** |
||
1496 | * Adds a listener to the model.creating, model.updating, and model.deleting events. |
||
1497 | */ |
||
1498 | public static function beforePersist(callable $listener, int $priority = 0) |
||
1504 | |||
1505 | /** |
||
1506 | * Adds a listener to the model.created, model.updated, and model.deleted events. |
||
1507 | */ |
||
1508 | public static function afterPersist(callable $listener, int $priority = 0) |
||
1514 | |||
1515 | /** |
||
1516 | * Adds a listener to the model.creating event. |
||
1517 | */ |
||
1518 | public static function creating(callable $listener, int $priority = 0) |
||
1522 | |||
1523 | /** |
||
1524 | * Adds a listener to the model.created event. |
||
1525 | */ |
||
1526 | public static function created(callable $listener, int $priority = 0) |
||
1530 | |||
1531 | /** |
||
1532 | * Adds a listener to the model.updating event. |
||
1533 | */ |
||
1534 | public static function updating(callable $listener, int $priority = 0) |
||
1538 | |||
1539 | /** |
||
1540 | * Adds a listener to the model.updated event. |
||
1541 | */ |
||
1542 | public static function updated(callable $listener, int $priority = 0) |
||
1546 | |||
1547 | /** |
||
1548 | * Adds a listener to the model.deleting event. |
||
1549 | */ |
||
1550 | public static function deleting(callable $listener, int $priority = 0) |
||
1554 | |||
1555 | /** |
||
1556 | * Adds a listener to the model.deleted event. |
||
1557 | */ |
||
1558 | public static function deleted(callable $listener, int $priority = 0) |
||
1562 | |||
1563 | /** |
||
1564 | * Dispatches the given event and checks if it was successful. |
||
1565 | * |
||
1566 | * @return bool true if the events were successfully propagated |
||
1567 | */ |
||
1568 | private function performDispatch(AbstractEvent $event, bool $usesTransactions): bool |
||
1583 | |||
1584 | ///////////////////////////// |
||
1585 | // Validation |
||
1586 | ///////////////////////////// |
||
1587 | |||
1588 | /** |
||
1589 | * Gets the error stack for this model. |
||
1590 | */ |
||
1591 | public function getErrors(): Errors |
||
1599 | |||
1600 | /** |
||
1601 | * Checks if the model in its current state is valid. |
||
1602 | */ |
||
1603 | public function valid(): bool |
||
1617 | } |
||
1618 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.