Conditions | 29 |
Paths | 537 |
Total Lines | 114 |
Code Lines | 66 |
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 |
||
105 | { |
||
106 | $options += ['allowUnknowns' => false, 'defaultRequired' => false]; |
||
107 | |||
108 | $allowUnknowns = $options['allowUnknowns']; |
||
109 | $defaultRequired = $options['defaultRequired']; |
||
110 | |||
111 | if ($allowUnknowns !== false && $allowUnknowns !== true) { |
||
112 | throw new \InvalidArgumentException("'allowUnknowns' option was not a bool"); |
||
113 | } |
||
114 | |||
115 | if ($defaultRequired !== false && $defaultRequired !== true) { |
||
116 | throw new \InvalidArgumentException("'defaultRequired' option was not a bool"); |
||
117 | } |
||
118 | |||
119 | $inputToFilter = array_intersect_key($input, $spec); |
||
120 | $leftOverSpec = array_diff_key($spec, $input); |
||
121 | $leftOverInput = array_diff_key($input, $spec); |
||
122 | |||
123 | $errors = []; |
||
124 | foreach ($inputToFilter as $field => $value) { |
||
125 | $filters = $spec[$field]; |
||
126 | |||
127 | if (!is_array($filters)) { |
||
128 | throw new \InvalidArgumentException("filters for field '{$field}' was not a array"); |
||
129 | } |
||
130 | |||
131 | $customError = null; |
||
132 | if (array_key_exists('error', $filters)) { |
||
133 | $customError = $filters['error']; |
||
134 | if (!is_string($customError) || trim($customError) === '') { |
||
135 | throw new \InvalidArgumentException("error for field '{$field}' was not a non-empty string"); |
||
136 | } |
||
137 | |||
138 | unset($filters['error']);//unset so its not used as a filter |
||
139 | } |
||
140 | |||
141 | unset($filters['required']);//doesn't matter if required since we have this one |
||
142 | unset($filters['default']);//doesn't matter if there is a default since we have a value |
||
143 | foreach ($filters as $filter) { |
||
144 | if (!is_array($filter)) { |
||
145 | throw new \InvalidArgumentException("filter for field '{$field}' was not a array"); |
||
146 | } |
||
147 | |||
148 | if (empty($filter)) { |
||
149 | continue; |
||
150 | } |
||
151 | |||
152 | $function = array_shift($filter); |
||
153 | if ((is_string($function) || is_int($function)) && array_key_exists($function, self::$filterAliases)) { |
||
154 | $function = self::$filterAliases[$function]; |
||
155 | } |
||
156 | |||
157 | if (!is_callable($function)) { |
||
158 | throw new Exception( |
||
159 | "Function '" . trim(var_export($function, true), "'") . "' for field '{$field}' is not callable" |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | array_unshift($filter, $value); |
||
164 | try { |
||
165 | $value = call_user_func_array($function, $filter); |
||
166 | } catch (Exception $e) { |
||
167 | $error = $customError; |
||
168 | if ($error === null) { |
||
169 | $error = sprintf( |
||
170 | "Field '%s' with value '%s' failed filtering, message '%s'", |
||
171 | $field, |
||
172 | trim(var_export($value, true), "'"), |
||
173 | $e->getMessage() |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | $errors[] = $error; |
||
178 | continue 2;//next field |
||
179 | } |
||
180 | } |
||
181 | |||
182 | $inputToFilter[$field] = $value; |
||
183 | } |
||
184 | |||
185 | foreach ($leftOverSpec as $field => $filters) { |
||
186 | if (!is_array($filters)) { |
||
187 | throw new \InvalidArgumentException("filters for field '{$field}' was not a array"); |
||
188 | } |
||
189 | |||
190 | $required = isset($filters['required']) ? $filters['required'] : $defaultRequired; |
||
191 | |||
192 | if ($required !== false && $required !== true) { |
||
193 | throw new \InvalidArgumentException("'required' for field '{$field}' was not a bool"); |
||
194 | } |
||
195 | |||
196 | if (array_key_exists('default', $filters)) { |
||
197 | $inputToFilter[$field] = $filters['default']; |
||
198 | continue; |
||
199 | } |
||
200 | |||
201 | if ($required) { |
||
202 | $errors[] = "Field '{$field}' was required and not present"; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if (!$allowUnknowns) { |
||
207 | foreach ($leftOverInput as $field => $value) { |
||
208 | $errors[] = "Field '{$field}' with value '" . trim(var_export($value, true), "'") . "' is unknown"; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | if (empty($errors)) { |
||
213 | return [true, $inputToFilter, null, $leftOverInput]; |
||
214 | } |
||
215 | |||
216 | return [false, null, implode("\n", $errors), $leftOverInput]; |
||
217 | } |
||
218 | |||
219 | /** |
||
281 |