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 = false; |
||
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 = []) |
||
166 | { |
||
167 | // initialize the model |
||
168 | $this->init(); |
||
169 | |||
170 | $ids = []; |
||
171 | $this->hasId = true; |
||
172 | foreach (static::$ids as $name) { |
||
173 | $id = null; |
||
174 | if (array_key_exists($name, $values)) { |
||
175 | $idProperty = static::definition()->get($name); |
||
176 | $id = Type::cast($idProperty, $values[$name]); |
||
177 | } |
||
178 | |||
179 | $ids[$name] = $id; |
||
180 | $this->hasId = $this->hasId && $id; |
||
181 | } |
||
182 | |||
183 | $this->idValues = $ids; |
||
184 | |||
185 | // load any given values |
||
186 | if ($this->hasId && count($values) > count($ids)) { |
||
187 | $this->refreshWith($values); |
||
188 | } elseif (!$this->hasId) { |
||
189 | $this->_unsaved = $values; |
||
190 | } else { |
||
191 | $this->_values = $this->idValues; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Performs initialization on this model. |
||
197 | */ |
||
198 | private function init() |
||
199 | { |
||
200 | // ensure the initialize function is called only once |
||
201 | $k = static::class; |
||
202 | if (!isset(self::$initialized[$k])) { |
||
203 | $this->initialize(); |
||
204 | self::$initialized[$k] = true; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * The initialize() method is called once per model. This is a great |
||
210 | * place to install event listeners. Any methods on the model that have |
||
211 | * "autoInitialize" in the name will automatically be called. |
||
212 | */ |
||
213 | protected function initialize() |
||
214 | { |
||
215 | // Use reflection to automatically call any method here that has a name |
||
216 | // that starts with "autoInitialize". This is useful for traits to install listeners. |
||
217 | $methods = get_class_methods(static::class); |
||
218 | foreach ($methods as $method) { |
||
219 | if (0 === strpos($method, 'autoInitialize')) { |
||
220 | $this->$method(); |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Sets the driver for all models. |
||
227 | */ |
||
228 | public static function setDriver(DriverInterface $driver) |
||
229 | { |
||
230 | self::$driver = $driver; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Gets the driver for all models. |
||
235 | * |
||
236 | * @throws DriverMissingException when a driver has not been set yet |
||
237 | */ |
||
238 | public static function getDriver(): DriverInterface |
||
239 | { |
||
240 | if (!self::$driver) { |
||
241 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
242 | } |
||
243 | |||
244 | return self::$driver; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Clears the driver for all models. |
||
249 | */ |
||
250 | public static function clearDriver() |
||
251 | { |
||
252 | self::$driver = null; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Gets the name of the model, i.e. User. |
||
257 | */ |
||
258 | public static function modelName(): string |
||
259 | { |
||
260 | // strip namespacing |
||
261 | $paths = explode('\\', static::class); |
||
262 | |||
263 | return end($paths); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Gets the model ID. |
||
268 | * |
||
269 | * @return string|number|false ID |
||
270 | */ |
||
271 | public function id() |
||
272 | { |
||
273 | if (!$this->hasId) { |
||
274 | return false; |
||
275 | } |
||
276 | |||
277 | if (1 == count($this->idValues)) { |
||
278 | return reset($this->idValues); |
||
279 | } |
||
280 | |||
281 | $result = []; |
||
282 | foreach (static::$ids as $k) { |
||
283 | $result[] = $this->idValues[$k]; |
||
284 | } |
||
285 | |||
286 | return implode(',', $result); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Gets a key-value map of the model ID. |
||
291 | * |
||
292 | * @return array ID map |
||
293 | */ |
||
294 | public function ids(): array |
||
295 | { |
||
296 | return $this->idValues; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Checks if the model has an identifier present. |
||
301 | * This does not indicate whether the model has been |
||
302 | * persisted to the database or loaded from the database. |
||
303 | */ |
||
304 | public function hasId(): bool |
||
305 | { |
||
306 | return $this->hasId; |
||
307 | } |
||
308 | |||
309 | ///////////////////////////// |
||
310 | // Magic Methods |
||
311 | ///////////////////////////// |
||
312 | |||
313 | /** |
||
314 | * Converts the model into a string. |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | public function __toString() |
||
319 | { |
||
320 | $values = array_merge($this->_values, $this->_unsaved, $this->idValues); |
||
321 | ksort($values); |
||
322 | |||
323 | return static::class.'('.json_encode($values, JSON_PRETTY_PRINT).')'; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Shortcut to a get() call for a given property. |
||
328 | * |
||
329 | * @param string $name |
||
330 | * |
||
331 | * @return mixed |
||
332 | */ |
||
333 | public function __get($name) |
||
334 | { |
||
335 | $result = $this->get([$name]); |
||
336 | |||
337 | return reset($result); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Sets an unsaved value. |
||
342 | * |
||
343 | * @param string $name |
||
344 | * @param mixed $value |
||
345 | */ |
||
346 | public function __set($name, $value) |
||
347 | { |
||
348 | // if changing property, remove relation model |
||
349 | if (isset($this->_relationships[$name])) { |
||
350 | unset($this->_relationships[$name]); |
||
351 | } |
||
352 | |||
353 | // call any mutators |
||
354 | $mutator = self::getMutator($name); |
||
355 | if ($mutator) { |
||
356 | $this->_unsaved[$name] = $this->$mutator($value); |
||
357 | } else { |
||
358 | $this->_unsaved[$name] = $value; |
||
359 | } |
||
360 | |||
361 | // set local ID property on belongs_to relationship |
||
362 | if (static::definition()->has($name)) { |
||
363 | $property = static::definition()->get($name); |
||
364 | if (Relationship::BELONGS_TO == $property->getRelationshipType() && !$property->isPersisted()) { |
||
365 | if ($value instanceof self) { |
||
366 | $this->_unsaved[$property->getLocalKey()] = $value->{$property->getForeignKey()}; |
||
367 | } elseif (null === $value) { |
||
368 | $this->_unsaved[$property->getLocalKey()] = null; |
||
369 | } else { |
||
370 | throw new ModelException('The value set on the "'.$name.'" property must be a model or null.'); |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Checks if an unsaved value or property exists by this name. |
||
378 | * |
||
379 | * @param string $name |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | public function __isset($name) |
||
384 | { |
||
385 | // isset() must return true for any value that could be returned by offsetGet |
||
386 | // because many callers will first check isset() to see if the value is accessible. |
||
387 | // This method is not supposed to only be valid for unsaved values, or properties |
||
388 | // that have a value. |
||
389 | return array_key_exists($name, $this->_unsaved) || static::definition()->has($name); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Unsets an unsaved value. |
||
394 | * |
||
395 | * @param string $name |
||
396 | */ |
||
397 | public function __unset($name) |
||
398 | { |
||
399 | if (array_key_exists($name, $this->_unsaved)) { |
||
400 | // if changing property, remove relation model |
||
401 | if (isset($this->_relationships[$name])) { |
||
402 | unset($this->_relationships[$name]); |
||
403 | } |
||
404 | |||
405 | unset($this->_unsaved[$name]); |
||
406 | } |
||
407 | } |
||
408 | |||
409 | ///////////////////////////// |
||
410 | // ArrayAccess Interface |
||
411 | ///////////////////////////// |
||
412 | |||
413 | public function offsetExists($offset) |
||
414 | { |
||
415 | return isset($this->$offset); |
||
416 | } |
||
417 | |||
418 | public function offsetGet($offset) |
||
419 | { |
||
420 | return $this->$offset; |
||
421 | } |
||
422 | |||
423 | public function offsetSet($offset, $value) |
||
424 | { |
||
425 | $this->$offset = $value; |
||
426 | } |
||
427 | |||
428 | public function offsetUnset($offset) |
||
429 | { |
||
430 | unset($this->$offset); |
||
431 | } |
||
432 | |||
433 | public static function __callStatic($name, $parameters) |
||
434 | { |
||
435 | // Any calls to unkown static methods should be deferred to |
||
436 | // the query. This allows calls like User::where() |
||
437 | // to replace User::query()->where(). |
||
438 | return call_user_func_array([static::query(), $name], $parameters); |
||
439 | } |
||
440 | |||
441 | ///////////////////////////// |
||
442 | // Property Definitions |
||
443 | ///////////////////////////// |
||
444 | |||
445 | /** |
||
446 | * Gets the model definition. |
||
447 | */ |
||
448 | public static function definition(): Definition |
||
449 | { |
||
450 | return DefinitionBuilder::get(static::class); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * The buildDefinition() method is called once per model. It's used |
||
455 | * to generate the model definition. This is a great place to add any |
||
456 | * dynamic model properties. |
||
457 | */ |
||
458 | public static function buildDefinition(): Definition |
||
459 | { |
||
460 | // Use reflection to automatically call any method on the model that has a name |
||
461 | // that starts with "buildDefinition". This is useful for traits to add properties. |
||
462 | $methods = get_class_methods(static::class); |
||
463 | foreach ($methods as $method) { |
||
464 | if (0 === strpos($method, 'autoDefinition')) { |
||
465 | static::$method(); |
||
466 | } |
||
467 | } |
||
468 | |||
469 | return DefinitionBuilder::build(static::$properties, static::class); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Gets the names of the model ID properties. |
||
474 | */ |
||
475 | public static function getIDProperties(): array |
||
479 | |||
480 | /** |
||
481 | * Gets the mutator method name for a given property name. |
||
482 | * Looks for methods in the form of `setPropertyValue`. |
||
483 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
484 | * |
||
485 | * @param string $property property |
||
486 | * |
||
487 | * @return string|null method name if it exists |
||
488 | */ |
||
489 | public static function getMutator(string $property): ?string |
||
507 | |||
508 | /** |
||
509 | * Gets the accessor method name for a given property name. |
||
510 | * Looks for methods in the form of `getPropertyValue`. |
||
511 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
512 | * |
||
513 | * @param string $property property |
||
514 | * |
||
515 | * @return string|null method name if it exists |
||
516 | */ |
||
517 | public static function getAccessor(string $property): ?string |
||
535 | |||
536 | ///////////////////////////// |
||
537 | // CRUD Operations |
||
538 | ///////////////////////////// |
||
539 | |||
540 | /** |
||
541 | * Gets the table name for storing this model. |
||
542 | */ |
||
543 | public function getTablename(): string |
||
553 | |||
554 | /** |
||
555 | * Gets the ID of the connection in the connection manager |
||
556 | * that stores this model. |
||
557 | */ |
||
558 | public function getConnection(): ?string |
||
562 | |||
563 | protected function usesTransactions(): bool |
||
567 | |||
568 | /** |
||
569 | * Saves the model. |
||
570 | * |
||
571 | * @return bool true when the operation was successful |
||
572 | */ |
||
573 | public function save(): bool |
||
581 | |||
582 | /** |
||
583 | * Saves the model. Throws an exception when the operation fails. |
||
584 | * |
||
585 | * @throws ModelException when the model cannot be saved |
||
586 | */ |
||
587 | public function saveOrFail() |
||
598 | |||
599 | /** |
||
600 | * Creates a new model. |
||
601 | * |
||
602 | * @param array $data optional key-value properties to set |
||
603 | * |
||
604 | * @return bool true when the operation was successful |
||
605 | * |
||
606 | * @throws BadMethodCallException when called on an existing model |
||
607 | */ |
||
608 | public function create(array $data = []): bool |
||
732 | |||
733 | /** |
||
734 | * Ignores unsaved values when fetching the next value. |
||
735 | * |
||
736 | * @return $this |
||
737 | */ |
||
738 | public function ignoreUnsaved() |
||
744 | |||
745 | /** |
||
746 | * Fetches property values from the model. |
||
747 | * |
||
748 | * This method looks up values in this order: |
||
749 | * IDs, local cache, unsaved values, storage layer, defaults |
||
750 | * |
||
751 | * @param array $properties list of property names to fetch values of |
||
752 | */ |
||
753 | public function get(array $properties): array |
||
754 | { |
||
755 | // check if unsaved values will be returned |
||
756 | $ignoreUnsaved = $this->ignoreUnsaved; |
||
757 | $this->ignoreUnsaved = false; |
||
758 | |||
759 | // Check if the model needs to be loaded from the database. This |
||
760 | // is used if an ID was supplied for the model but the values have |
||
761 | // not been hydrated from the database. We only want to load values |
||
762 | // from the database if there are properties requested that are both |
||
763 | // persisted to the database AND do not already have a value present. |
||
764 | $this->loadIfNeeded($properties, $ignoreUnsaved); |
||
765 | |||
766 | // build a key-value map of the requested properties |
||
774 | |||
775 | /** |
||
776 | * Loads the model from the database if needed. |
||
777 | */ |
||
778 | private function loadIfNeeded(array $properties, bool $ignoreUnsaved): void |
||
795 | |||
796 | /** |
||
797 | * Gets a property value from the model. |
||
798 | * |
||
799 | * Values are looked up in this order: |
||
800 | * 1. unsaved values |
||
801 | * 2. local values |
||
802 | * 3. default value |
||
803 | * 4. null |
||
804 | * |
||
805 | * @return mixed |
||
806 | */ |
||
807 | private function getValue(string $name, bool $ignoreUnsaved) |
||
830 | |||
831 | /** |
||
832 | * Populates a newly created model with its ID. |
||
833 | */ |
||
834 | private function getNewId() |
||
856 | |||
857 | protected function getMassAssignmentWhitelist(): ?array |
||
866 | |||
867 | protected function getMassAssignmentBlacklist(): ?array |
||
876 | |||
877 | /** |
||
878 | * Sets a collection values on the model from an untrusted input. |
||
879 | * |
||
880 | * @param array $values |
||
881 | * |
||
882 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
883 | * |
||
884 | * @return $this |
||
885 | */ |
||
886 | public function setValues($values) |
||
917 | |||
918 | /** |
||
919 | * Converts the model to an array. |
||
920 | */ |
||
921 | public function toArray(): array |
||
968 | |||
969 | /** |
||
970 | * Checks if the unsaved value for a property is present and |
||
971 | * is different from the original value. |
||
972 | * |
||
973 | * @property string|null $name |
||
974 | * @property bool $hasChanged when true, checks if the unsaved value is different from the saved value |
||
975 | */ |
||
976 | public function dirty(?string $name = null, bool $hasChanged = false): bool |
||
996 | |||
997 | /** |
||
998 | * Updates the model. |
||
999 | * |
||
1000 | * @param array $data optional key-value properties to set |
||
1001 | * |
||
1002 | * @return bool true when the operation was successful |
||
1003 | * |
||
1004 | * @throws BadMethodCallException when not called on an existing model |
||
1005 | */ |
||
1006 | public function set(array $data = []): bool |
||
1105 | |||
1106 | /** |
||
1107 | * Delete the model. |
||
1108 | * |
||
1109 | * @return bool true when the operation was successful |
||
1110 | */ |
||
1111 | public function delete(): bool |
||
1161 | |||
1162 | /** |
||
1163 | * Restores a soft-deleted model. |
||
1164 | */ |
||
1165 | public function restore(): bool |
||
1199 | |||
1200 | /** |
||
1201 | * Checks if the model has been deleted. |
||
1202 | */ |
||
1203 | public function isDeleted(): bool |
||
1211 | |||
1212 | ///////////////////////////// |
||
1213 | // Queries |
||
1214 | ///////////////////////////// |
||
1215 | |||
1216 | /** |
||
1217 | * Generates a new query instance. |
||
1218 | */ |
||
1219 | public static function query(): Query |
||
1234 | |||
1235 | /** |
||
1236 | * Generates a new query instance that includes soft-deleted models. |
||
1237 | */ |
||
1238 | public static function withDeleted(): Query |
||
1247 | |||
1248 | /** |
||
1249 | * Finds a single instance of a model given it's ID. |
||
1250 | * |
||
1251 | * @param mixed $id |
||
1252 | * |
||
1253 | * @return static|null |
||
1254 | */ |
||
1255 | public static function find($id): ?self |
||
1272 | |||
1273 | /** |
||
1274 | * Finds a single instance of a model given it's ID or throws an exception. |
||
1275 | * |
||
1276 | * @param mixed $id |
||
1277 | * |
||
1278 | * @return static |
||
1279 | * |
||
1280 | * @throws ModelNotFoundException when a model could not be found |
||
1281 | */ |
||
1282 | public static function findOrFail($id): self |
||
1291 | |||
1292 | /** |
||
1293 | * Tells if this model instance has been persisted to the data layer. |
||
1294 | * |
||
1295 | * NOTE: this does not actually perform a check with the data layer |
||
1296 | */ |
||
1297 | public function persisted(): bool |
||
1301 | |||
1302 | /** |
||
1303 | * Loads the model from the storage layer. |
||
1304 | * |
||
1305 | * @return $this |
||
1306 | */ |
||
1307 | public function refresh() |
||
1331 | |||
1332 | /** |
||
1333 | * Loads values into the model. |
||
1334 | * |
||
1335 | * @param array $values values |
||
1336 | * |
||
1337 | * @return $this |
||
1338 | */ |
||
1339 | public function refreshWith(array $values) |
||
1347 | |||
1348 | /** |
||
1349 | * Clears the cache for this model. |
||
1350 | * |
||
1351 | * @return $this |
||
1352 | */ |
||
1353 | public function clearCache() |
||
1362 | |||
1363 | ///////////////////////////// |
||
1364 | // Relationships |
||
1365 | ///////////////////////////// |
||
1366 | |||
1367 | /** |
||
1368 | * Gets the relationship manager for a property. |
||
1369 | * |
||
1370 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
1371 | */ |
||
1372 | private function getRelationship(Property $property): AbstractRelation |
||
1381 | |||
1382 | /** |
||
1383 | * Saves any unsaved models attached through a relationship. This will only |
||
1384 | * save attached models that have not been saved yet. |
||
1385 | */ |
||
1386 | private function saveRelationships(bool $usesTransactions): bool |
||
1420 | |||
1421 | /** |
||
1422 | * This hydrates an individual property in the model. It can be a |
||
1423 | * scalar value or relationship. |
||
1424 | * |
||
1425 | * @internal |
||
1426 | * |
||
1427 | * @param $value |
||
1428 | */ |
||
1429 | public function hydrateValue(string $name, $value): void |
||
1438 | |||
1439 | /** |
||
1440 | * @deprecated |
||
1441 | * |
||
1442 | * Gets the model(s) for a relationship |
||
1443 | * |
||
1444 | * @param string $k property |
||
1445 | * |
||
1446 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
1447 | * |
||
1448 | * @return Model|array|null |
||
1449 | */ |
||
1450 | public function relation(string $k) |
||
1459 | |||
1460 | /** |
||
1461 | * @deprecated |
||
1462 | * |
||
1463 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
1464 | * |
||
1465 | * @return $this |
||
1466 | */ |
||
1467 | public function setRelation(string $k, Model $model) |
||
1474 | |||
1475 | /** |
||
1476 | * @deprecated |
||
1477 | * |
||
1478 | * Sets the model for a one-to-many relationship |
||
1479 | * |
||
1480 | * @return $this |
||
1481 | */ |
||
1482 | public function setRelationCollection(string $k, iterable $models) |
||
1488 | |||
1489 | /** |
||
1490 | * @deprecated |
||
1491 | * |
||
1492 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null |
||
1493 | * |
||
1494 | * @return $this |
||
1495 | */ |
||
1496 | public function clearRelation(string $k) |
||
1503 | |||
1504 | ///////////////////////////// |
||
1505 | // Events |
||
1506 | ///////////////////////////// |
||
1507 | |||
1508 | /** |
||
1509 | * Gets the event dispatcher. |
||
1510 | */ |
||
1511 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
1520 | |||
1521 | /** |
||
1522 | * Subscribes to a listener to an event. |
||
1523 | * |
||
1524 | * @param string $event event name |
||
1525 | * @param int $priority optional priority, higher #s get called first |
||
1526 | */ |
||
1527 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
1531 | |||
1532 | /** |
||
1533 | * Adds a listener to the model.creating and model.updating events. |
||
1534 | */ |
||
1535 | public static function saving(callable $listener, int $priority = 0) |
||
1540 | |||
1541 | /** |
||
1542 | * Adds a listener to the model.created and model.updated events. |
||
1543 | */ |
||
1544 | public static function saved(callable $listener, int $priority = 0) |
||
1549 | |||
1550 | /** |
||
1551 | * Adds a listener to the model.creating, model.updating, and model.deleting events. |
||
1552 | */ |
||
1553 | public static function beforePersist(callable $listener, int $priority = 0) |
||
1559 | |||
1560 | /** |
||
1561 | * Adds a listener to the model.created, model.updated, and model.deleted events. |
||
1562 | */ |
||
1563 | public static function afterPersist(callable $listener, int $priority = 0) |
||
1569 | |||
1570 | /** |
||
1571 | * Adds a listener to the model.creating event. |
||
1572 | */ |
||
1573 | public static function creating(callable $listener, int $priority = 0) |
||
1577 | |||
1578 | /** |
||
1579 | * Adds a listener to the model.created event. |
||
1580 | */ |
||
1581 | public static function created(callable $listener, int $priority = 0) |
||
1585 | |||
1586 | /** |
||
1587 | * Adds a listener to the model.updating event. |
||
1588 | */ |
||
1589 | public static function updating(callable $listener, int $priority = 0) |
||
1593 | |||
1594 | /** |
||
1595 | * Adds a listener to the model.updated event. |
||
1596 | */ |
||
1597 | public static function updated(callable $listener, int $priority = 0) |
||
1601 | |||
1602 | /** |
||
1603 | * Adds a listener to the model.deleting event. |
||
1604 | */ |
||
1605 | public static function deleting(callable $listener, int $priority = 0) |
||
1609 | |||
1610 | /** |
||
1611 | * Adds a listener to the model.deleted event. |
||
1612 | */ |
||
1613 | public static function deleted(callable $listener, int $priority = 0) |
||
1617 | |||
1618 | /** |
||
1619 | * Dispatches the given event and checks if it was successful. |
||
1620 | * |
||
1621 | * @return bool true if the events were successfully propagated |
||
1622 | */ |
||
1623 | private function performDispatch(AbstractEvent $event, bool $usesTransactions): bool |
||
1638 | |||
1639 | ///////////////////////////// |
||
1640 | // Validation |
||
1641 | ///////////////////////////// |
||
1642 | |||
1643 | /** |
||
1644 | * Gets the error stack for this model. |
||
1645 | */ |
||
1646 | public function getErrors(): Errors |
||
1654 | |||
1655 | /** |
||
1656 | * Checks if the model in its current state is valid. |
||
1657 | */ |
||
1658 | public function valid(): bool |
||
1672 | } |
||
1673 |
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.