| Conditions | 12 |
| Paths | 11 |
| Total Lines | 44 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php /** MicroValidator */ |
||
| 75 | public function run($model, $client = false) |
||
| 76 | { |
||
| 77 | $elements = explode(',', str_replace(' ', '', array_shift($this->rule))); |
||
| 78 | $name = array_shift($this->rule); |
||
| 79 | |||
| 80 | $className = $this->getValidatorClass($name); |
||
| 81 | if (!$className) { |
||
| 82 | if (function_exists($name)) { |
||
| 83 | foreach ($elements AS $element) { |
||
| 84 | if (property_exists($model, $element)) { |
||
| 85 | $model->$element = call_user_func($name, $model->$element); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return true; |
||
| 90 | } elseif (method_exists($model, $name)) { |
||
| 91 | foreach ($elements AS $element) { |
||
| 92 | if (property_exists($model, $element) && !call_user_func([$model, $name], $element)) { |
||
| 93 | return false; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return true; |
||
| 98 | } else { |
||
| 99 | throw new Exception('Validator ' . $name . ' not defined.'); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** @var IValidator $valid */ |
||
| 104 | $valid = new $className(['container' => $this->container, 'params' => $this->rule]); |
||
| 105 | $valid->elements = $elements; |
||
|
|
|||
| 106 | |||
| 107 | if ($client && method_exists($valid, 'client')) { |
||
| 108 | $result = $this->clientValidate($valid, $model); |
||
| 109 | } else { |
||
| 110 | $result = $valid->validate($model); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($valid->errors) { |
||
| 114 | $this->errors[] = $valid->errors; |
||
| 115 | } |
||
| 116 | |||
| 117 | return $result; |
||
| 118 | } |
||
| 119 | |||
| 190 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: