Complex classes like Filterer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Filterer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | final class Filterer |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | const DEFAULT_FILTER_ALIASES = [ |
||
| 18 | 'in' => '\TraderInteractive\Filter\Arrays::in', |
||
| 19 | 'array' => '\TraderInteractive\Filter\Arrays::filter', |
||
| 20 | 'bool' => '\TraderInteractive\Filter\Booleans::filter', |
||
| 21 | 'float' => '\TraderInteractive\Filter\Floats::filter', |
||
| 22 | 'int' => '\TraderInteractive\Filter\Ints::filter', |
||
| 23 | 'bool-convert' => '\TraderInteractive\Filter\Booleans::convert', |
||
| 24 | 'uint' => '\TraderInteractive\Filter\UnsignedInt::filter', |
||
| 25 | 'string' => '\TraderInteractive\Filter\Strings::filter', |
||
| 26 | 'ofScalars' => '\TraderInteractive\Filterer::ofScalars', |
||
| 27 | 'ofArrays' => '\TraderInteractive\Filterer::ofArrays', |
||
| 28 | 'ofArray' => '\TraderInteractive\Filterer::ofArray', |
||
| 29 | 'url' => '\TraderInteractive\Filter\Url::filter', |
||
| 30 | 'email' => '\TraderInteractive\Filter\Email::filter', |
||
| 31 | 'explode' => '\TraderInteractive\Filter\Strings::explode', |
||
| 32 | 'flatten' => '\TraderInteractive\Filter\Arrays::flatten', |
||
| 33 | 'date' => '\TraderInteractive\Filter\DateTime::filter', |
||
| 34 | 'date-format' => '\TraderInteractive\Filter\DateTime::format', |
||
| 35 | 'timezone' => '\TraderInteractive\Filter\DateTimeZone::filter', |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private static $filterAliases = self::DEFAULT_FILTER_ALIASES; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Example: |
||
| 45 | * <pre> |
||
| 46 | * <?php |
||
| 47 | * class AppendFilter |
||
| 48 | * { |
||
| 49 | * public function filter($value, $extraArg) |
||
| 50 | * { |
||
| 51 | * return $value . $extraArg; |
||
| 52 | * } |
||
| 53 | * } |
||
| 54 | * $appendFilter = new AppendFilter(); |
||
| 55 | * |
||
| 56 | * $trimFunc = function($val) { return trim($val); }; |
||
| 57 | * |
||
| 58 | * list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter( |
||
| 59 | * [ |
||
| 60 | * 'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']], |
||
| 61 | * 'field two' => ['required' => true, ['floatval']], |
||
| 62 | * 'field three' => ['required' => false, ['float']], |
||
| 63 | * 'field four' => ['required' => true, 'default' => 1, ['uint']], |
||
| 64 | * ], |
||
| 65 | * ['field one' => ' abcd', 'field two' => '3.14'] |
||
| 66 | * ); |
||
| 67 | * |
||
| 68 | * var_dump($status); |
||
| 69 | * var_dump($result); |
||
| 70 | * var_dump($error); |
||
| 71 | * var_dump($unknowns); |
||
| 72 | * </pre> |
||
| 73 | * prints: |
||
| 74 | * <pre> |
||
| 75 | * bool(true) |
||
| 76 | * array(3) { |
||
| 77 | * 'field one' => |
||
| 78 | * string(6) "abcboo" |
||
| 79 | * 'field two' => |
||
| 80 | * double(3.14) |
||
| 81 | * 'field four' => |
||
| 82 | * int(1) |
||
| 83 | * } |
||
| 84 | * NULL |
||
| 85 | * array(0) { |
||
| 86 | * } |
||
| 87 | * </pre> |
||
| 88 | * |
||
| 89 | * @param array $spec the specification to apply to the $input. An array where each key is a known input field and |
||
| 90 | * each value is an array of filters. Each filter should be an array with the first member being |
||
| 91 | * anything that can pass is_callable() as well as accepting the value to filter as its first |
||
| 92 | * argument. Two examples would be the string 'trim' or an object function specified like [$obj, |
||
| 93 | * 'filter'], see is_callable() documentation. The rest of the members are extra arguments to the |
||
| 94 | * callable. The result of one filter will be the first argument to the next filter. In addition |
||
| 95 | * to the filters, the specification values may contain a 'required' key (default false) that |
||
| 96 | * controls the same behavior as the 'defaultRequired' option below but on a per field basis. A |
||
| 97 | * 'default' specification value may be used to substitute in a default to the $input when the |
||
| 98 | * key is not present (whether 'required' is specified or not). |
||
| 99 | * @param array $input the input the apply the $spec on. |
||
| 100 | * @param array $options 'allowUnknowns' (default false) true to allow unknowns or false to treat as error, |
||
| 101 | * 'defaultRequired' (default false) true to make fields required by default and treat as |
||
| 102 | * error on absence and false to allow their absence by default |
||
| 103 | * |
||
| 104 | * @return array on success [true, $input filtered, null, array of unknown fields] |
||
| 105 | * on error [false, null, 'error message', array of unknown fields] |
||
| 106 | * |
||
| 107 | * @throws Exception |
||
| 108 | * @throws \InvalidArgumentException if 'allowUnknowns' option was not a bool |
||
| 109 | * @throws \InvalidArgumentException if 'defaultRequired' option was not a bool |
||
| 110 | * @throws \InvalidArgumentException if filters for a field was not a array |
||
| 111 | * @throws \InvalidArgumentException if a filter for a field was not a array |
||
| 112 | * @throws \InvalidArgumentException if 'required' for a field was not a bool |
||
| 113 | */ |
||
| 114 | public static function filter(array $spec, array $input, array $options = []) : array |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Return the filter aliases. |
||
| 178 | * |
||
| 179 | * @return array array where keys are aliases and values pass is_callable(). |
||
| 180 | */ |
||
| 181 | public static function getFilterAliases() : array |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set the filter aliases. |
||
| 188 | * |
||
| 189 | * @param array $aliases array where keys are aliases and values pass is_callable(). |
||
| 190 | * @return void |
||
| 191 | * |
||
| 192 | * @throws Exception Thrown if any of the given $aliases is not valid. @see registerAlias() |
||
| 193 | */ |
||
| 194 | public static function setFilterAliases(array $aliases) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Register a new alias with the Filterer |
||
| 210 | * |
||
| 211 | * @param string|int $alias the alias to register |
||
| 212 | * @param callable $filter the aliased callable filter |
||
| 213 | * @param bool $overwrite Flag to overwrite existing alias if it exists |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | * |
||
| 217 | * @throws \InvalidArgumentException if $alias was not a string or int |
||
| 218 | * @throws Exception if $overwrite is false and $alias exists |
||
| 219 | */ |
||
| 220 | public static function registerAlias($alias, callable $filter, bool $overwrite = false) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Filter an array by applying filters to each member |
||
| 229 | * |
||
| 230 | * @param array $values an array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
| 231 | * you pass into Filterer |
||
| 232 | * @param array $filters filters with each specified the same as in @see self::filter. |
||
| 233 | * Eg [['string', false, 2], ['uint']] |
||
| 234 | * |
||
| 235 | * @return array the filtered $values |
||
| 236 | * |
||
| 237 | * @throws FilterException if any member of $values fails filtering |
||
| 238 | */ |
||
| 239 | public static function ofScalars(array $values, array $filters) : array |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Filter an array by applying filters to each member |
||
| 256 | * |
||
| 257 | * @param array $values as array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
| 258 | * you pass into Filterer |
||
| 259 | * @param array $spec spec to apply to each $values member, specified the same as in @see self::filter. |
||
| 260 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
| 261 | * |
||
| 262 | * @return array the filtered $values |
||
| 263 | * |
||
| 264 | * @throws Exception if any member of $values fails filtering |
||
| 265 | */ |
||
| 266 | public static function ofArrays(array $values, array $spec) : array |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Filter $value by using a Filterer $spec and Filterer's default options. |
||
| 294 | * |
||
| 295 | * @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you |
||
| 296 | * pass into Filterer |
||
| 297 | * @param array $spec spec to apply to $value, specified the same as in @see self::filter. |
||
| 298 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
| 299 | * |
||
| 300 | * @return array the filtered $value |
||
| 301 | * |
||
| 302 | * @throws FilterException if $value fails filtering |
||
| 303 | */ |
||
| 304 | public static function ofArray(array $value, array $spec) : array |
||
| 313 | |||
| 314 | private static function assertIfStringOrInt($alias) |
||
| 320 | |||
| 321 | private static function assertIfAliasExists($alias, bool $overwrite) |
||
| 327 | |||
| 328 | private static function checkForUnknowns(array $leftOverInput, array $errors) : array |
||
| 336 | |||
| 337 | private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOverInput, array $errors) : array |
||
| 345 | |||
| 346 | private static function handleRequiredFields(bool $required, string $field, array $errors) : array |
||
| 353 | |||
| 354 | private static function getRequired($filters, $defaultRequired, $field) : bool |
||
| 363 | |||
| 364 | private static function assertFiltersIsAnArray($filters, string $field) |
||
| 370 | |||
| 371 | private static function handleCustomError( |
||
| 391 | |||
| 392 | private static function assertFunctionIsCallable($function, string $field) |
||
| 400 | |||
| 401 | private static function handleFilterAliases($function) |
||
| 409 | |||
| 410 | private static function assertFilterIsNotArray($filter, string $field) |
||
| 416 | |||
| 417 | private static function validateCustomError(array $filters, string $field) |
||
| 431 | |||
| 432 | private static function getAllowUnknowns(array $options) : bool |
||
| 441 | |||
| 442 | private static function getDefaultRequired(array $options) : bool |
||
| 451 | } |
||
| 452 |