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 = []) |
||
162 | |||
163 | /** |
||
164 | * The initialize() method is called once per model. It's used |
||
165 | * to perform any one-off tasks before the model gets |
||
166 | * constructed. This is a great place to add any model |
||
167 | * properties. When extending this method be sure to call |
||
168 | * parent::initialize() as some important stuff happens here. |
||
169 | * If extending this method to add properties then you should |
||
170 | * call parent::initialize() after adding any properties. |
||
171 | */ |
||
172 | protected function initialize() |
||
186 | |||
187 | private function installAutoTimestamps() |
||
204 | |||
205 | /** |
||
206 | * Injects a DI container. |
||
207 | * |
||
208 | * @param \Pimple\Container $app |
||
209 | */ |
||
210 | public static function inject(Container $app) |
||
214 | |||
215 | /** |
||
216 | * Gets the DI container used for this model. |
||
217 | * |
||
218 | * @return \Pimple\Container |
||
219 | */ |
||
220 | public function getApp() |
||
224 | |||
225 | /** |
||
226 | * Sets the driver for all models. |
||
227 | * |
||
228 | * @param DriverInterface $driver |
||
229 | */ |
||
230 | public static function setDriver(DriverInterface $driver) |
||
234 | |||
235 | /** |
||
236 | * Gets the driver for all models. |
||
237 | * |
||
238 | * @return DriverInterface |
||
239 | * |
||
240 | * @throws DriverMissingException |
||
241 | */ |
||
242 | public static function getDriver() |
||
250 | |||
251 | /** |
||
252 | * Clears the driver for all models. |
||
253 | */ |
||
254 | public static function clearDriver() |
||
258 | |||
259 | /** |
||
260 | * Sets the locale instance for all models. |
||
261 | * |
||
262 | * @param Locale $locale |
||
263 | */ |
||
264 | public static function setLocale(Locale $locale) |
||
268 | |||
269 | /** |
||
270 | * Clears the locale for all models. |
||
271 | */ |
||
272 | public static function clearLocale() |
||
276 | |||
277 | /** |
||
278 | * Gets the name of the model without namespacing. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | public static function modelName() |
||
286 | |||
287 | /** |
||
288 | * Gets the model ID. |
||
289 | * |
||
290 | * @return string|number|null ID |
||
291 | */ |
||
292 | public function id() |
||
304 | |||
305 | /** |
||
306 | * Gets a key-value map of the model ID. |
||
307 | * |
||
308 | * @return array ID map |
||
309 | */ |
||
310 | public function ids() |
||
314 | |||
315 | ///////////////////////////// |
||
316 | // Magic Methods |
||
317 | ///////////////////////////// |
||
318 | |||
319 | public function __toString() |
||
323 | |||
324 | public function __get($name) |
||
328 | |||
329 | public function __set($name, $value) |
||
333 | |||
334 | public function __isset($name) |
||
338 | |||
339 | public function __unset($name) |
||
349 | |||
350 | public static function __callStatic($name, $parameters) |
||
357 | |||
358 | ///////////////////////////// |
||
359 | // ArrayAccess Interface |
||
360 | ///////////////////////////// |
||
361 | |||
362 | public function offsetExists($offset) |
||
366 | |||
367 | public function offsetGet($offset) |
||
371 | |||
372 | public function offsetSet($offset, $value) |
||
376 | |||
377 | public function offsetUnset($offset) |
||
381 | |||
382 | ///////////////////////////// |
||
383 | // Property Definitions |
||
384 | ///////////////////////////// |
||
385 | |||
386 | /** |
||
387 | * Gets the names of the model ID properties. |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | public static function getIdProperties() |
||
395 | |||
396 | /** |
||
397 | * Builds an existing model instance given a single ID value or |
||
398 | * ordered array of ID values. |
||
399 | * |
||
400 | * @param mixed $id |
||
401 | * |
||
402 | * @return Model |
||
403 | */ |
||
404 | public static function buildFromId($id) |
||
416 | |||
417 | /** |
||
418 | * Gets the mutator method name for a given proeprty name. |
||
419 | * Looks for methods in the form of `setPropertyValue`. |
||
420 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
421 | * |
||
422 | * @param string $property |
||
423 | * |
||
424 | * @return string|false method name if it exists |
||
425 | */ |
||
426 | View Code Duplication | public static function getMutator($property) |
|
444 | |||
445 | /** |
||
446 | * Gets the accessor method name for a given proeprty name. |
||
447 | * Looks for methods in the form of `getPropertyValue`. |
||
448 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
449 | * |
||
450 | * @param string $property |
||
451 | * |
||
452 | * @return string|false method name if it exists |
||
453 | */ |
||
454 | View Code Duplication | public static function getAccessor($property) |
|
472 | |||
473 | /** |
||
474 | * Checks if a given property is a relationship. |
||
475 | * |
||
476 | * @param string $property |
||
477 | * |
||
478 | * @return bool |
||
479 | */ |
||
480 | public static function isRelationship($property) |
||
484 | |||
485 | /** |
||
486 | * Gets the string date format for a property. Defaults to |
||
487 | * UNIX timestamps. |
||
488 | * |
||
489 | * @param string $property |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | public static function getDateFormat($property) |
||
501 | |||
502 | /** |
||
503 | * Gets the title of a property. |
||
504 | * |
||
505 | * @param string $name |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | public static function getPropertyTitle($name) |
||
521 | |||
522 | /** |
||
523 | * Gets the type cast for a property. |
||
524 | * |
||
525 | * @param string $property |
||
526 | * |
||
527 | * @return string|null |
||
528 | */ |
||
529 | public static function getPropertyType($property) |
||
535 | |||
536 | /** |
||
537 | * Casts a value to a given type. |
||
538 | * |
||
539 | * @param string|null $type |
||
540 | * @param mixed $value |
||
541 | * @param string $property optional property name |
||
542 | * |
||
543 | * @return mixed casted value |
||
544 | */ |
||
545 | public static function cast($type, $value, $property = null) |
||
594 | |||
595 | /** |
||
596 | * Gets the properties of this model. |
||
597 | * |
||
598 | * @return array |
||
599 | */ |
||
600 | public function getProperties() |
||
605 | |||
606 | /** |
||
607 | * Checks if the model has a property. |
||
608 | * |
||
609 | * @param string $property |
||
610 | * |
||
611 | * @return bool has property |
||
612 | */ |
||
613 | public function hasProperty($property) |
||
618 | |||
619 | ///////////////////////////// |
||
620 | // Values |
||
621 | ///////////////////////////// |
||
622 | |||
623 | /** |
||
624 | * Sets an unsaved value. |
||
625 | * |
||
626 | * @param string $name |
||
627 | * @param mixed $value |
||
628 | * @param bool $unsaved when true, sets an unsaved value |
||
629 | * |
||
630 | * @throws BadMethodCallException when setting a relationship |
||
631 | * |
||
632 | * @return self |
||
633 | */ |
||
634 | public function setValue($name, $value, $unsaved = true) |
||
659 | |||
660 | /** |
||
661 | * Sets a collection values on the model from an untrusted |
||
662 | * input. Also known as mass assignment. |
||
663 | * |
||
664 | * @param array $values |
||
665 | * |
||
666 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
667 | * |
||
668 | * @return self |
||
669 | */ |
||
670 | public function setValues($values) |
||
690 | |||
691 | /** |
||
692 | * Ignores unsaved values when fetching the next value. |
||
693 | * |
||
694 | * @return self |
||
695 | */ |
||
696 | public function ignoreUnsaved() |
||
702 | |||
703 | /** |
||
704 | * Gets property values from the model. |
||
705 | * |
||
706 | * This method looks up values from these locations in this |
||
707 | * precedence order (least important to most important): |
||
708 | * 1. local values |
||
709 | * 2. unsaved values |
||
710 | * |
||
711 | * @param array $properties list of property names to fetch values of |
||
712 | * |
||
713 | * @return array |
||
714 | * |
||
715 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
716 | */ |
||
717 | public function get(array $properties) |
||
760 | |||
761 | /** |
||
762 | * Converts the model to an array. |
||
763 | * |
||
764 | * @return array model array |
||
765 | */ |
||
766 | public function toArray() |
||
797 | |||
798 | ///////////////////////////// |
||
799 | // Persistence |
||
800 | ///////////////////////////// |
||
801 | |||
802 | /** |
||
803 | * Saves the model. |
||
804 | * |
||
805 | * @return bool |
||
806 | */ |
||
807 | public function save() |
||
815 | |||
816 | /** |
||
817 | * Creates a new model. |
||
818 | * |
||
819 | * @param array $data optional key-value properties to set |
||
820 | * |
||
821 | * @return bool |
||
822 | * |
||
823 | * @throws BadMethodCallException when called on an existing model |
||
824 | */ |
||
825 | public function create(array $data = []) |
||
864 | |||
865 | /** |
||
866 | * Gets the IDs for a newly created model. |
||
867 | * |
||
868 | * @return string |
||
869 | */ |
||
870 | protected function getNewIds() |
||
885 | |||
886 | /** |
||
887 | * Updates the model. |
||
888 | * |
||
889 | * @param array $data optional key-value properties to set |
||
890 | * |
||
891 | * @return bool |
||
892 | * |
||
893 | * @throws BadMethodCallException when not called on an existing model |
||
894 | */ |
||
895 | public function set(array $data = []) |
||
933 | |||
934 | /** |
||
935 | * Delete the model. |
||
936 | * |
||
937 | * @return bool success |
||
938 | */ |
||
939 | public function delete() |
||
965 | |||
966 | /** |
||
967 | * Tells if the model has been persisted. |
||
968 | * |
||
969 | * @return bool |
||
970 | */ |
||
971 | public function persisted() |
||
975 | |||
976 | /** |
||
977 | * Loads the model from the data layer. |
||
978 | * |
||
979 | * @return self |
||
980 | * |
||
981 | * @throws NotFoundException |
||
982 | */ |
||
983 | public function refresh() |
||
1000 | |||
1001 | /** |
||
1002 | * Loads values into the model retrieved from the data layer. |
||
1003 | * |
||
1004 | * @param array $values values |
||
1005 | * |
||
1006 | * @return self |
||
1007 | */ |
||
1008 | public function refreshWith(array $values) |
||
1025 | |||
1026 | ///////////////////////////// |
||
1027 | // Queries |
||
1028 | ///////////////////////////// |
||
1029 | |||
1030 | /** |
||
1031 | * Generates a new query instance. |
||
1032 | * |
||
1033 | * @return Query |
||
1034 | */ |
||
1035 | public static function query() |
||
1044 | |||
1045 | /** |
||
1046 | * Finds a single instance of a model given it's ID. |
||
1047 | * |
||
1048 | * @param mixed $id |
||
1049 | * |
||
1050 | * @return Model|null |
||
1051 | */ |
||
1052 | public static function find($id) |
||
1058 | |||
1059 | /** |
||
1060 | * Finds a single instance of a model given it's ID or throws an exception. |
||
1061 | * |
||
1062 | * @param mixed $id |
||
1063 | * |
||
1064 | * @return Model|false |
||
1065 | * |
||
1066 | * @throws NotFoundException when a model could not be found |
||
1067 | */ |
||
1068 | public static function findOrFail($id) |
||
1077 | |||
1078 | /** |
||
1079 | * Gets the toal number of records matching an optional criteria. |
||
1080 | * |
||
1081 | * @param array $where criteria |
||
1082 | * |
||
1083 | * @return int total |
||
1084 | */ |
||
1085 | public static function totalRecords(array $where = []) |
||
1092 | |||
1093 | ///////////////////////////// |
||
1094 | // Relationships |
||
1095 | ///////////////////////////// |
||
1096 | |||
1097 | /** |
||
1098 | * Creates the parent side of a One-To-One relationship. |
||
1099 | * |
||
1100 | * @param string $model foreign model class |
||
1101 | * @param string $foreignKey identifying key on foreign model |
||
1102 | * @param string $localKey identifying key on local model |
||
1103 | * |
||
1104 | * @return \Pulsar\Relation\Relation |
||
1105 | */ |
||
1106 | View Code Duplication | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
1121 | |||
1122 | /** |
||
1123 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
1124 | * |
||
1125 | * @param string $model foreign model class |
||
1126 | * @param string $foreignKey identifying key on foreign model |
||
1127 | * @param string $localKey identifying key on local model |
||
1128 | * |
||
1129 | * @return \Pulsar\Relation\Relation |
||
1130 | */ |
||
1131 | View Code Duplication | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
1146 | |||
1147 | /** |
||
1148 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
1149 | * |
||
1150 | * @param string $model foreign model class |
||
1151 | * @param string $foreignKey identifying key on foreign model |
||
1152 | * @param string $localKey identifying key on local model |
||
1153 | * |
||
1154 | * @return \Pulsar\Relation\Relation |
||
1155 | */ |
||
1156 | View Code Duplication | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
1171 | |||
1172 | /** |
||
1173 | * Creates the child side of a Many-To-Many relationship. |
||
1174 | * |
||
1175 | * @param string $model foreign model class |
||
1176 | * @param string $foreignKey identifying key on foreign model |
||
1177 | * @param string $localKey identifying key on local model |
||
1178 | * |
||
1179 | * @return \Pulsar\Relation\Relation |
||
1180 | */ |
||
1181 | View Code Duplication | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
1196 | |||
1197 | /** |
||
1198 | * Loads a given relationship (if not already) and returns |
||
1199 | * its results. |
||
1200 | * |
||
1201 | * @param string $name |
||
1202 | * |
||
1203 | * @return mixed |
||
1204 | */ |
||
1205 | protected function loadRelationship($name) |
||
1214 | |||
1215 | ///////////////////////////// |
||
1216 | // Events |
||
1217 | ///////////////////////////// |
||
1218 | |||
1219 | /** |
||
1220 | * Gets the event dispatcher. |
||
1221 | * |
||
1222 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
1223 | */ |
||
1224 | public static function getDispatcher($ignoreCache = false) |
||
1233 | |||
1234 | /** |
||
1235 | * Subscribes to a listener to an event. |
||
1236 | * |
||
1237 | * @param string $event event name |
||
1238 | * @param callable $listener |
||
1239 | * @param int $priority optional priority, higher #s get called first |
||
1240 | */ |
||
1241 | public static function listen($event, callable $listener, $priority = 0) |
||
1245 | |||
1246 | /** |
||
1247 | * Adds a listener to the model.creating event. |
||
1248 | * |
||
1249 | * @param callable $listener |
||
1250 | * @param int $priority |
||
1251 | */ |
||
1252 | public static function creating(callable $listener, $priority = 0) |
||
1256 | |||
1257 | /** |
||
1258 | * Adds a listener to the model.created event. |
||
1259 | * |
||
1260 | * @param callable $listener |
||
1261 | * @param int $priority |
||
1262 | */ |
||
1263 | public static function created(callable $listener, $priority = 0) |
||
1267 | |||
1268 | /** |
||
1269 | * Adds a listener to the model.updating event. |
||
1270 | * |
||
1271 | * @param callable $listener |
||
1272 | * @param int $priority |
||
1273 | */ |
||
1274 | public static function updating(callable $listener, $priority = 0) |
||
1278 | |||
1279 | /** |
||
1280 | * Adds a listener to the model.updated event. |
||
1281 | * |
||
1282 | * @param callable $listener |
||
1283 | * @param int $priority |
||
1284 | */ |
||
1285 | public static function updated(callable $listener, $priority = 0) |
||
1289 | |||
1290 | /** |
||
1291 | * Adds a listener to the model.deleting event. |
||
1292 | * |
||
1293 | * @param callable $listener |
||
1294 | * @param int $priority |
||
1295 | */ |
||
1296 | public static function deleting(callable $listener, $priority = 0) |
||
1300 | |||
1301 | /** |
||
1302 | * Adds a listener to the model.deleted event. |
||
1303 | * |
||
1304 | * @param callable $listener |
||
1305 | * @param int $priority |
||
1306 | */ |
||
1307 | public static function deleted(callable $listener, $priority = 0) |
||
1311 | |||
1312 | /** |
||
1313 | * Dispatches an event. |
||
1314 | * |
||
1315 | * @param string $eventName |
||
1316 | * |
||
1317 | * @return ModelEvent |
||
1318 | */ |
||
1319 | protected function dispatch($eventName) |
||
1325 | |||
1326 | ///////////////////////////// |
||
1327 | // Validation |
||
1328 | ///////////////////////////// |
||
1329 | |||
1330 | /** |
||
1331 | * Gets the error stack for this model instance. Used to |
||
1332 | * keep track of validation errors. |
||
1333 | * |
||
1334 | * @return Errors |
||
1335 | */ |
||
1336 | public function errors() |
||
1344 | |||
1345 | /** |
||
1346 | * Checks if the model is valid in its current state. |
||
1347 | * |
||
1348 | * @return bool |
||
1349 | */ |
||
1350 | public function valid() |
||
1367 | |||
1368 | /** |
||
1369 | * Gets a new validator instance for this model. |
||
1370 | * |
||
1371 | * @return Validator |
||
1372 | */ |
||
1373 | public function getValidator() |
||
1377 | } |
||
1378 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.