| Conditions | 21 |
| Paths | 393 |
| Total Lines | 96 |
| Code Lines | 59 |
| 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 |
||
| 67 | public function validateRecursive($field_name, $filter_name, $filter_argv, $html = false, $secure = false) |
||
| 68 | { |
||
| 69 | // check if we got it from form defined request method |
||
| 70 | if (App::$Request->getMethod() !== $this->_sendMethod) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | $inputTypes = []; |
||
| 75 | // check input data type. Maybe file or input (text) |
||
| 76 | if (method_exists($this, 'inputTypes')) { |
||
| 77 | $inputTypes = $this->inputTypes(); |
||
| 78 | } |
||
| 79 | |||
| 80 | // sounds like file |
||
| 81 | if ($inputTypes[$field_name] === 'file') { |
||
| 82 | $field_value = $this->getFile($field_name); |
||
| 83 | } else { // sounds like plain post data |
||
| 84 | $field_value = $this->getInput($field_name); |
||
| 85 | // remove or safe use html |
||
| 86 | if ($html === false) { |
||
| 87 | $field_value = App::$Security->strip_tags($field_value); |
||
| 88 | } else { |
||
| 89 | if ($secure !== true) { |
||
| 90 | $field_value = App::$Security->secureHtml($field_value); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $check = false; |
||
|
|
|||
| 96 | // maybe no filter required? |
||
| 97 | if ($filter_name === 'used') { |
||
| 98 | $check = true; |
||
| 99 | } elseif (Str::contains('::', $filter_name)) { // sounds like a callback class::method::method |
||
| 100 | // string to array via delimiter :: |
||
| 101 | $callbackArray = explode('::', $filter_name); |
||
| 102 | // first item is a class name |
||
| 103 | $class = array_shift($callbackArray); |
||
| 104 | // last item its a function |
||
| 105 | $method = array_pop($callbackArray); |
||
| 106 | // left any items? maybe post-static callbacks? |
||
| 107 | if (count($callbackArray) > 0) { |
||
| 108 | foreach ($callbackArray as $obj) { |
||
| 109 | if (Str::startsWith('$', $obj) && property_exists($class, ltrim($obj, '$'))) { // sounds like a variable |
||
| 110 | $obj = ltrim($obj, '$'); // trim variable symbol '$' |
||
| 111 | $class = $class::$$obj; // make magic :) |
||
| 112 | } elseif (method_exists($class, $obj)) { // maybe its a function? |
||
| 113 | $class = $class::$obj; // call function |
||
| 114 | } else { |
||
| 115 | throw new SyntaxException('Filter callback execution failed: ' . $filter_name); |
||
| 116 | } |
||
| 117 | |||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // check is endpoint method exist |
||
| 122 | if (method_exists($class, $method)) { |
||
| 123 | $check = @$class::$method($field_value, $filter_argv); |
||
| 124 | } else { |
||
| 125 | throw new SyntaxException('Filter callback execution failed: ' . $filter_name); |
||
| 126 | } |
||
| 127 | } elseif (method_exists('Ffcms\Core\Filter\Native', $filter_name)) { // only full namespace\class path based :( |
||
| 128 | if ($filter_argv != null) { |
||
| 129 | $check = Native::$filter_name($field_value, $filter_argv); |
||
| 130 | } else { |
||
| 131 | $check = Native::$filter_name($field_value); |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | throw new SyntaxException('Filter "' . $filter_name . '" is not exist'); |
||
| 135 | } |
||
| 136 | if ($check !== true) { // switch only on fail check. |
||
| 137 | $this->_badAttr[] = $field_name; |
||
| 138 | } else { |
||
| 139 | $field_set_name = $field_name; |
||
| 140 | // prevent array-type setting |
||
| 141 | if (Str::contains('.', $field_set_name)) { |
||
| 142 | $field_set_name = strstr($field_set_name, '.', true); |
||
| 143 | } |
||
| 144 | if (property_exists($this, $field_set_name)) { |
||
| 145 | if ($field_name !== $field_set_name) { // array-based property |
||
| 146 | $dot_path = trim(strstr($field_name, '.'), '.'); |
||
| 147 | // prevent throws any exceptions for null and false objects |
||
| 148 | if (!Obj::isArray($this->{$field_set_name})) { |
||
| 149 | $this->{$field_set_name} = []; |
||
| 150 | } |
||
| 151 | // use dot-data provider to compile output array |
||
| 152 | $dotData = new DotData($this->{$field_set_name}); |
||
| 153 | $dotData->set($dot_path, $field_value); // todo: check me!!! bug here |
||
| 154 | // export data from dot-data lib to model property |
||
| 155 | $this->{$field_set_name} = $dotData->export(); |
||
| 156 | } else { // just single property |
||
| 157 | $this->{$field_name} = $field_value; // refresh model property's from post data |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | return $check; |
||
| 162 | } |
||
| 163 | |||
| 262 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.