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