| Conditions | 18 |
| Paths | 55 |
| Total Lines | 82 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 112 | public function validateRecursive($propertyName, $filterName, $filterArgs = null) |
||
| 113 | { |
||
| 114 | // check if we got it from form defined request method |
||
| 115 | if (App::$Request->getMethod() !== $this->_sendMethod) { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | // get field value from user input data |
||
| 120 | $fieldValue = $this->getFieldValue($propertyName); |
||
| 121 | |||
| 122 | $check = false; |
||
| 123 | // maybe no filter required? |
||
| 124 | if ($filterName === 'used') { |
||
| 125 | $check = true; |
||
| 126 | } elseif (Str::contains('::', $filterName)) { // sounds like a callback class::method::method |
||
| 127 | // string to array via delimiter :: |
||
| 128 | $callbackArray = explode('::', $filterName); |
||
| 129 | // first item is a class name |
||
| 130 | $class = array_shift($callbackArray); |
||
| 131 | // last item its a function |
||
| 132 | $method = array_pop($callbackArray); |
||
| 133 | // left any items? maybe post-static callbacks? |
||
| 134 | if (count($callbackArray) > 0) { |
||
| 135 | foreach ($callbackArray as $obj) { |
||
| 136 | if (Str::startsWith('$', $obj) && property_exists($class, ltrim($obj, '$'))) { // sounds like a variable |
||
| 137 | $obj = ltrim($obj, '$'); // trim variable symbol '$' |
||
| 138 | $class = $class::${$obj}; // make magic :) |
||
| 139 | } elseif (method_exists($class, $obj)) { // maybe its a function? |
||
| 140 | $class = $class::$obj; // call function |
||
| 141 | } else { |
||
| 142 | throw new SyntaxException('Filter callback execution failed: ' . $filterName); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | // check is endpoint method exist |
||
| 148 | if (method_exists($class, $method)) { |
||
| 149 | $check = @$class::$method($fieldValue, $filterArgs); |
||
| 150 | } else { |
||
| 151 | throw new SyntaxException('Filter callback execution failed: ' . $filterName); |
||
| 152 | } |
||
| 153 | } elseif (method_exists('Ffcms\Core\Helper\ModelFilters', $filterName)) { // only full namespace\class path based :( |
||
| 154 | if ($filterArgs != null) { |
||
| 155 | $check = ModelFilters::$filterName($fieldValue, $filterArgs); |
||
| 156 | } else { |
||
| 157 | $check = ModelFilters::$filterName($fieldValue); |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | throw new SyntaxException('Filter "' . $filterName . '" is not exist'); |
||
| 161 | } |
||
| 162 | |||
| 163 | // if one from all validation tests is fail - mark as incorrect attribute |
||
| 164 | if ($check !== true) { |
||
| 165 | $this->_badAttr[] = $propertyName; |
||
| 166 | if (App::$Debug) { |
||
| 167 | App::$Debug->addMessage('Validation failed. Property: ' . $propertyName . ', filter: ' . $filterName, 'warning'); |
||
| 168 | } |
||
| 169 | } else { |
||
| 170 | $field_set_name = $propertyName; |
||
| 171 | // prevent array-type setting |
||
| 172 | if (Str::contains('.', $field_set_name)) { |
||
| 173 | $field_set_name = strstr($field_set_name, '.', true); |
||
| 174 | } |
||
| 175 | if (property_exists($this, $field_set_name)) { |
||
| 176 | if ($propertyName !== $field_set_name) { // array-based property |
||
| 177 | $dot_path = trim(strstr($propertyName, '.'), '.'); |
||
| 178 | // prevent throws any exceptions for null and false objects |
||
| 179 | if (!Any::isArray($this->{$field_set_name})) { |
||
| 180 | $this->{$field_set_name} = []; |
||
| 181 | } |
||
| 182 | |||
| 183 | // use dot-data provider to compile output array |
||
| 184 | $dotData = new DotData($this->{$field_set_name}); |
||
| 185 | $dotData->set($dot_path, $fieldValue); // todo: check me!!! Here can be bug of fail parsing dots and passing path-value |
||
| 186 | // export data from dot-data lib to model property |
||
| 187 | $this->{$field_set_name} = $dotData->export(); |
||
| 188 | } else { // just single property |
||
| 189 | $this->{$propertyName} = $fieldValue; // refresh model property's from post data |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | return $check; |
||
| 194 | } |
||
| 347 |