Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
29 | abstract class Model implements \ArrayAccess |
||
30 | { |
||
31 | const TYPE_STRING = 'string'; |
||
32 | const TYPE_INTEGER = 'integer'; |
||
33 | const TYPE_FLOAT = 'float'; |
||
34 | const TYPE_BOOLEAN = 'boolean'; |
||
35 | const TYPE_DATE = 'date'; |
||
36 | const TYPE_OBJECT = 'object'; |
||
37 | const TYPE_ARRAY = 'array'; |
||
38 | |||
39 | const DEFAULT_ID_PROPERTY = 'id'; |
||
40 | |||
41 | const DEFAULT_DATE_FORMAT = 'U'; // unix timestamps |
||
42 | |||
43 | ///////////////////////////// |
||
44 | // Model visible variables |
||
45 | ///////////////////////////// |
||
46 | |||
47 | /** |
||
48 | * List of model ID property names. |
||
49 | * |
||
50 | * @staticvar array |
||
51 | */ |
||
52 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
53 | |||
54 | /** |
||
55 | * Validation rules expressed as a key-value map with |
||
56 | * property names as the keys. |
||
57 | * i.e. ['name' => 'string:2']. |
||
58 | * |
||
59 | * @staticvar array |
||
60 | */ |
||
61 | protected static $validations = []; |
||
62 | |||
63 | /** |
||
64 | * @staticvar array |
||
65 | */ |
||
66 | protected static $relationships = []; |
||
67 | |||
68 | /** |
||
69 | * @staticvar array |
||
70 | */ |
||
71 | protected static $dates = []; |
||
72 | |||
73 | /** |
||
74 | * @staticvar \Pimple\Container |
||
75 | */ |
||
76 | protected static $injectedApp; |
||
77 | |||
78 | /** |
||
79 | * @staticvar array |
||
80 | */ |
||
81 | protected static $dispatchers; |
||
82 | |||
83 | /** |
||
84 | * @var \Pimple\Container |
||
85 | */ |
||
86 | protected $app; |
||
87 | |||
88 | /** |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $_values = []; |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $_unsaved = []; |
||
97 | |||
98 | /** |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected $_persisted = false; |
||
102 | |||
103 | /** |
||
104 | * @var Errors |
||
105 | */ |
||
106 | protected $_errors; |
||
107 | |||
108 | ///////////////////////////// |
||
109 | // Base model variables |
||
110 | ///////////////////////////// |
||
111 | |||
112 | /** |
||
113 | * @staticvar array |
||
114 | */ |
||
115 | private static $initialized = []; |
||
116 | |||
117 | /** |
||
118 | * @staticvar DriverInterface |
||
119 | */ |
||
120 | private static $driver; |
||
121 | |||
122 | /** |
||
123 | * @staticvar Locale |
||
124 | */ |
||
125 | private static $locale; |
||
126 | |||
127 | /** |
||
128 | * @staticvar array |
||
129 | */ |
||
130 | private static $accessors = []; |
||
131 | |||
132 | /** |
||
133 | * @staticvar array |
||
134 | */ |
||
135 | private static $mutators = []; |
||
136 | |||
137 | /** |
||
138 | * @var bool |
||
139 | */ |
||
140 | private $_ignoreUnsaved; |
||
141 | |||
142 | /** |
||
143 | * Creates a new model object. |
||
144 | * |
||
145 | * @param array $values values to fill model with |
||
146 | */ |
||
147 | public function __construct(array $values = []) |
||
159 | |||
160 | /** |
||
161 | * The initialize() method is called once per model. It's used |
||
162 | * to perform any one-off tasks before the model gets |
||
163 | * constructed. This is a great place to add any model |
||
164 | * properties. When extending this method be sure to call |
||
165 | * parent::initialize() as some important stuff happens here. |
||
166 | * If extending this method to add properties then you should |
||
167 | * call parent::initialize() after adding any properties. |
||
168 | */ |
||
169 | protected function initialize() |
||
183 | |||
184 | private function installAutoTimestamps() |
||
201 | |||
202 | /** |
||
203 | * Injects a DI container. |
||
204 | * |
||
205 | * @param \Pimple\Container $app |
||
206 | */ |
||
207 | public static function inject(Container $app) |
||
211 | |||
212 | /** |
||
213 | * Gets the DI container used for this model. |
||
214 | * |
||
215 | * @return \Pimple\Container |
||
216 | */ |
||
217 | public function getApp() |
||
221 | |||
222 | /** |
||
223 | * Sets the driver for all models. |
||
224 | * |
||
225 | * @param DriverInterface $driver |
||
226 | */ |
||
227 | public static function setDriver(DriverInterface $driver) |
||
231 | |||
232 | /** |
||
233 | * Gets the driver for all models. |
||
234 | * |
||
235 | * @return DriverInterface |
||
236 | * |
||
237 | * @throws DriverMissingException |
||
238 | */ |
||
239 | public static function getDriver() |
||
247 | |||
248 | /** |
||
249 | * Clears the driver for all models. |
||
250 | */ |
||
251 | public static function clearDriver() |
||
255 | |||
256 | /** |
||
257 | * Sets the locale instance for all models. |
||
258 | * |
||
259 | * @param Locale $locale |
||
260 | */ |
||
261 | public static function setLocale(Locale $locale) |
||
265 | |||
266 | /** |
||
267 | * Clears the locale for all models. |
||
268 | */ |
||
269 | public static function clearLocale() |
||
273 | |||
274 | /** |
||
275 | * Gets the name of the model without namespacing. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public static function modelName() |
||
283 | |||
284 | /** |
||
285 | * Gets the model ID. |
||
286 | * |
||
287 | * @return string|number|null ID |
||
288 | */ |
||
289 | public function id() |
||
301 | |||
302 | /** |
||
303 | * Gets a key-value map of the model ID. |
||
304 | * |
||
305 | * @return array ID map |
||
306 | */ |
||
307 | public function ids() |
||
311 | |||
312 | ///////////////////////////// |
||
313 | // Magic Methods |
||
314 | ///////////////////////////// |
||
315 | |||
316 | public function __toString() |
||
320 | |||
321 | public function __get($name) |
||
325 | |||
326 | public function __set($name, $value) |
||
330 | |||
331 | public function __isset($name) |
||
335 | |||
336 | public function __unset($name) |
||
346 | |||
347 | public static function __callStatic($name, $parameters) |
||
354 | |||
355 | ///////////////////////////// |
||
356 | // ArrayAccess Interface |
||
357 | ///////////////////////////// |
||
358 | |||
359 | public function offsetExists($offset) |
||
363 | |||
364 | public function offsetGet($offset) |
||
368 | |||
369 | public function offsetSet($offset, $value) |
||
373 | |||
374 | public function offsetUnset($offset) |
||
378 | |||
379 | ///////////////////////////// |
||
380 | // Property Definitions |
||
381 | ///////////////////////////// |
||
382 | |||
383 | /** |
||
384 | * Gets the names of the model ID properties. |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | public static function getIdProperties() |
||
392 | |||
393 | /** |
||
394 | * Builds an existing model instance given a single ID value or |
||
395 | * ordered array of ID values. |
||
396 | * |
||
397 | * @param mixed $id |
||
398 | * |
||
399 | * @return Model |
||
400 | */ |
||
401 | public static function buildFromId($id) |
||
413 | |||
414 | /** |
||
415 | * Gets the mutator method name for a given proeprty name. |
||
416 | * Looks for methods in the form of `setPropertyValue`. |
||
417 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
418 | * |
||
419 | * @param string $property |
||
420 | * |
||
421 | * @return string|false method name if it exists |
||
422 | */ |
||
423 | View Code Duplication | public static function getMutator($property) |
|
441 | |||
442 | /** |
||
443 | * Gets the accessor method name for a given proeprty name. |
||
444 | * Looks for methods in the form of `getPropertyValue`. |
||
445 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
446 | * |
||
447 | * @param string $property |
||
448 | * |
||
449 | * @return string|false method name if it exists |
||
450 | */ |
||
451 | View Code Duplication | public static function getAccessor($property) |
|
469 | |||
470 | /** |
||
471 | * Checks if a given property is a relationship. |
||
472 | * |
||
473 | * @param string $property |
||
474 | * |
||
475 | * @return bool |
||
476 | */ |
||
477 | public static function isRelationship($property) |
||
481 | |||
482 | /** |
||
483 | * Gets the string date format for a property. Defaults to |
||
484 | * UNIX timestamps. |
||
485 | * |
||
486 | * @param string $property |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | public static function getDateFormat($property) |
||
498 | |||
499 | /** |
||
500 | * Gets the title of a property. |
||
501 | * |
||
502 | * @param string $name |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | public static function getPropertyTitle($name) |
||
518 | |||
519 | /** |
||
520 | * Gets the type cast for a property. |
||
521 | * |
||
522 | * @param string $property |
||
523 | * |
||
524 | * @return string|null |
||
525 | */ |
||
526 | public static function getPropertyType($property) |
||
532 | |||
533 | /** |
||
534 | * Casts a value to a given type. |
||
535 | * |
||
536 | * @param string|null $type |
||
537 | * @param mixed $value |
||
538 | * @param string $property optional property name |
||
539 | * |
||
540 | * @return mixed casted value |
||
541 | */ |
||
542 | public static function cast($type, $value, $property = null) |
||
591 | |||
592 | /** |
||
593 | * Gets the properties of this model. |
||
594 | * |
||
595 | * @return array |
||
596 | */ |
||
597 | public function getProperties() |
||
602 | |||
603 | /** |
||
604 | * Checks if the model has a property. |
||
605 | * |
||
606 | * @param string $property |
||
607 | * |
||
608 | * @return bool has property |
||
609 | */ |
||
610 | public function hasProperty($property) |
||
615 | |||
616 | ///////////////////////////// |
||
617 | // Values |
||
618 | ///////////////////////////// |
||
619 | |||
620 | /** |
||
621 | * Sets an unsaved value. |
||
622 | * |
||
623 | * @param string $name |
||
624 | * @param mixed $value |
||
625 | * |
||
626 | * @throws BadMethodCallException when setting a relationship |
||
627 | * |
||
628 | * @return self |
||
629 | */ |
||
630 | public function setValue($name, $value) |
||
650 | |||
651 | /** |
||
652 | * Sets a collection values on the model from an untrusted input. |
||
653 | * |
||
654 | * @param array $values |
||
655 | * |
||
656 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
657 | * |
||
658 | * @return self |
||
659 | */ |
||
660 | public function setValues($values) |
||
680 | |||
681 | /** |
||
682 | * Ignores unsaved values when fetching the next value. |
||
683 | * |
||
684 | * @return self |
||
685 | */ |
||
686 | public function ignoreUnsaved() |
||
692 | |||
693 | /** |
||
694 | * Gets property values from the model. |
||
695 | * |
||
696 | * This method looks up values from these locations in this |
||
697 | * precedence order (least important to most important): |
||
698 | * 1. local values |
||
699 | * 2. unsaved values |
||
700 | * |
||
701 | * @param array $properties list of property names to fetch values of |
||
702 | * |
||
703 | * @return array |
||
704 | * |
||
705 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
706 | */ |
||
707 | public function get(array $properties) |
||
750 | |||
751 | /** |
||
752 | * Converts the model to an array. |
||
753 | * |
||
754 | * @return array model array |
||
755 | */ |
||
756 | public function toArray() |
||
783 | |||
784 | ///////////////////////////// |
||
785 | // Persistence |
||
786 | ///////////////////////////// |
||
787 | |||
788 | /** |
||
789 | * Saves the model. |
||
790 | * |
||
791 | * @return bool |
||
792 | */ |
||
793 | public function save() |
||
801 | |||
802 | /** |
||
803 | * Creates a new model. |
||
804 | * |
||
805 | * @param array $data optional key-value properties to set |
||
806 | * |
||
807 | * @return bool |
||
808 | * |
||
809 | * @throws BadMethodCallException when called on an existing model |
||
810 | */ |
||
811 | public function create(array $data = []) |
||
850 | |||
851 | /** |
||
852 | * Gets the IDs for a newly created model. |
||
853 | * |
||
854 | * @return string |
||
855 | */ |
||
856 | protected function getNewIds() |
||
871 | |||
872 | /** |
||
873 | * Updates the model. |
||
874 | * |
||
875 | * @param array $data optional key-value properties to set |
||
876 | * |
||
877 | * @return bool |
||
878 | * |
||
879 | * @throws BadMethodCallException when not called on an existing model |
||
880 | */ |
||
881 | public function set(array $data = []) |
||
919 | |||
920 | /** |
||
921 | * Delete the model. |
||
922 | * |
||
923 | * @return bool success |
||
924 | */ |
||
925 | public function delete() |
||
951 | |||
952 | /** |
||
953 | * Tells if the model has been persisted. |
||
954 | * |
||
955 | * @return bool |
||
956 | */ |
||
957 | public function persisted() |
||
961 | |||
962 | /** |
||
963 | * Loads the model from the data layer. |
||
964 | * |
||
965 | * @return self |
||
966 | * |
||
967 | * @throws NotFoundException |
||
968 | */ |
||
969 | public function refresh() |
||
986 | |||
987 | /** |
||
988 | * Loads values into the model retrieved from the data layer. |
||
989 | * |
||
990 | * @param array $values values |
||
991 | * |
||
992 | * @return self |
||
993 | */ |
||
994 | public function refreshWith(array $values) |
||
1011 | |||
1012 | ///////////////////////////// |
||
1013 | // Queries |
||
1014 | ///////////////////////////// |
||
1015 | |||
1016 | /** |
||
1017 | * Generates a new query instance. |
||
1018 | * |
||
1019 | * @return Query |
||
1020 | */ |
||
1021 | public static function query() |
||
1030 | |||
1031 | /** |
||
1032 | * Finds a single instance of a model given it's ID. |
||
1033 | * |
||
1034 | * @param mixed $id |
||
1035 | * |
||
1036 | * @return Model|null |
||
1037 | */ |
||
1038 | public static function find($id) |
||
1044 | |||
1045 | /** |
||
1046 | * Finds a single instance of a model given it's ID or throws an exception. |
||
1047 | * |
||
1048 | * @param mixed $id |
||
1049 | * |
||
1050 | * @return Model|false |
||
1051 | * |
||
1052 | * @throws NotFoundException when a model could not be found |
||
1053 | */ |
||
1054 | public static function findOrFail($id) |
||
1063 | |||
1064 | /** |
||
1065 | * Gets the toal number of records matching an optional criteria. |
||
1066 | * |
||
1067 | * @param array $where criteria |
||
1068 | * |
||
1069 | * @return int total |
||
1070 | */ |
||
1071 | public static function totalRecords(array $where = []) |
||
1078 | |||
1079 | ///////////////////////////// |
||
1080 | // Relationships |
||
1081 | ///////////////////////////// |
||
1082 | |||
1083 | /** |
||
1084 | * Creates the parent side of a One-To-One relationship. |
||
1085 | * |
||
1086 | * @param string $model foreign model class |
||
1087 | * @param string $foreignKey identifying key on foreign model |
||
1088 | * @param string $localKey identifying key on local model |
||
1089 | * |
||
1090 | * @return \Pulsar\Relation\Relation |
||
1091 | */ |
||
1092 | View Code Duplication | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
1107 | |||
1108 | /** |
||
1109 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
1110 | * |
||
1111 | * @param string $model foreign model class |
||
1112 | * @param string $foreignKey identifying key on foreign model |
||
1113 | * @param string $localKey identifying key on local model |
||
1114 | * |
||
1115 | * @return \Pulsar\Relation\Relation |
||
1116 | */ |
||
1117 | View Code Duplication | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
1132 | |||
1133 | /** |
||
1134 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
1135 | * |
||
1136 | * @param string $model foreign model class |
||
1137 | * @param string $foreignKey identifying key on foreign model |
||
1138 | * @param string $localKey identifying key on local model |
||
1139 | * |
||
1140 | * @return \Pulsar\Relation\Relation |
||
1141 | */ |
||
1142 | View Code Duplication | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
1157 | |||
1158 | /** |
||
1159 | * Creates the child side of a Many-To-Many relationship. |
||
1160 | * |
||
1161 | * @param string $model foreign model class |
||
1162 | * @param string $foreignKey identifying key on foreign model |
||
1163 | * @param string $localKey identifying key on local model |
||
1164 | * |
||
1165 | * @return \Pulsar\Relation\Relation |
||
1166 | */ |
||
1167 | View Code Duplication | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
1182 | |||
1183 | /** |
||
1184 | * Loads a given relationship (if not already) and returns |
||
1185 | * its results. |
||
1186 | * |
||
1187 | * @param string $name |
||
1188 | * |
||
1189 | * @return mixed |
||
1190 | */ |
||
1191 | protected function loadRelationship($name) |
||
1200 | |||
1201 | ///////////////////////////// |
||
1202 | // Events |
||
1203 | ///////////////////////////// |
||
1204 | |||
1205 | /** |
||
1206 | * Gets the event dispatcher. |
||
1207 | * |
||
1208 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
1209 | */ |
||
1210 | public static function getDispatcher($ignoreCache = false) |
||
1219 | |||
1220 | /** |
||
1221 | * Subscribes to a listener to an event. |
||
1222 | * |
||
1223 | * @param string $event event name |
||
1224 | * @param callable $listener |
||
1225 | * @param int $priority optional priority, higher #s get called first |
||
1226 | */ |
||
1227 | public static function listen($event, callable $listener, $priority = 0) |
||
1231 | |||
1232 | /** |
||
1233 | * Adds a listener to the model.creating event. |
||
1234 | * |
||
1235 | * @param callable $listener |
||
1236 | * @param int $priority |
||
1237 | */ |
||
1238 | public static function creating(callable $listener, $priority = 0) |
||
1242 | |||
1243 | /** |
||
1244 | * Adds a listener to the model.created event. |
||
1245 | * |
||
1246 | * @param callable $listener |
||
1247 | * @param int $priority |
||
1248 | */ |
||
1249 | public static function created(callable $listener, $priority = 0) |
||
1253 | |||
1254 | /** |
||
1255 | * Adds a listener to the model.updating event. |
||
1256 | * |
||
1257 | * @param callable $listener |
||
1258 | * @param int $priority |
||
1259 | */ |
||
1260 | public static function updating(callable $listener, $priority = 0) |
||
1264 | |||
1265 | /** |
||
1266 | * Adds a listener to the model.updated event. |
||
1267 | * |
||
1268 | * @param callable $listener |
||
1269 | * @param int $priority |
||
1270 | */ |
||
1271 | public static function updated(callable $listener, $priority = 0) |
||
1275 | |||
1276 | /** |
||
1277 | * Adds a listener to the model.deleting event. |
||
1278 | * |
||
1279 | * @param callable $listener |
||
1280 | * @param int $priority |
||
1281 | */ |
||
1282 | public static function deleting(callable $listener, $priority = 0) |
||
1286 | |||
1287 | /** |
||
1288 | * Adds a listener to the model.deleted event. |
||
1289 | * |
||
1290 | * @param callable $listener |
||
1291 | * @param int $priority |
||
1292 | */ |
||
1293 | public static function deleted(callable $listener, $priority = 0) |
||
1297 | |||
1298 | /** |
||
1299 | * Dispatches an event. |
||
1300 | * |
||
1301 | * @param string $eventName |
||
1302 | * |
||
1303 | * @return ModelEvent |
||
1304 | */ |
||
1305 | protected function dispatch($eventName) |
||
1311 | |||
1312 | ///////////////////////////// |
||
1313 | // Validation |
||
1314 | ///////////////////////////// |
||
1315 | |||
1316 | /** |
||
1317 | * Gets the error stack for this model instance. Used to |
||
1318 | * keep track of validation errors. |
||
1319 | * |
||
1320 | * @return Errors |
||
1321 | */ |
||
1322 | public function errors() |
||
1330 | |||
1331 | /** |
||
1332 | * Checks if the model is valid in its current state. |
||
1333 | * |
||
1334 | * @return bool |
||
1335 | */ |
||
1336 | public function valid() |
||
1353 | |||
1354 | /** |
||
1355 | * Gets a new validator instance for this model. |
||
1356 | * |
||
1357 | * @return Validator |
||
1358 | */ |
||
1359 | public function getValidator() |
||
1363 | } |
||
1364 |
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@property
annotation 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.