Complex classes like Kohana_Jam_Validated 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 Kohana_Jam_Validated, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 14 | abstract class Kohana_Jam_Validated extends Model implements Serializable { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array The original data set on the object |
||
| 18 | */ |
||
| 19 | protected $_original = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array Data that's changed since the object was loaded |
||
| 23 | */ |
||
| 24 | protected $_changed = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array Data that's already been retrieved is cached |
||
| 28 | */ |
||
| 29 | protected $_retrieved = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array Unmapped data that is still accessible |
||
| 33 | */ |
||
| 34 | protected $_unmapped = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var boolean Whether or not the model is validating |
||
| 38 | */ |
||
| 39 | protected $_is_validating = FALSE; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Jam_Meta A copy of this object's meta object |
||
| 43 | */ |
||
| 44 | protected $_meta = NULL; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Boolean A flag that keeps track of whether or not the model is valid |
||
| 48 | */ |
||
| 49 | protected $_errors = NULL; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * |
||
| 54 | * A key can be passed to automatically load a model by its |
||
| 55 | * unique key. |
||
| 56 | * |
||
| 57 | * @param mixed|null $key |
||
|
|
|||
| 58 | */ |
||
| 59 | 441 | public function __construct($meta_name = NULL) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Gets the value of a field. |
||
| 74 | * |
||
| 75 | * Unlike Jam_Model::get(), values that are returned are cached |
||
| 76 | * until they are changed and relationships are automatically select()ed. |
||
| 77 | * |
||
| 78 | * @see get() |
||
| 79 | * @param string $name The name or alias of the field you're retrieving |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | 146 | public function &__get($name) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Sets the value of a field. |
||
| 94 | * |
||
| 95 | * @see set() |
||
| 96 | * @param string $name The name of the field you're setting |
||
| 97 | * @param mixed $value The value you're setting |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | 76 | public function __set($name, $value) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Passes unknown methods along to the behaviors. |
||
| 114 | * |
||
| 115 | * @param string $method |
||
| 116 | * @param array $args |
||
| 117 | * @return mixed |
||
| 118 | **/ |
||
| 119 | 34 | public function __call($method, $args) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Returns true if $name is a field of the model or an unmapped column. |
||
| 126 | * |
||
| 127 | * This does not conform to the standard of returning FALSE if the |
||
| 128 | * property is set but the value is NULL. Rather this acts more like |
||
| 129 | * property_exists. |
||
| 130 | * |
||
| 131 | * @param string $name |
||
| 132 | * @return boolean |
||
| 133 | */ |
||
| 134 | public function __isset($name) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * This doesn't unset fields. Rather, it sets them to their original |
||
| 141 | * value. Unmapped, changed, and retrieved values are unset. |
||
| 142 | * |
||
| 143 | * In essence, unsetting a field sets it as if you never made any changes |
||
| 144 | * to it, and clears the cache if the value has been retrieved with those changes. |
||
| 145 | * |
||
| 146 | * @param string $name |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public function __unset($name) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns a string representation of the model in the |
||
| 160 | * form of `Model_Name (id)` or `Model_Name (NULL)` if |
||
| 161 | * the model is not loaded. |
||
| 162 | * |
||
| 163 | * This is designed to be useful for debugging. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function __toString() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets the value for a field. |
||
| 174 | * |
||
| 175 | * @param string $name The field's name |
||
| 176 | * @return array|mixed |
||
| 177 | */ |
||
| 178 | 215 | public function get($name) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the original value of a field, before it was changed. |
||
| 203 | * |
||
| 204 | * This method—combined with get(), which first searches for changed |
||
| 205 | * values—is useful for comparing changes that occurred on a model. |
||
| 206 | * |
||
| 207 | * @param string $field The field's or alias name |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | 207 | public function original($field_name = NULL) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Returns an array of values in the fields. |
||
| 223 | * |
||
| 224 | * You can pass an array of field names to retrieve |
||
| 225 | * only the values for those fields: |
||
| 226 | * |
||
| 227 | * $model->as_array(array('id', 'name', 'status')); |
||
| 228 | * |
||
| 229 | * @param array $fields |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | 10 | public function as_array(array $fields = NULL) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Set preloaded values, without changing the save, loaded and changed flags |
||
| 247 | * @param array|string $values |
||
| 248 | * @param mixed $value |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 12 | public function retrieved($values, $value = NULL) |
|
| 263 | /** |
||
| 264 | * Sets the value of a field. |
||
| 265 | * |
||
| 266 | * You can pass an array of key => value pairs |
||
| 267 | * to set multiple fields at the same time: |
||
| 268 | * |
||
| 269 | * $model->set(array( |
||
| 270 | * 'field1' => 'value', |
||
| 271 | * 'field2' => 'value', |
||
| 272 | * .... |
||
| 273 | * )); |
||
| 274 | * |
||
| 275 | * @param array|string $values |
||
| 276 | * @param mixed|null $value |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | 93 | public function set($values, $value = NULL) |
|
| 280 | { |
||
| 281 | // Accept set('name', 'value'); |
||
| 282 | 93 | if ( ! is_array($values)) |
|
| 283 | { |
||
| 284 | $values = array($values => $value); |
||
| 285 | } |
||
| 286 | |||
| 287 | 93 | foreach ($values as $key => & $value) |
|
| 288 | { |
||
| 289 | 91 | if ($field = $this->meta()->field($key)) |
|
| 290 | { |
||
| 291 | 78 | $converted_field_value = $field->set($this, $value, TRUE); |
|
| 292 | 78 | if ($converted_field_value !== $this->{$field->name}) |
|
| 293 | { |
||
| 294 | 56 | $this->_changed[$field->name] = $converted_field_value; |
|
| 295 | |||
| 296 | 56 | if (array_key_exists($field->name, $this->_retrieved)) |
|
| 297 | { |
||
| 298 | 78 | unset($this->_retrieved[$field->name]); |
|
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | 34 | elseif (property_exists($this, $key)) |
|
| 303 | { |
||
| 304 | 1 | $this->$key = $value; |
|
| 305 | } |
||
| 306 | else |
||
| 307 | { |
||
| 308 | 34 | unset($this->_retrieved[$key]); |
|
| 309 | 91 | $this->_unmapped[$key] = $value; |
|
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | 93 | return $this; |
|
| 314 | } |
||
| 315 | |||
| 316 | 19 | public function is_valid() |
|
| 317 | { |
||
| 318 | 19 | return count($this->errors()) == 0; |
|
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Validates the current model's data |
||
| 323 | * |
||
| 324 | * @throws Jam_Exception_Validation |
||
| 325 | * @param Validation|null $extra_validation |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | 19 | public function check($force = FALSE) |
|
| 329 | { |
||
| 330 | 19 | $this->_is_validating = TRUE; |
|
| 331 | // Run validation only when new or changed |
||
| 332 | 19 | if ($this->changed() OR $force) |
|
| 333 | { |
||
| 334 | // Reset the errors before checking |
||
| 335 | 19 | $this->_errors = FALSE; |
|
| 336 | |||
| 337 | 19 | $this->meta()->events()->trigger('model.before_check', $this, array($this->_changed)); |
|
| 338 | |||
| 339 | 19 | $this->meta()->execute_validators($this, $force); |
|
| 340 | |||
| 341 | 19 | $this->meta()->events()->trigger('model.after_check', $this, array($this->_changed)); |
|
| 342 | } |
||
| 343 | |||
| 344 | 19 | $this->_is_validating = FALSE; |
|
| 345 | |||
| 346 | 19 | return $this->is_valid(); |
|
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return $this |
||
| 351 | * @throws Jam_Exception_Validation |
||
| 352 | * |
||
| 353 | * @deprecated Since 0.5.24 Use try-catch around save() instead. This might be removed in future version. |
||
| 354 | */ |
||
| 355 | 1 | public function check_insist() |
|
| 356 | { |
||
| 357 | 1 | @trigger_error('check_insist() is deprecated and might be removed in future version.', E_USER_DEPRECATED); |
|
| 358 | |||
| 359 | 1 | if ( ! $this->check()) |
|
| 360 | 1 | throw new Jam_Exception_Validation('There was an error validating the :model: :errors', $this); |
|
| 361 | |||
| 362 | return $this; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Override this function to add custom validation after the validators. |
||
| 367 | * Having an empty validate function allow safely calling parent::validate() |
||
| 368 | * when extending models. |
||
| 369 | * |
||
| 370 | * You need to set errors with: |
||
| 371 | * $this->errors()->add('field', 'error_name'); |
||
| 372 | * |
||
| 373 | * @link http://git.io/5I47Tw docs |
||
| 374 | */ |
||
| 375 | public function validate() {} |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the errors from the previous check, if you provide a name, will return the errors only for that name |
||
| 379 | * Automatically loads the error messages file from messages/jam-validation/<model_name> |
||
| 380 | * If there are no errors yet - return NULL |
||
| 381 | * |
||
| 382 | * @param string $name the name of the field to get errors of |
||
| 383 | * @return Jam_Errors|string[]|NULL |
||
| 384 | */ |
||
| 385 | 202 | public function errors($name = NULL) |
|
| 386 | { |
||
| 387 | 202 | if ( ! $this->_errors) |
|
| 388 | { |
||
| 389 | 202 | $this->_errors = new Jam_Errors($this, $this->meta()->errors_filename()); |
|
| 390 | } |
||
| 391 | |||
| 392 | 202 | if ($name !== NULL) |
|
| 393 | 1 | return $this->_errors->messages($name); |
|
| 394 | |||
| 395 | 201 | return $this->_errors; |
|
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Removes any changes made to a model. |
||
| 400 | * |
||
| 401 | * This method only works on loaded models. |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function revert() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Sets a model to its original state, as if freshly instantiated |
||
| 417 | * |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | 196 | public function clear() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Whether or not the model is in the process of being validated |
||
| 435 | * |
||
| 436 | * @return boolean |
||
| 437 | */ |
||
| 438 | 7 | public function is_validating() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Returns whether or not the particular $field has changed. |
||
| 445 | * |
||
| 446 | * If $field is NULL, the method returns whether or not any |
||
| 447 | * data whatsoever was changed on the model. |
||
| 448 | * |
||
| 449 | * @param string $field |
||
| 450 | * @return boolean |
||
| 451 | */ |
||
| 452 | 58 | public function changed($name = NULL) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the value of the model's primary key |
||
| 466 | * |
||
| 467 | * @return mixed |
||
| 468 | */ |
||
| 469 | 146 | public function id() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the value of the model's name key |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | 17 | public function name() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Returns the model's meta object |
||
| 486 | * |
||
| 487 | * @return Jam_Meta |
||
| 488 | */ |
||
| 489 | 754 | public function meta() |
|
| 490 | { |
||
| 491 | 754 | if ( ! $this->_meta) |
|
| 492 | throw new Kohana_Exception('Model for class :class does not have a meta', array(':class' => get_class($this))); |
||
| 493 | |||
| 494 | 754 | return $this->_meta; |
|
| 495 | } |
||
| 496 | |||
| 497 | 26 | public function unmapped($name) |
|
| 501 | |||
| 502 | |||
| 503 | public function serialize() |
||
| 504 | { |
||
| 505 | return serialize(array( |
||
| 506 | 'original' => $this->_original, |
||
| 507 | 'changed' => $this->_changed, |
||
| 508 | 'unmapped' => $this->_unmapped, |
||
| 509 | )); |
||
| 510 | } |
||
| 511 | |||
| 512 | public function unserialize($data) |
||
| 520 | } |
||
| 521 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.