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 |
||
| 11 | final class Filterer |
||
| 12 | { |
||
| 13 | private static $filterAliases = [ |
||
| 14 | 'in' => '\TraderInteractive\Filter\Arrays::in', |
||
| 15 | 'array' => '\TraderInteractive\Filter\Arrays::filter', |
||
| 16 | 'bool' => '\TraderInteractive\Filter\Booleans::filter', |
||
| 17 | 'float' => '\TraderInteractive\Filter\Floats::filter', |
||
| 18 | 'int' => '\TraderInteractive\Filter\Ints::filter', |
||
| 19 | 'bool-convert' => '\TraderInteractive\Filter\Booleans::convert', |
||
| 20 | 'uint' => '\TraderInteractive\Filter\UnsignedInt::filter', |
||
| 21 | 'string' => '\TraderInteractive\Filter\Strings::filter', |
||
| 22 | 'ofScalars' => '\TraderInteractive\Filter\Arrays::ofScalars', |
||
| 23 | 'ofArrays' => '\TraderInteractive\Filter\Arrays::ofArrays', |
||
| 24 | 'ofArray' => '\TraderInteractive\Filter\Arrays::ofArray', |
||
| 25 | 'url' => '\TraderInteractive\Filter\Url::filter', |
||
| 26 | 'email' => '\TraderInteractive\Filter\Email::filter', |
||
| 27 | 'explode' => '\TraderInteractive\Filter\Strings::explode', |
||
| 28 | 'flatten' => '\TraderInteractive\Filter\Arrays::flatten', |
||
| 29 | 'date' => '\TraderInteractive\Filter\DateTime::filter', |
||
| 30 | 'date-format' => '\TraderInteractive\Filter\DateTime::format', |
||
| 31 | 'timezone' => '\TraderInteractive\Filter\DateTimeZone::filter', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Example: |
||
| 36 | * <pre> |
||
| 37 | * <?php |
||
| 38 | * class AppendFilter |
||
| 39 | * { |
||
| 40 | * public function filter($value, $extraArg) |
||
| 41 | * { |
||
| 42 | * return $value . $extraArg; |
||
| 43 | * } |
||
| 44 | * } |
||
| 45 | * $appendFilter = new AppendFilter(); |
||
| 46 | * |
||
| 47 | * $trimFunc = function($val) { return trim($val); }; |
||
| 48 | * |
||
| 49 | * list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter( |
||
| 50 | * [ |
||
| 51 | * 'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']], |
||
| 52 | * 'field two' => ['required' => true, ['floatval']], |
||
| 53 | * 'field three' => ['required' => false, ['float']], |
||
| 54 | * 'field four' => ['required' => true, 'default' => 1, ['uint']], |
||
| 55 | * ], |
||
| 56 | * ['field one' => ' abcd', 'field two' => '3.14'] |
||
| 57 | * ); |
||
| 58 | * |
||
| 59 | * var_dump($status); |
||
| 60 | * var_dump($result); |
||
| 61 | * var_dump($error); |
||
| 62 | * var_dump($unknowns); |
||
| 63 | * </pre> |
||
| 64 | * prints: |
||
| 65 | * <pre> |
||
| 66 | * bool(true) |
||
| 67 | * array(3) { |
||
| 68 | * 'field one' => |
||
| 69 | * string(6) "abcboo" |
||
| 70 | * 'field two' => |
||
| 71 | * double(3.14) |
||
| 72 | * 'field four' => |
||
| 73 | * int(1) |
||
| 74 | * } |
||
| 75 | * NULL |
||
| 76 | * array(0) { |
||
| 77 | * } |
||
| 78 | * </pre> |
||
| 79 | * |
||
| 80 | * @param array $spec the specification to apply to the $input. An array where each key is a known input field and |
||
| 81 | * each value is an array of filters. Each filter should be an array with the first member being |
||
| 82 | * anything that can pass is_callable() as well as accepting the value to filter as its first |
||
| 83 | * argument. Two examples would be the string 'trim' or an object function specified like [$obj, |
||
| 84 | * 'filter'], see is_callable() documentation. The rest of the members are extra arguments to the |
||
| 85 | * callable. The result of one filter will be the first argument to the next filter. In addition |
||
| 86 | * to the filters, the specification values may contain a 'required' key (default false) that |
||
| 87 | * controls the same behavior as the 'defaultRequired' option below but on a per field basis. A |
||
| 88 | * 'default' specification value may be used to substitute in a default to the $input when the |
||
| 89 | * key is not present (whether 'required' is specified or not). |
||
| 90 | * @param array $input the input the apply the $spec on. |
||
| 91 | * @param array $options 'allowUnknowns' (default false) true to allow unknowns or false to treat as error, |
||
| 92 | * 'defaultRequired' (default false) true to make fields required by default and treat as |
||
| 93 | * error on absence and false to allow their absence by default |
||
| 94 | * |
||
| 95 | * @return array on success [true, $input filtered, null, array of unknown fields] |
||
| 96 | * on error [false, null, 'error message', array of unknown fields] |
||
| 97 | * |
||
| 98 | * @throws Exception |
||
| 99 | * @throws \InvalidArgumentException if 'allowUnknowns' option was not a bool |
||
| 100 | * @throws \InvalidArgumentException if 'defaultRequired' option was not a bool |
||
| 101 | * @throws \InvalidArgumentException if filters for a field was not a array |
||
| 102 | * @throws \InvalidArgumentException if a filter for a field was not a array |
||
| 103 | * @throws \InvalidArgumentException if 'required' for a field was not a bool |
||
| 104 | */ |
||
| 105 | public static function filter(array $spec, array $input, array $options = []) : array |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the filter aliases. |
||
| 169 | * |
||
| 170 | * @return array array where keys are aliases and values pass is_callable(). |
||
| 171 | */ |
||
| 172 | public static function getFilterAliases() : array |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the filter aliases. |
||
| 179 | * |
||
| 180 | * @param array $aliases array where keys are aliases and values pass is_callable(). |
||
| 181 | * @return void |
||
| 182 | * |
||
| 183 | * @throws Exception Thrown if any of the given $aliases is not valid. @see registerAlias() |
||
| 184 | */ |
||
| 185 | public static function setFilterAliases(array $aliases) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Register a new alias with the Filterer |
||
| 201 | * |
||
| 202 | * @param string|int $alias the alias to register |
||
| 203 | * @param callable $filter the aliased callable filter |
||
| 204 | * @param bool $overwrite Flag to overwrite existing alias if it exists |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | * |
||
| 208 | * @throws \InvalidArgumentException if $alias was not a string or int |
||
| 209 | * @throws Exception if $overwrite is false and $alias exists |
||
| 210 | */ |
||
| 211 | public static function registerAlias($alias, callable $filter, bool $overwrite = false) |
||
| 217 | |||
| 218 | private static function assertIfStringOrInt($alias) |
||
| 224 | |||
| 225 | private static function assertIfAliasExists($alias, bool $overwrite) |
||
| 231 | |||
| 232 | private static function checkForUnknowns(array $leftOverInput, array $errors) : array |
||
| 240 | |||
| 241 | private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOverInput, array $errors) : array |
||
| 249 | |||
| 250 | private static function handleRequiredFields(bool $required, string $field, array $errors) : array |
||
| 257 | |||
| 258 | private static function getRequired($filters, $defaultRequired, $field) : bool |
||
| 267 | |||
| 268 | private static function assertFiltersIsAnArray($filters, string $field) |
||
| 274 | |||
| 275 | private static function handleCustomError( |
||
| 276 | string $customError = null, |
||
| 277 | string $field, |
||
|
|
|||
| 278 | $value, |
||
| 279 | Throwable $e, |
||
| 280 | array $errors |
||
| 281 | ) : array { |
||
| 282 | $error = $customError; |
||
| 356 |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: