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 |
||
79 | public function validateRecursive($field_name, $filter_name, $filter_argv, $html = false, $secure = false) |
||
80 | { |
||
81 | // check if we got it from form defined request method |
||
82 | if (App::$Request->getMethod() !== $this->_sendMethod) { |
||
83 | return false; |
||
84 | } |
||
85 | |||
86 | // get field value from user input data |
||
87 | $field_value = $this->getFieldValue($field_name, $html, $secure); |
||
|
|||
88 | |||
89 | $check = false; |
||
90 | // maybe no filter required? |
||
91 | if ($filter_name === 'used') { |
||
92 | $check = true; |
||
93 | } elseif (Str::contains('::', $filter_name)) { // sounds like a callback class::method::method |
||
94 | // string to array via delimiter :: |
||
95 | $callbackArray = explode('::', $filter_name); |
||
96 | // first item is a class name |
||
97 | $class = array_shift($callbackArray); |
||
98 | // last item its a function |
||
99 | $method = array_pop($callbackArray); |
||
100 | // left any items? maybe post-static callbacks? |
||
101 | if (count($callbackArray) > 0) { |
||
102 | foreach ($callbackArray as $obj) { |
||
103 | if (Str::startsWith('$', $obj) && property_exists($class, ltrim($obj, '$'))) { // sounds like a variable |
||
104 | $obj = ltrim($obj, '$'); // trim variable symbol '$' |
||
105 | $class = $class::$$obj; // make magic :) |
||
106 | } elseif (method_exists($class, $obj)) { // maybe its a function? |
||
107 | $class = $class::$obj; // call function |
||
108 | } else { |
||
109 | throw new SyntaxException('Filter callback execution failed: ' . $filter_name); |
||
110 | } |
||
111 | |||
112 | } |
||
113 | } |
||
114 | |||
115 | // check is endpoint method exist |
||
116 | if (method_exists($class, $method)) { |
||
117 | $check = @$class::$method($field_value, $filter_argv); |
||
118 | } else { |
||
119 | throw new SyntaxException('Filter callback execution failed: ' . $filter_name); |
||
120 | } |
||
121 | } elseif (method_exists('Ffcms\Core\Filter\Native', $filter_name)) { // only full namespace\class path based :( |
||
122 | if ($filter_argv != null) { |
||
123 | $check = Native::$filter_name($field_value, $filter_argv); |
||
124 | } else { |
||
125 | $check = Native::$filter_name($field_value); |
||
126 | } |
||
127 | } else { |
||
128 | throw new SyntaxException('Filter "' . $filter_name . '" is not exist'); |
||
129 | } |
||
130 | if ($check !== true) { // switch only on fail check. |
||
131 | $this->_badAttr[] = $field_name; |
||
132 | } else { |
||
133 | $field_set_name = $field_name; |
||
134 | // prevent array-type setting |
||
135 | if (Str::contains('.', $field_set_name)) { |
||
136 | $field_set_name = strstr($field_set_name, '.', true); |
||
137 | } |
||
138 | if (property_exists($this, $field_set_name)) { |
||
139 | if ($field_name !== $field_set_name) { // array-based property |
||
140 | $dot_path = trim(strstr($field_name, '.'), '.'); |
||
141 | // prevent throws any exceptions for null and false objects |
||
142 | if (!Obj::isArray($this->{$field_set_name})) { |
||
143 | $this->{$field_set_name} = []; |
||
144 | } |
||
145 | // use dot-data provider to compile output array |
||
146 | $dotData = new DotData($this->{$field_set_name}); |
||
147 | $dotData->set($dot_path, $field_value); // todo: check me!!! big change the bug is there |
||
148 | // export data from dot-data lib to model property |
||
149 | $this->{$field_set_name} = $dotData->export(); |
||
150 | } else { // just single property |
||
151 | $this->{$field_name} = $field_value; // refresh model property's from post data |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | return $check; |
||
156 | } |
||
157 | |||
303 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.