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 |
||
13 | final class Filterer |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | const DEFAULT_FILTER_ALIASES = [ |
||
19 | 'array' => '\\TraderInteractive\\Filter\\Arrays::filter', |
||
20 | 'arrayize' => '\\TraderInteractive\\Filter\\Arrays::arrayize', |
||
21 | 'bool' => '\\TraderInteractive\\Filter\\Booleans::filter', |
||
22 | 'bool-convert' => '\\TraderInteractive\\Filter\\Booleans::convert', |
||
23 | 'concat' => '\\TraderInteractive\\Filter\\Strings::concat', |
||
24 | 'date' => '\\TraderInteractive\\Filter\\DateTime::filter', |
||
25 | 'date-format' => '\\TraderInteractive\\Filter\\DateTime::format', |
||
26 | 'email' => '\\TraderInteractive\\Filter\\Email::filter', |
||
27 | 'explode' => '\\TraderInteractive\\Filter\\Strings::explode', |
||
28 | 'flatten' => '\\TraderInteractive\\Filter\\Arrays::flatten', |
||
29 | 'float' => '\\TraderInteractive\\Filter\\Floats::filter', |
||
30 | 'in' => '\\TraderInteractive\\Filter\\Arrays::in', |
||
31 | 'int' => '\\TraderInteractive\\Filter\\Ints::filter', |
||
32 | 'ofArray' => '\\TraderInteractive\\Filterer::ofArray', |
||
33 | 'ofArrays' => '\\TraderInteractive\\Filterer::ofArrays', |
||
34 | 'ofScalars' => '\\TraderInteractive\\Filterer::ofScalars', |
||
35 | 'redact' => '\\TraderInteractive\\Filter\\Strings::redact', |
||
36 | 'string' => '\\TraderInteractive\\Filter\\Strings::filter', |
||
37 | 'strip-tags' => '\\TraderInteractive\\Filter\\Strings::stripTags', |
||
38 | 'timezone' => '\\TraderInteractive\\Filter\\DateTimeZone::filter', |
||
39 | 'translate' => '\\TraderInteractive\\Filter\\Strings::translate', |
||
40 | 'uint' => '\\TraderInteractive\\Filter\\UnsignedInt::filter', |
||
41 | 'url' => '\\TraderInteractive\\Filter\\Url::filter', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | const RESPONSE_TYPE_ARRAY = 'array'; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | const RESPONSE_TYPE_FILTER = FilterResponse::class; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private static $filterAliases = self::DEFAULT_FILTER_ALIASES; |
||
58 | |||
59 | /** |
||
60 | * Example: |
||
61 | * <pre> |
||
62 | * <?php |
||
63 | * class AppendFilter |
||
64 | * { |
||
65 | * public function filter($value, $extraArg) |
||
66 | * { |
||
67 | * return $value . $extraArg; |
||
68 | * } |
||
69 | * } |
||
70 | * $appendFilter = new AppendFilter(); |
||
71 | * |
||
72 | * $trimFunc = function($val) { return trim($val); }; |
||
73 | * |
||
74 | * list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter( |
||
75 | * [ |
||
76 | * 'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']], |
||
77 | * 'field two' => ['required' => true, ['floatval']], |
||
78 | * 'field three' => ['required' => false, ['float']], |
||
79 | * 'field four' => ['required' => true, 'default' => 1, ['uint']], |
||
80 | * ], |
||
81 | * ['field one' => ' abcd', 'field two' => '3.14'] |
||
82 | * ); |
||
83 | * |
||
84 | * var_dump($status); |
||
85 | * var_dump($result); |
||
86 | * var_dump($error); |
||
87 | * var_dump($unknowns); |
||
88 | * </pre> |
||
89 | * prints: |
||
90 | * <pre> |
||
91 | * bool(true) |
||
92 | * array(3) { |
||
93 | * 'field one' => |
||
94 | * string(6) "abcboo" |
||
95 | * 'field two' => |
||
96 | * double(3.14) |
||
97 | * 'field four' => |
||
98 | * int(1) |
||
99 | * } |
||
100 | * NULL |
||
101 | * array(0) { |
||
102 | * } |
||
103 | * </pre> |
||
104 | * |
||
105 | * @param array $spec the specification to apply to the $input. An array where each key is a known input field and |
||
106 | * each value is an array of filters. Each filter should be an array with the first member being |
||
107 | * anything that can pass is_callable() as well as accepting the value to filter as its first |
||
108 | * argument. Two examples would be the string 'trim' or an object function specified like [$obj, |
||
109 | * 'filter'], see is_callable() documentation. The rest of the members are extra arguments to the |
||
110 | * callable. The result of one filter will be the first argument to the next filter. In addition |
||
111 | * to the filters, the specification values may contain a 'required' key (default false) that |
||
112 | * controls the same behavior as the 'defaultRequired' option below but on a per field basis. A |
||
113 | * 'default' specification value may be used to substitute in a default to the $input when the |
||
114 | * key is not present (whether 'required' is specified or not). |
||
115 | * @param array $input the input the apply the $spec on. |
||
116 | * @param array $options 'allowUnknowns' (default false) true to allow unknowns or false to treat as error, |
||
117 | * 'defaultRequired' (default false) true to make fields required by default and treat as |
||
118 | * error on absence and false to allow their absence by default |
||
119 | * 'responseType' (default RESPONSE_TYPE_ARRAY) Determines the return type, as described |
||
120 | * in the return section. |
||
121 | * |
||
122 | * @return array|FilterResponse If 'responseType' option is RESPONSE_TYPE_ARRAY: |
||
123 | * on success [true, $input filtered, null, array of unknown fields] |
||
124 | * on error [false, null, 'error message', array of unknown fields] |
||
125 | * If 'responseType' option is RESPONSE_TYPE_FILTER: a FilterResponse instance. |
||
126 | * |
||
127 | * @throws Exception |
||
128 | * @throws InvalidArgumentException if 'allowUnknowns' option was not a bool |
||
129 | * @throws InvalidArgumentException if 'defaultRequired' option was not a bool |
||
130 | * @throws InvalidArgumentException if 'responseType' option was not a recognized type |
||
131 | * @throws InvalidArgumentException if filters for a field was not an array |
||
132 | * @throws InvalidArgumentException if a filter for a field was not an array |
||
133 | * @throws InvalidArgumentException if 'required' for a field was not a bool |
||
134 | */ |
||
135 | public static function filter(array $spec, array $input, array $options = []) |
||
193 | |||
194 | /** |
||
195 | * Return the filter aliases. |
||
196 | * |
||
197 | * @return array array where keys are aliases and values pass is_callable(). |
||
198 | */ |
||
199 | public static function getFilterAliases() : array |
||
203 | |||
204 | /** |
||
205 | * Set the filter aliases. |
||
206 | * |
||
207 | * @param array $aliases array where keys are aliases and values pass is_callable(). |
||
208 | * @return void |
||
209 | * |
||
210 | * @throws Exception Thrown if any of the given $aliases is not valid. @see registerAlias() |
||
211 | */ |
||
212 | public static function setFilterAliases(array $aliases) |
||
225 | |||
226 | /** |
||
227 | * Register a new alias with the Filterer |
||
228 | * |
||
229 | * @param string|int $alias the alias to register |
||
230 | * @param callable $filter the aliased callable filter |
||
231 | * @param bool $overwrite Flag to overwrite existing alias if it exists |
||
232 | * |
||
233 | * @return void |
||
234 | * |
||
235 | * @throws \InvalidArgumentException if $alias was not a string or int |
||
236 | * @throws Exception if $overwrite is false and $alias exists |
||
237 | */ |
||
238 | public static function registerAlias($alias, callable $filter, bool $overwrite = false) |
||
244 | |||
245 | /** |
||
246 | * Filter an array by applying filters to each member |
||
247 | * |
||
248 | * @param array $values an array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
249 | * you pass into Filterer |
||
250 | * @param array $filters filters with each specified the same as in @see self::filter. |
||
251 | * Eg [['string', false, 2], ['uint']] |
||
252 | * |
||
253 | * @return array the filtered $values |
||
254 | * |
||
255 | * @throws FilterException if any member of $values fails filtering |
||
256 | */ |
||
257 | public static function ofScalars(array $values, array $filters) : array |
||
271 | |||
272 | /** |
||
273 | * Filter an array by applying filters to each member |
||
274 | * |
||
275 | * @param array $values as array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
276 | * you pass into Filterer |
||
277 | * @param array $spec spec to apply to each $values member, specified the same as in @see self::filter. |
||
278 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
279 | * |
||
280 | * @return array the filtered $values |
||
281 | * |
||
282 | * @throws Exception if any member of $values fails filtering |
||
283 | */ |
||
284 | public static function ofArrays(array $values, array $spec) : array |
||
309 | |||
310 | /** |
||
311 | * Filter $value by using a Filterer $spec and Filterer's default options. |
||
312 | * |
||
313 | * @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you |
||
314 | * pass into Filterer |
||
315 | * @param array $spec spec to apply to $value, specified the same as in @see self::filter. |
||
316 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
317 | * |
||
318 | * @return array the filtered $value |
||
319 | * |
||
320 | * @throws FilterException if $value fails filtering |
||
321 | */ |
||
322 | public static function ofArray(array $value, array $spec) : array |
||
331 | |||
332 | private static function assertIfStringOrInt($alias) |
||
338 | |||
339 | private static function assertIfAliasExists($alias, bool $overwrite) |
||
345 | |||
346 | private static function checkForUnknowns(array $leftOverInput, array $errors) : array |
||
354 | |||
355 | private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOverInput, array $errors) : array |
||
363 | |||
364 | private static function handleRequiredFields(bool $required, string $field, array $errors) : array |
||
371 | |||
372 | private static function getRequired($filters, $defaultRequired, $field) : bool |
||
381 | |||
382 | private static function assertFiltersIsAnArray($filters, string $field) |
||
388 | |||
389 | private static function handleCustomError( |
||
408 | |||
409 | private static function assertFunctionIsCallable($function, string $field) |
||
417 | |||
418 | private static function handleFilterAliases($function) |
||
426 | |||
427 | private static function assertFilterIsNotArray($filter, string $field) |
||
433 | |||
434 | private static function validateCustomError(array &$filters, string $field) |
||
448 | |||
449 | private static function getAllowUnknowns(array $options) : bool |
||
458 | |||
459 | private static function getDefaultRequired(array $options) : bool |
||
468 | |||
469 | /** |
||
470 | * @param string $responseType The type of object that should be returned. |
||
471 | * @param array $filteredValue The filtered input to return. |
||
472 | * @param array $errors The errors to return. |
||
473 | * @param array $unknowns The unknowns to return. |
||
474 | * |
||
475 | * @return array|FilterResponse |
||
476 | * |
||
477 | * @see filter For more information on how responseType is handled and returns are structured. |
||
478 | */ |
||
479 | private static function generateFilterResponse( |
||
502 | } |
||
503 |