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 |
||
| 25 | abstract class Model implements \ArrayAccess |
||
| 26 | { |
||
| 27 | const IMMUTABLE = 0; |
||
| 28 | const MUTABLE_CREATE_ONLY = 1; |
||
| 29 | const MUTABLE = 2; |
||
| 30 | |||
| 31 | const TYPE_STRING = 'string'; |
||
| 32 | const TYPE_NUMBER = 'number'; |
||
| 33 | const TYPE_BOOLEAN = 'boolean'; |
||
| 34 | const TYPE_DATE = 'date'; |
||
| 35 | const TYPE_OBJECT = 'object'; |
||
| 36 | const TYPE_ARRAY = 'array'; |
||
| 37 | |||
| 38 | const ERROR_REQUIRED_FIELD_MISSING = 'required_field_missing'; |
||
| 39 | const ERROR_VALIDATION_FAILED = 'validation_failed'; |
||
| 40 | const ERROR_NOT_UNIQUE = 'not_unique'; |
||
| 41 | |||
| 42 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 43 | |||
| 44 | ///////////////////////////// |
||
| 45 | // Model visible variables |
||
| 46 | ///////////////////////////// |
||
| 47 | |||
| 48 | /** |
||
| 49 | * List of model ID property names. |
||
| 50 | * |
||
| 51 | * @staticvar array |
||
| 52 | */ |
||
| 53 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Property definitions expressed as a key-value map with |
||
| 57 | * property names as the keys. |
||
| 58 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 59 | * |
||
| 60 | * @staticvar array |
||
| 61 | */ |
||
| 62 | protected static $properties = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @staticvar array |
||
| 66 | */ |
||
| 67 | protected static $relationships = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @staticvar \Pimple\Container |
||
| 71 | */ |
||
| 72 | protected static $injectedApp; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @staticvar array |
||
| 76 | */ |
||
| 77 | protected static $dispatchers; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \Pimple\Container |
||
| 81 | */ |
||
| 82 | protected $app; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $_values = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $_unsaved = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | protected $_exists = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \Infuse\ErrorStack |
||
| 101 | */ |
||
| 102 | protected $_errors; |
||
| 103 | |||
| 104 | ///////////////////////////// |
||
| 105 | // Base model variables |
||
| 106 | ///////////////////////////// |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @staticvar array |
||
| 110 | */ |
||
| 111 | private static $propertyDefinitionBase = [ |
||
| 112 | 'type' => self::TYPE_STRING, |
||
| 113 | 'mutable' => self::MUTABLE, |
||
| 114 | 'null' => false, |
||
| 115 | 'unique' => false, |
||
| 116 | 'required' => false, |
||
| 117 | ]; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @staticvar array |
||
| 121 | */ |
||
| 122 | private static $defaultIDProperty = [ |
||
| 123 | 'type' => self::TYPE_NUMBER, |
||
| 124 | 'mutable' => self::IMMUTABLE, |
||
| 125 | ]; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @staticvar array |
||
| 129 | */ |
||
| 130 | private static $timestampProperties = [ |
||
| 131 | 'created_at' => [ |
||
| 132 | 'type' => self::TYPE_DATE, |
||
| 133 | 'default' => null, |
||
| 134 | 'null' => true, |
||
| 135 | 'validate' => 'timestamp|db_timestamp', |
||
| 136 | ], |
||
| 137 | 'updated_at' => [ |
||
| 138 | 'type' => self::TYPE_DATE, |
||
| 139 | 'validate' => 'timestamp|db_timestamp', |
||
| 140 | ], |
||
| 141 | ]; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @staticvar array |
||
| 145 | */ |
||
| 146 | private static $initialized = []; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @staticvar Model\Driver\DriverInterface |
||
| 150 | */ |
||
| 151 | private static $driver; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @staticvar array |
||
| 155 | */ |
||
| 156 | private static $accessors = []; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @staticvar array |
||
| 160 | */ |
||
| 161 | private static $mutators = []; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var bool |
||
| 165 | */ |
||
| 166 | private $_ignoreUnsaved; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Creates a new model object. |
||
| 170 | * |
||
| 171 | * @param array $values values to fill model with |
||
| 172 | */ |
||
| 173 | public function __construct(array $values = []) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Performs initialization on this model. |
||
| 181 | */ |
||
| 182 | private function init() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The initialize() method is called once per model. It's used |
||
| 196 | * to perform any one-off tasks before the model gets |
||
| 197 | * constructed. This is a great place to add any model |
||
| 198 | * properties. When extending this method be sure to call |
||
| 199 | * parent::initialize() as some important stuff happens here. |
||
| 200 | * If extending this method to add properties then you should |
||
| 201 | * call parent::initialize() after adding any properties. |
||
| 202 | */ |
||
| 203 | protected function initialize() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Injects a DI container. |
||
| 228 | * |
||
| 229 | * @param \Pimple\Container $app |
||
| 230 | */ |
||
| 231 | public static function inject(Container $app) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Gets the DI container used for this model. |
||
| 238 | * |
||
| 239 | * @return \Pimple\Container |
||
| 240 | */ |
||
| 241 | public function getApp() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Sets the driver for all models. |
||
| 248 | * |
||
| 249 | * @param Model\Driver\DriverInterface $driver |
||
| 250 | */ |
||
| 251 | public static function setDriver(DriverInterface $driver) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the driver for all models. |
||
| 258 | * |
||
| 259 | * @return Model\Driver\DriverInterface |
||
| 260 | * |
||
| 261 | * @throws BadMethodCallException |
||
| 262 | */ |
||
| 263 | public static function getDriver() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Clears the driver for all models. |
||
| 274 | */ |
||
| 275 | public static function clearDriver() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the name of the model without namespacing. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public static function modelName() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Gets the model ID. |
||
| 297 | * |
||
| 298 | * @return string|number|null ID |
||
| 299 | */ |
||
| 300 | public function id() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Gets a key-value map of the model ID(s). |
||
| 315 | * |
||
| 316 | * @return array ID map |
||
| 317 | */ |
||
| 318 | public function ids() |
||
| 322 | |||
| 323 | ///////////////////////////// |
||
| 324 | // Magic Methods |
||
| 325 | ///////////////////////////// |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Converts the model into a string. |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function __toString() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Shortcut to a get() call for a given property. |
||
| 339 | * |
||
| 340 | * @param string $name |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | public function __get($name) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Sets an unsaved value. |
||
| 359 | * |
||
| 360 | * @param string $name |
||
| 361 | * @param mixed $value |
||
| 362 | * |
||
| 363 | * @throws BadMethodCallException |
||
| 364 | */ |
||
| 365 | public function __set($name, $value) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Checks if an unsaved value or property exists by this name. |
||
| 383 | * |
||
| 384 | * @param string $name |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function __isset($name) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Unsets an unsaved value. |
||
| 395 | * |
||
| 396 | * @param string $name |
||
| 397 | * |
||
| 398 | * @throws BadMethodCallException |
||
| 399 | */ |
||
| 400 | public function __unset($name) |
||
| 410 | |||
| 411 | ///////////////////////////// |
||
| 412 | // ArrayAccess Interface |
||
| 413 | ///////////////////////////// |
||
| 414 | |||
| 415 | public function offsetExists($offset) |
||
| 419 | |||
| 420 | public function offsetGet($offset) |
||
| 424 | |||
| 425 | public function offsetSet($offset, $value) |
||
| 429 | |||
| 430 | public function offsetUnset($offset) |
||
| 434 | |||
| 435 | public static function __callStatic($name, $parameters) |
||
| 442 | |||
| 443 | ///////////////////////////// |
||
| 444 | // Property Definitions |
||
| 445 | ///////////////////////////// |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Gets all the property definitions for the model. |
||
| 449 | * |
||
| 450 | * @return array key-value map of properties |
||
| 451 | */ |
||
| 452 | public static function getProperties() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Gets a property defition for the model. |
||
| 459 | * |
||
| 460 | * @param string $property property to lookup |
||
| 461 | * |
||
| 462 | * @return array|null property |
||
| 463 | */ |
||
| 464 | public static function getProperty($property) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Gets the names of the model ID properties. |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | public static function getIdProperties() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Builds an existing model instance given a single ID value or |
||
| 481 | * ordered array of ID values. |
||
| 482 | * |
||
| 483 | * @param mixed $id |
||
| 484 | * |
||
| 485 | * @return Model |
||
| 486 | */ |
||
| 487 | public static function buildFromIds($id) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Checks if the model has a property. |
||
| 502 | * |
||
| 503 | * @param string $property property |
||
| 504 | * |
||
| 505 | * @return bool has property |
||
| 506 | */ |
||
| 507 | public static function hasProperty($property) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Gets the mutator method name for a given proeprty name. |
||
| 514 | * Looks for methods in the form of `setPropertyValue`. |
||
| 515 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 516 | * |
||
| 517 | * @param string $property property |
||
| 518 | * |
||
| 519 | * @return string|false method name if it exists |
||
| 520 | */ |
||
| 521 | View Code Duplication | public static function getMutator($property) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Gets the accessor method name for a given proeprty name. |
||
| 542 | * Looks for methods in the form of `getPropertyValue`. |
||
| 543 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 544 | * |
||
| 545 | * @param string $property property |
||
| 546 | * |
||
| 547 | * @return string|false method name if it exists |
||
| 548 | */ |
||
| 549 | View Code Duplication | public static function getAccessor($property) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Checks if a given property is a relationship. |
||
| 570 | * |
||
| 571 | * @param string $property |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | public static function isRelationship($property) |
||
| 579 | |||
| 580 | ///////////////////////////// |
||
| 581 | // CRUD Operations |
||
| 582 | ///////////////////////////// |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Saves the model. |
||
| 586 | * |
||
| 587 | * @return bool |
||
| 588 | */ |
||
| 589 | public function save() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Creates a new model. |
||
| 600 | * |
||
| 601 | * @param array $data optional key-value properties to set |
||
| 602 | * |
||
| 603 | * @return bool |
||
| 604 | * |
||
| 605 | * @throws BadMethodCallException when called on an existing model |
||
| 606 | */ |
||
| 607 | public function create(array $data = []) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Ignores unsaved values when fetching the next value. |
||
| 684 | * |
||
| 685 | * @return self |
||
| 686 | */ |
||
| 687 | public function ignoreUnsaved() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Gets property values from the model. |
||
| 696 | * |
||
| 697 | * This method looks up values from these locations in this |
||
| 698 | * precedence order (least important to most important): |
||
| 699 | * 1. defaults |
||
| 700 | * 2. data layer |
||
| 701 | * 3. local cache |
||
| 702 | * 4. unsaved values |
||
| 703 | * |
||
| 704 | * @param array $properties list of property names to fetch values of |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | public function get(array $properties) |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Builds a key-value map of the requested properties given a set of values. |
||
| 739 | * |
||
| 740 | * @param array $properties |
||
| 741 | * @param array $values |
||
| 742 | * |
||
| 743 | * @return array |
||
| 744 | * |
||
| 745 | * @throws InvalidArgumentException when a property was requested not present in the values |
||
| 746 | */ |
||
| 747 | private function buildGetResponse(array $properties, array $values) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Gets the IDs for a newly created model. |
||
| 779 | * |
||
| 780 | * @return string |
||
| 781 | */ |
||
| 782 | protected function getNewIds() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Converts the model to an array. |
||
| 800 | * |
||
| 801 | * @return array model array |
||
| 802 | */ |
||
| 803 | public function toArray() |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Updates the model. |
||
| 824 | * |
||
| 825 | * @param array $data optional key-value properties to set |
||
| 826 | * |
||
| 827 | * @return bool |
||
| 828 | * |
||
| 829 | * @throws BadMethodCallException when not called on an existing model |
||
| 830 | */ |
||
| 831 | public function set(array $data = []) |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Delete the model. |
||
| 898 | * |
||
| 899 | * @return bool success |
||
| 900 | */ |
||
| 901 | public function delete() |
||
| 930 | |||
| 931 | ///////////////////////////// |
||
| 932 | // Queries |
||
| 933 | ///////////////////////////// |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Generates a new query instance. |
||
| 937 | * |
||
| 938 | * @return Query |
||
| 939 | */ |
||
| 940 | public static function query() |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Finds a single instance of a model given it's ID. |
||
| 952 | * |
||
| 953 | * @param mixed $id |
||
| 954 | * |
||
| 955 | * @return Model|false |
||
| 956 | */ |
||
| 957 | public static function find($id) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Gets the toal number of records matching an optional criteria. |
||
| 971 | * |
||
| 972 | * @param array $where criteria |
||
| 973 | * |
||
| 974 | * @return int total |
||
| 975 | */ |
||
| 976 | public static function totalRecords(array $where = []) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Checks if the model exists. |
||
| 986 | * |
||
| 987 | * @return bool |
||
| 988 | */ |
||
| 989 | public function exists() |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Loads the model from the data layer. |
||
| 996 | * |
||
| 997 | * @return self |
||
| 998 | */ |
||
| 999 | public function refresh() |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Loads values into the model retrieved from the data layer. |
||
| 1016 | * |
||
| 1017 | * @param array $values values |
||
| 1018 | * |
||
| 1019 | * @return self |
||
| 1020 | */ |
||
| 1021 | public function refreshWith(array $values) |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Clears the cache for this model. |
||
| 1031 | * |
||
| 1032 | * @return self |
||
| 1033 | */ |
||
| 1034 | public function clearCache() |
||
| 1041 | |||
| 1042 | ///////////////////////////// |
||
| 1043 | // Relationships |
||
| 1044 | ///////////////////////////// |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Creates the parent side of a One-To-One relationship. |
||
| 1048 | * |
||
| 1049 | * @param string $model foreign model class |
||
| 1050 | * @param string $foreignKey identifying key on foreign model |
||
| 1051 | * @param string $localKey identifying key on local model |
||
| 1052 | * |
||
| 1053 | * @return \Pulsar\Relation\Relation |
||
| 1054 | */ |
||
| 1055 | View Code Duplication | public function hasOne($model, $foreignKey = '', $localKey = '') |
|
| 1070 | |||
| 1071 | /** |
||
| 1072 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1073 | * |
||
| 1074 | * @param string $model foreign model class |
||
| 1075 | * @param string $foreignKey identifying key on foreign model |
||
| 1076 | * @param string $localKey identifying key on local model |
||
| 1077 | * |
||
| 1078 | * @return \Pulsar\Relation\Relation |
||
| 1079 | */ |
||
| 1080 | View Code Duplication | public function belongsTo($model, $foreignKey = '', $localKey = '') |
|
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1098 | * |
||
| 1099 | * @param string $model foreign model class |
||
| 1100 | * @param string $foreignKey identifying key on foreign model |
||
| 1101 | * @param string $localKey identifying key on local model |
||
| 1102 | * |
||
| 1103 | * @return \Pulsar\Relation\Relation |
||
| 1104 | */ |
||
| 1105 | View Code Duplication | public function hasMany($model, $foreignKey = '', $localKey = '') |
|
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Creates the child side of a Many-To-Many relationship. |
||
| 1123 | * |
||
| 1124 | * @param string $model foreign model class |
||
| 1125 | * @param string $foreignKey identifying key on foreign model |
||
| 1126 | * @param string $localKey identifying key on local model |
||
| 1127 | * |
||
| 1128 | * @return \Pulsar\Relation\Relation |
||
| 1129 | */ |
||
| 1130 | View Code Duplication | public function belongsToMany($model, $foreignKey = '', $localKey = '') |
|
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Loads a given relationship (if not already) and returns |
||
| 1148 | * its results. |
||
| 1149 | * |
||
| 1150 | * @param string $name |
||
| 1151 | * |
||
| 1152 | * @return mixed |
||
| 1153 | */ |
||
| 1154 | protected function loadRelationship($name) |
||
| 1163 | |||
| 1164 | ///////////////////////////// |
||
| 1165 | // Events |
||
| 1166 | ///////////////////////////// |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Gets the event dispatcher. |
||
| 1170 | * |
||
| 1171 | * @return \Symfony\Component\EventDispatcher\EventDispatcher |
||
| 1172 | */ |
||
| 1173 | public static function getDispatcher($ignoreCache = false) |
||
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Subscribes to a listener to an event. |
||
| 1185 | * |
||
| 1186 | * @param string $event event name |
||
| 1187 | * @param callable $listener |
||
| 1188 | * @param int $priority optional priority, higher #s get called first |
||
| 1189 | */ |
||
| 1190 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Adds a listener to the model.creating event. |
||
| 1197 | * |
||
| 1198 | * @param callable $listener |
||
| 1199 | * @param int $priority |
||
| 1200 | */ |
||
| 1201 | public static function creating(callable $listener, $priority = 0) |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Adds a listener to the model.created event. |
||
| 1208 | * |
||
| 1209 | * @param callable $listener |
||
| 1210 | * @param int $priority |
||
| 1211 | */ |
||
| 1212 | public static function created(callable $listener, $priority = 0) |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * Adds a listener to the model.updating event. |
||
| 1219 | * |
||
| 1220 | * @param callable $listener |
||
| 1221 | * @param int $priority |
||
| 1222 | */ |
||
| 1223 | public static function updating(callable $listener, $priority = 0) |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Adds a listener to the model.updated event. |
||
| 1230 | * |
||
| 1231 | * @param callable $listener |
||
| 1232 | * @param int $priority |
||
| 1233 | */ |
||
| 1234 | public static function updated(callable $listener, $priority = 0) |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * Adds a listener to the model.deleting event. |
||
| 1241 | * |
||
| 1242 | * @param callable $listener |
||
| 1243 | * @param int $priority |
||
| 1244 | */ |
||
| 1245 | public static function deleting(callable $listener, $priority = 0) |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Adds a listener to the model.deleted event. |
||
| 1252 | * |
||
| 1253 | * @param callable $listener |
||
| 1254 | * @param int $priority |
||
| 1255 | */ |
||
| 1256 | public static function deleted(callable $listener, $priority = 0) |
||
| 1260 | |||
| 1261 | /** |
||
| 1262 | * Dispatches an event. |
||
| 1263 | * |
||
| 1264 | * @param string $eventName |
||
| 1265 | * |
||
| 1266 | * @return ModelEvent |
||
| 1267 | */ |
||
| 1268 | protected function dispatch($eventName) |
||
| 1274 | |||
| 1275 | ///////////////////////////// |
||
| 1276 | // Validation |
||
| 1277 | ///////////////////////////// |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Gets the error stack for this model instance. Used to |
||
| 1281 | * keep track of validation errors. |
||
| 1282 | * |
||
| 1283 | * @return \Infuse\ErrorStack |
||
| 1284 | */ |
||
| 1285 | public function getErrors() |
||
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Validates and marshals a value to storage. |
||
| 1296 | * |
||
| 1297 | * @param array $property |
||
| 1298 | * @param string $propertyName |
||
| 1299 | * @param mixed $value |
||
| 1300 | * |
||
| 1301 | * @return bool |
||
| 1302 | */ |
||
| 1303 | private function filterAndValidate(array $property, $propertyName, &$value) |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Validates a value for a property. |
||
| 1326 | * |
||
| 1327 | * @param array $property |
||
| 1328 | * @param string $propertyName |
||
| 1329 | * @param mixed $value |
||
| 1330 | * |
||
| 1331 | * @return bool |
||
| 1332 | */ |
||
| 1333 | private function validate(array $property, $propertyName, $value) |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Checks if a value is unique for a property. |
||
| 1356 | * |
||
| 1357 | * @param array $property |
||
| 1358 | * @param string $propertyName |
||
| 1359 | * @param mixed $value |
||
| 1360 | * |
||
| 1361 | * @return bool |
||
| 1362 | */ |
||
| 1363 | private function checkUniqueness(array $property, $propertyName, $value) |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Checks if an input has all of the required values. Adds |
||
| 1380 | * messages for any missing values to the error stack. |
||
| 1381 | * |
||
| 1382 | * @param array $values |
||
| 1383 | * |
||
| 1384 | * @return bool |
||
| 1385 | */ |
||
| 1386 | private function hasRequiredValues(array $values) |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * Gets the marshaled default value for a property (if set). |
||
| 1407 | * |
||
| 1408 | * @param string $property |
||
| 1409 | * |
||
| 1410 | * @return mixed |
||
| 1411 | */ |
||
| 1412 | private function getPropertyDefault(array $property) |
||
| 1416 | } |
||
| 1417 |
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.