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 = []) |
||
194 | |||
195 | /** |
||
196 | * Return the filter aliases. |
||
197 | * |
||
198 | * @return array array where keys are aliases and values pass is_callable(). |
||
199 | */ |
||
200 | public static function getFilterAliases() : array |
||
204 | |||
205 | /** |
||
206 | * Set the filter aliases. |
||
207 | * |
||
208 | * @param array $aliases array where keys are aliases and values pass is_callable(). |
||
209 | * @return void |
||
210 | * |
||
211 | * @throws Exception Thrown if any of the given $aliases is not valid. @see registerAlias() |
||
212 | */ |
||
213 | public static function setFilterAliases(array $aliases) |
||
226 | |||
227 | /** |
||
228 | * Register a new alias with the Filterer |
||
229 | * |
||
230 | * @param string|int $alias the alias to register |
||
231 | * @param callable $filter the aliased callable filter |
||
232 | * @param bool $overwrite Flag to overwrite existing alias if it exists |
||
233 | * |
||
234 | * @return void |
||
235 | * |
||
236 | * @throws \InvalidArgumentException if $alias was not a string or int |
||
237 | * @throws Exception if $overwrite is false and $alias exists |
||
238 | */ |
||
239 | public static function registerAlias($alias, callable $filter, bool $overwrite = false) |
||
245 | |||
246 | /** |
||
247 | * Filter an array by applying filters to each member |
||
248 | * |
||
249 | * @param array $values an array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
250 | * you pass into Filterer |
||
251 | * @param array $filters filters with each specified the same as in @see self::filter. |
||
252 | * Eg [['string', false, 2], ['uint']] |
||
253 | * |
||
254 | * @return array the filtered $values |
||
255 | * |
||
256 | * @throws FilterException if any member of $values fails filtering |
||
257 | */ |
||
258 | public static function ofScalars(array $values, array $filters) : array |
||
272 | |||
273 | /** |
||
274 | * Filter an array by applying filters to each member |
||
275 | * |
||
276 | * @param array $values as array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
277 | * you pass into Filterer |
||
278 | * @param array $spec spec to apply to each $values member, specified the same as in @see self::filter. |
||
279 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
280 | * |
||
281 | * @return array the filtered $values |
||
282 | * |
||
283 | * @throws Exception if any member of $values fails filtering |
||
284 | */ |
||
285 | public static function ofArrays(array $values, array $spec) : array |
||
310 | |||
311 | /** |
||
312 | * Filter $value by using a Filterer $spec and Filterer's default options. |
||
313 | * |
||
314 | * @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you |
||
315 | * pass into Filterer |
||
316 | * @param array $spec spec to apply to $value, specified the same as in @see self::filter. |
||
317 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
318 | * |
||
319 | * @return array the filtered $value |
||
320 | * |
||
321 | * @throws FilterException if $value fails filtering |
||
322 | */ |
||
323 | public static function ofArray(array $value, array $spec) : array |
||
332 | |||
333 | private static function assertIfStringOrInt($alias) |
||
339 | |||
340 | private static function assertIfAliasExists($alias, bool $overwrite) |
||
346 | |||
347 | private static function checkForUnknowns(array $leftOverInput, array $errors) : array |
||
355 | |||
356 | private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOverInput, array $errors) : array |
||
364 | |||
365 | private static function handleRequiredFields(bool $required, string $field, array $errors) : array |
||
372 | |||
373 | private static function getRequired($filters, $defaultRequired, $field) : bool |
||
382 | |||
383 | private static function assertFiltersIsAnArray($filters, string $field) |
||
389 | |||
390 | private static function handleCustomError( |
||
409 | |||
410 | private static function assertFunctionIsCallable($function, string $field) |
||
418 | |||
419 | private static function handleFilterAliases($function) |
||
427 | |||
428 | private static function assertFilterIsNotArray($filter, string $field) |
||
434 | |||
435 | private static function validateCustomError(array &$filters, string $field) |
||
449 | |||
450 | private static function getAllowUnknowns(array $options) : bool |
||
459 | |||
460 | private static function getDefaultRequired(array $options) : bool |
||
469 | |||
470 | /** |
||
471 | * @param string $responseType The type of object that should be returned. |
||
472 | * @param array $filteredValue The filtered input to return. |
||
473 | * @param array $errors The errors to return. |
||
474 | * @param array $unknowns The unknowns to return. |
||
475 | * |
||
476 | * @return array|FilterResponse |
||
477 | * |
||
478 | * @see filter For more information on how responseType is handled and returns are structured. |
||
479 | */ |
||
480 | private static function generateFilterResponse( |
||
498 | } |
||
499 |