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 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | const DEFAULT_FILTER_ALIASES = [ |
||
17 | 'in' => '\TraderInteractive\Filter\Arrays::in', |
||
18 | 'array' => '\TraderInteractive\Filter\Arrays::filter', |
||
19 | 'bool' => '\TraderInteractive\Filter\Booleans::filter', |
||
20 | 'float' => '\TraderInteractive\Filter\Floats::filter', |
||
21 | 'int' => '\TraderInteractive\Filter\Ints::filter', |
||
22 | 'bool-convert' => '\TraderInteractive\Filter\Booleans::convert', |
||
23 | 'uint' => '\TraderInteractive\Filter\UnsignedInt::filter', |
||
24 | 'string' => '\TraderInteractive\Filter\Strings::filter', |
||
25 | 'ofScalars' => '\TraderInteractive\Filterer::ofScalars', |
||
26 | 'ofArrays' => '\TraderInteractive\Filterer::ofArrays', |
||
27 | 'ofArray' => '\TraderInteractive\Filterer::ofArray', |
||
28 | 'url' => '\TraderInteractive\Filter\Url::filter', |
||
29 | 'email' => '\TraderInteractive\Filter\Email::filter', |
||
30 | 'explode' => '\TraderInteractive\Filter\Strings::explode', |
||
31 | 'flatten' => '\TraderInteractive\Filter\Arrays::flatten', |
||
32 | 'date' => '\TraderInteractive\Filter\DateTime::filter', |
||
33 | 'date-format' => '\TraderInteractive\Filter\DateTime::format', |
||
34 | 'timezone' => '\TraderInteractive\Filter\DateTimeZone::filter', |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $filterAliases = self::DEFAULT_FILTER_ALIASES; |
||
41 | |||
42 | /** |
||
43 | * Example: |
||
44 | * <pre> |
||
45 | * <?php |
||
46 | * class AppendFilter |
||
47 | * { |
||
48 | * public function filter($value, $extraArg) |
||
49 | * { |
||
50 | * return $value . $extraArg; |
||
51 | * } |
||
52 | * } |
||
53 | * $appendFilter = new AppendFilter(); |
||
54 | * |
||
55 | * $trimFunc = function($val) { return trim($val); }; |
||
56 | * |
||
57 | * list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter( |
||
58 | * [ |
||
59 | * 'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']], |
||
60 | * 'field two' => ['required' => true, ['floatval']], |
||
61 | * 'field three' => ['required' => false, ['float']], |
||
62 | * 'field four' => ['required' => true, 'default' => 1, ['uint']], |
||
63 | * ], |
||
64 | * ['field one' => ' abcd', 'field two' => '3.14'] |
||
65 | * ); |
||
66 | * |
||
67 | * var_dump($status); |
||
68 | * var_dump($result); |
||
69 | * var_dump($error); |
||
70 | * var_dump($unknowns); |
||
71 | * </pre> |
||
72 | * prints: |
||
73 | * <pre> |
||
74 | * bool(true) |
||
75 | * array(3) { |
||
76 | * 'field one' => |
||
77 | * string(6) "abcboo" |
||
78 | * 'field two' => |
||
79 | * double(3.14) |
||
80 | * 'field four' => |
||
81 | * int(1) |
||
82 | * } |
||
83 | * NULL |
||
84 | * array(0) { |
||
85 | * } |
||
86 | * </pre> |
||
87 | * |
||
88 | * @param array $spec the specification to apply to the $input. An array where each key is a known input field and |
||
89 | * each value is an array of filters. Each filter should be an array with the first member being |
||
90 | * anything that can pass is_callable() as well as accepting the value to filter as its first |
||
91 | * argument. Two examples would be the string 'trim' or an object function specified like [$obj, |
||
92 | * 'filter'], see is_callable() documentation. The rest of the members are extra arguments to the |
||
93 | * callable. The result of one filter will be the first argument to the next filter. In addition |
||
94 | * to the filters, the specification values may contain a 'required' key (default false) that |
||
95 | * controls the same behavior as the 'defaultRequired' option below but on a per field basis. A |
||
96 | * 'default' specification value may be used to substitute in a default to the $input when the |
||
97 | * key is not present (whether 'required' is specified or not). |
||
98 | * @param array $input the input the apply the $spec on. |
||
99 | * @param array $options 'allowUnknowns' (default false) true to allow unknowns or false to treat as error, |
||
100 | * 'defaultRequired' (default false) true to make fields required by default and treat as |
||
101 | * error on absence and false to allow their absence by default |
||
102 | * |
||
103 | * @return array on success [true, $input filtered, null, array of unknown fields] |
||
104 | * on error [false, null, 'error message', array of unknown fields] |
||
105 | * |
||
106 | * @throws Exception |
||
107 | * @throws \InvalidArgumentException if 'allowUnknowns' option was not a bool |
||
108 | * @throws \InvalidArgumentException if 'defaultRequired' option was not a bool |
||
109 | * @throws \InvalidArgumentException if filters for a field was not a array |
||
110 | * @throws \InvalidArgumentException if a filter for a field was not a array |
||
111 | * @throws \InvalidArgumentException if 'required' for a field was not a bool |
||
112 | */ |
||
113 | public static function filter(array $spec, array $input, array $options = []) : array |
||
174 | |||
175 | /** |
||
176 | * Return the filter aliases. |
||
177 | * |
||
178 | * @return array array where keys are aliases and values pass is_callable(). |
||
179 | */ |
||
180 | public static function getFilterAliases() : array |
||
184 | |||
185 | /** |
||
186 | * Set the filter aliases. |
||
187 | * |
||
188 | * @param array $aliases array where keys are aliases and values pass is_callable(). |
||
189 | * @return void |
||
190 | * |
||
191 | * @throws Exception Thrown if any of the given $aliases is not valid. @see registerAlias() |
||
192 | */ |
||
193 | public static function setFilterAliases(array $aliases) |
||
206 | |||
207 | /** |
||
208 | * Register a new alias with the Filterer |
||
209 | * |
||
210 | * @param string|int $alias the alias to register |
||
211 | * @param callable $filter the aliased callable filter |
||
212 | * @param bool $overwrite Flag to overwrite existing alias if it exists |
||
213 | * |
||
214 | * @return void |
||
215 | * |
||
216 | * @throws \InvalidArgumentException if $alias was not a string or int |
||
217 | * @throws Exception if $overwrite is false and $alias exists |
||
218 | */ |
||
219 | public static function registerAlias($alias, callable $filter, bool $overwrite = false) |
||
225 | |||
226 | /** |
||
227 | * Filter an array by applying filters to each member |
||
228 | * |
||
229 | * @param array $values an array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
230 | * you pass into Filterer |
||
231 | * @param array $filters filters with each specified the same as in @see self::filter. |
||
232 | * Eg [['string', false, 2], ['uint']] |
||
233 | * |
||
234 | * @return array the filtered $values |
||
235 | * |
||
236 | * @throws Exception if any member of $values fails filtering |
||
237 | */ |
||
238 | public static function ofScalars(array $values, array $filters) : array |
||
252 | |||
253 | /** |
||
254 | * Filter an array by applying filters to each member |
||
255 | * |
||
256 | * @param array $values as array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
257 | * you pass into Filterer |
||
258 | * @param array $spec spec to apply to each $values member, specified the same as in @see self::filter. |
||
259 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
260 | * |
||
261 | * @return array the filtered $values |
||
262 | * |
||
263 | * @throws Exception if any member of $values fails filtering |
||
264 | */ |
||
265 | public static function ofArrays(array $values, array $spec) : array |
||
290 | |||
291 | /** |
||
292 | * Filter $value by using a Filterer $spec and Filterer's default options. |
||
293 | * |
||
294 | * @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you |
||
295 | * pass into Filterer |
||
296 | * @param array $spec spec to apply to $value, specified the same as in @see self::filter. |
||
297 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
298 | * |
||
299 | * @return array the filtered $value |
||
300 | * |
||
301 | * @throws Exception if $value fails filtering |
||
302 | */ |
||
303 | public static function ofArray(array $value, array $spec) : array |
||
312 | |||
313 | private static function assertIfStringOrInt($alias) |
||
319 | |||
320 | private static function assertIfAliasExists($alias, bool $overwrite) |
||
326 | |||
327 | private static function checkForUnknowns(array $leftOverInput, array $errors) : array |
||
335 | |||
336 | private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOverInput, array $errors) : array |
||
344 | |||
345 | private static function handleRequiredFields(bool $required, string $field, array $errors) : array |
||
352 | |||
353 | private static function getRequired($filters, $defaultRequired, $field) : bool |
||
362 | |||
363 | private static function assertFiltersIsAnArray($filters, string $field) |
||
369 | |||
370 | private static function handleCustomError( |
||
390 | |||
391 | private static function assertFunctionIsCallable($function, string $field) |
||
399 | |||
400 | private static function handleFilterAliases($function) |
||
408 | |||
409 | private static function assertFilterIsNotArray($filter, string $field) |
||
415 | |||
416 | private static function validateCustomError(array $filters, string $field) |
||
430 | |||
431 | private static function getAllowUnknowns(array $options) : bool |
||
440 | |||
441 | private static function getDefaultRequired(array $options) : bool |
||
450 | } |
||
451 |