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