| Conditions | 26 | 
| Paths | 1672 | 
| Total Lines | 104 | 
| Code Lines | 54 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 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  | 
            ||
| 183 | public function validate()  | 
            ||
| 184 |     { | 
            ||
| 185 |         if (count($this->sourceVars) == 0) { | 
            ||
| 186 |             $this->addError(1, language()->getLine('SECURITY_RULES_E_DATA_SOURCE_EMPTY')); | 
            ||
| 187 | |||
| 188 | return false;  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 |         foreach ($this->clauses as $fieldName => $fieldParams) { | 
            ||
| 192 | |||
| 193 | /* Throw exception if existed rules field not yet exists in data source */  | 
            ||
| 194 |             if ( ! array_key_exists($fieldName, $this->sourceVars)) { | 
            ||
| 195 |                 throw new OutOfRangeException('SECURITY_RULES_E_HEADER_OUTOFRANGEEXCEPTION', 1); | 
            ||
| 196 | }  | 
            ||
| 197 | |||
| 198 |             if (is_string($fieldParams[ 'rules' ])) { | 
            ||
| 199 | /**  | 
            ||
| 200 | * Explode field rules by | as delimiter  | 
            ||
| 201 | */  | 
            ||
| 202 |                 $fieldRules = explode('|', $fieldParams[ 'rules' ]); | 
            ||
| 203 | |||
| 204 |                 foreach ($fieldRules as $fieldRuleMethod) { | 
            ||
| 205 | /* Get parameter from given data */  | 
            ||
| 206 | $fieldValue = $this->sourceVars[ $fieldName ];  | 
            ||
| 207 |                     if ( ! is_array($fieldValue)) { | 
            ||
| 208 | $fieldValue = [$fieldValue];  | 
            ||
| 209 | }  | 
            ||
| 210 | |||
| 211 |                     if (empty($fieldValue)) { | 
            ||
| 212 | array_unshift($fieldValue, null);  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | /* Check if rules has parameter */  | 
            ||
| 216 |                     if (preg_match_all("/\[(.*)\]/", $fieldRuleMethod, $fieldRuleParams)) { | 
            ||
| 217 | |||
| 218 | /* Remove [] from method */  | 
            ||
| 219 |                         $fieldRuleMethod = preg_replace("/\[.*\]/", '', $fieldRuleMethod); | 
            ||
| 220 | |||
| 221 | /* Explode rule parameter */  | 
            ||
| 222 |                         $fieldRuleParams = explode(',', preg_replace("/,[ ]+/", ',', $fieldRuleParams[ 1 ][ 0 ])); | 
            ||
| 223 | |||
| 224 |                         if ($fieldRuleMethod === 'match') { | 
            ||
| 225 |                             foreach ($fieldRuleParams as $fieldRuleParamKey => $fieldRuleParamValue) { | 
            ||
| 226 |                                 if (array_key_exists($fieldRuleParamValue, $this->sourceVars)) { | 
            ||
| 227 | $fieldRuleParams[ $fieldRuleParamKey ] = $this->sourceVars[ $fieldRuleParamValue ];  | 
            ||
| 228 | }  | 
            ||
| 229 | }  | 
            ||
| 230 |                         } elseif ($fieldRuleMethod === 'listed') { | 
            ||
| 231 | $fieldRuleParams = [$fieldRuleParams];  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | /* Merge method's param with field rule's params */  | 
            ||
| 235 | $fieldValue = array_merge($fieldValue, $fieldRuleParams);  | 
            ||
| 236 | }  | 
            ||
| 237 | |||
| 238 | $validationClass = new Validation;  | 
            ||
| 239 | $validationMethod = 'is' . studlycase($fieldRuleMethod);  | 
            ||
| 240 | $validationStatus = false;  | 
            ||
| 241 | |||
| 242 | /* Throw exception if method not exists in validation class */  | 
            ||
| 243 |                     if (method_exists($validationClass, $validationMethod)) { | 
            ||
| 244 | $validationStatus = call_user_func_array([&$validationClass, $validationMethod], $fieldValue);  | 
            ||
| 245 |                     } elseif (function_exists($fieldRuleMethod)) { | 
            ||
| 246 | $validationStatus = call_user_func_array($fieldRuleMethod, $fieldValue);  | 
            ||
| 247 |                     } elseif (is_callable($fieldRuleMethod)) { | 
            ||
| 248 | $validationStatus = call_user_func_array($fieldRuleMethod, $fieldValue);  | 
            ||
| 249 | }  | 
            ||
| 250 | |||
| 251 |                     if ($validationStatus === false) { | 
            ||
| 252 |                         if ( ! empty($fieldParams[ 'messages' ])) { | 
            ||
| 253 | $message = $fieldParams[ 'messages' ];  | 
            ||
| 254 | |||
| 255 | /* If $rule message is array, replace $message with specified message */  | 
            ||
| 256 |                             if (is_array($fieldParams[ 'messages' ])) { | 
            ||
| 257 |                                 if (isset($fieldParams[ 'messages' ][ $fieldRuleMethod ])) { | 
            ||
| 258 | $message = $fieldParams[ 'messages' ][ $fieldRuleMethod ];  | 
            ||
| 259 |                                 } else { | 
            ||
| 260 | $message = $fieldParams[ 'messages' ][ $fieldName ];  | 
            ||
| 261 | }  | 
            ||
| 262 | }  | 
            ||
| 263 |                         } elseif (array_key_exists($fieldName, $this->customErrors)) { | 
            ||
| 264 | $message = $this->customErrors[ $fieldName ];  | 
            ||
| 265 |                         } elseif (array_key_exists($fieldRuleMethod, $this->customErrors)) { | 
            ||
| 266 | $message = $this->customErrors[ $fieldRuleMethod ];  | 
            ||
| 267 |                         } else { | 
            ||
| 268 | $message = 'RULE_' . strtoupper($fieldRuleMethod);  | 
            ||
| 269 | }  | 
            ||
| 270 | |||
| 271 | /* Replace message placeholder, :attribute, :params */  | 
            ||
| 272 |                         $message = str_replace(':attribute', | 
            ||
| 273 | (isset($fieldParams[ 'label' ]) ? $fieldParams[ 'label' ] : $fieldName), $message);  | 
            ||
| 274 |                         if (isset($fieldRuleParams) AND ! empty($fieldRuleParams[ 0 ])) { | 
            ||
| 275 |                             $message = str_replace(':params', implode(',', $fieldRuleParams), $message); | 
            ||
| 276 | }  | 
            ||
| 277 | |||
| 278 | $this->setFieldError($fieldName, language($fieldParams[ 'label' ]),  | 
            ||
| 279 | language($message, [$fieldValue]));  | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 | }  | 
            ||
| 283 | }  | 
            ||
| 284 | }  | 
            ||
| 285 | |||
| 286 | return empty($this->errors) ? true : false;  | 
            ||
| 287 | }  | 
            ||
| 327 | }  |