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