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 array |
||
| 46 | */ |
||
| 47 | const DEFAULT_OPTIONS = [ |
||
| 48 | 'allowUnknowns' => false, |
||
| 49 | 'defaultRequired' => false, |
||
| 50 | 'responseType' => self::RESPONSE_TYPE_ARRAY, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | const RESPONSE_TYPE_ARRAY = 'array'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | const RESPONSE_TYPE_FILTER = FilterResponse::class; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $registeredFilterAliases = self::DEFAULT_FILTER_ALIASES; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $filterAliases; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $specification; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private $options; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param array $specification The specification to apply to the value. |
||
| 85 | * @param array $options The options apply during filtering. |
||
| 86 | * @param array|null $filterAliases The filter aliases to accept. |
||
| 87 | * |
||
| 88 | * @see filter For more detailed information on the parameters. |
||
| 89 | */ |
||
| 90 | public function __construct(array $specification, array $options = [], array $filterAliases = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param array $value The value to filter. |
||
| 99 | * |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function __invoke(array $value) : array |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param mixed $value The value to filter. |
||
| 114 | * @param array $specification Specification to merge with the instance specification. |
||
| 115 | * @param array $options Options to merge with the instance options. |
||
| 116 | * |
||
| 117 | * @return array|FilterResponse |
||
| 118 | * |
||
| 119 | * @throws Exception |
||
| 120 | * @see filter For more information on the return types and structure. |
||
| 121 | */ |
||
| 122 | public function execute(array $value, array $specification = [], array $options = []) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Example: |
||
| 184 | * <pre> |
||
| 185 | * <?php |
||
| 186 | * class AppendFilter |
||
| 187 | * { |
||
| 188 | * public function filter($value, $extraArg) |
||
| 189 | * { |
||
| 190 | * return $value . $extraArg; |
||
| 191 | * } |
||
| 192 | * } |
||
| 193 | * $appendFilter = new AppendFilter(); |
||
| 194 | * |
||
| 195 | * $trimFunc = function($val) { return trim($val); }; |
||
| 196 | * |
||
| 197 | * list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter( |
||
| 198 | * [ |
||
| 199 | * 'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']], |
||
| 200 | * 'field two' => ['required' => true, ['floatval']], |
||
| 201 | * 'field three' => ['required' => false, ['float']], |
||
| 202 | * 'field four' => ['required' => true, 'default' => 1, ['uint']], |
||
| 203 | * ], |
||
| 204 | * ['field one' => ' abcd', 'field two' => '3.14'] |
||
| 205 | * ); |
||
| 206 | * |
||
| 207 | * var_dump($status); |
||
| 208 | * var_dump($result); |
||
| 209 | * var_dump($error); |
||
| 210 | * var_dump($unknowns); |
||
| 211 | * </pre> |
||
| 212 | * prints: |
||
| 213 | * <pre> |
||
| 214 | * bool(true) |
||
| 215 | * array(3) { |
||
| 216 | * 'field one' => |
||
| 217 | * string(6) "abcboo" |
||
| 218 | * 'field two' => |
||
| 219 | * double(3.14) |
||
| 220 | * 'field four' => |
||
| 221 | * int(1) |
||
| 222 | * } |
||
| 223 | * NULL |
||
| 224 | * array(0) { |
||
| 225 | * } |
||
| 226 | * </pre> |
||
| 227 | * |
||
| 228 | * @param array $spec the specification to apply to the $input. An array where each key is a known input field and |
||
| 229 | * each value is an array of filters. Each filter should be an array with the first member being |
||
| 230 | * anything that can pass is_callable() as well as accepting the value to filter as its first |
||
| 231 | * argument. Two examples would be the string 'trim' or an object function specified like [$obj, |
||
| 232 | * 'filter'], see is_callable() documentation. The rest of the members are extra arguments to the |
||
| 233 | * callable. The result of one filter will be the first argument to the next filter. In addition |
||
| 234 | * to the filters, the specification values may contain a 'required' key (default false) that |
||
| 235 | * controls the same behavior as the 'defaultRequired' option below but on a per field basis. A |
||
| 236 | * 'default' specification value may be used to substitute in a default to the $input when the |
||
| 237 | * key is not present (whether 'required' is specified or not). |
||
| 238 | * @param array $input the input the apply the $spec on. |
||
| 239 | * @param array $options 'allowUnknowns' (default false) true to allow unknowns or false to treat as error, |
||
| 240 | * 'defaultRequired' (default false) true to make fields required by default and treat as |
||
| 241 | * error on absence and false to allow their absence by default |
||
| 242 | * 'responseType' (default RESPONSE_TYPE_ARRAY) Determines the return type, as described |
||
| 243 | * in the return section. |
||
| 244 | * |
||
| 245 | * @return array|FilterResponse If 'responseType' option is RESPONSE_TYPE_ARRAY: |
||
| 246 | * on success [true, $input filtered, null, array of unknown fields] |
||
| 247 | * on error [false, null, 'error message', array of unknown fields] |
||
| 248 | * If 'responseType' option is RESPONSE_TYPE_FILTER: a FilterResponse instance. |
||
| 249 | * |
||
| 250 | * @throws Exception |
||
| 251 | * @throws InvalidArgumentException if 'allowUnknowns' option was not a bool |
||
| 252 | * @throws InvalidArgumentException if 'defaultRequired' option was not a bool |
||
| 253 | * @throws InvalidArgumentException if 'responseType' option was not a recognized type |
||
| 254 | * @throws InvalidArgumentException if filters for a field was not an array |
||
| 255 | * @throws InvalidArgumentException if a filter for a field was not an array |
||
| 256 | * @throws InvalidArgumentException if 'required' for a field was not a bool |
||
| 257 | */ |
||
| 258 | public static function filter(array $spec, array $input, array $options = []) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return the filter aliases. |
||
| 266 | * |
||
| 267 | * @return array array where keys are aliases and values pass is_callable(). |
||
| 268 | */ |
||
| 269 | public static function getFilterAliases() : array |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the filter aliases. |
||
| 276 | * |
||
| 277 | * @param array $aliases array where keys are aliases and values pass is_callable(). |
||
| 278 | * @return void |
||
| 279 | * |
||
| 280 | * @throws Exception Thrown if any of the given $aliases is not valid. @see registerAlias() |
||
| 281 | */ |
||
| 282 | public static function setFilterAliases(array $aliases) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Register a new alias with the Filterer |
||
| 298 | * |
||
| 299 | * @param string|int $alias the alias to register |
||
| 300 | * @param callable $filter the aliased callable filter |
||
| 301 | * @param bool $overwrite Flag to overwrite existing alias if it exists |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | * |
||
| 305 | * @throws \InvalidArgumentException if $alias was not a string or int |
||
| 306 | * @throws Exception if $overwrite is false and $alias exists |
||
| 307 | */ |
||
| 308 | public static function registerAlias($alias, callable $filter, bool $overwrite = false) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Filter an array by applying filters to each member |
||
| 317 | * |
||
| 318 | * @param array $values an array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
| 319 | * you pass into Filterer |
||
| 320 | * @param array $filters filters with each specified the same as in @see self::filter. |
||
| 321 | * Eg [['string', false, 2], ['uint']] |
||
| 322 | * |
||
| 323 | * @return array the filtered $values |
||
| 324 | * |
||
| 325 | * @throws FilterException if any member of $values fails filtering |
||
| 326 | */ |
||
| 327 | public static function ofScalars(array $values, array $filters) : array |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Filter an array by applying filters to each member |
||
| 344 | * |
||
| 345 | * @param array $values as array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
| 346 | * you pass into Filterer |
||
| 347 | * @param array $spec spec to apply to each $values member, specified the same as in @see self::filter. |
||
| 348 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
| 349 | * |
||
| 350 | * @return array the filtered $values |
||
| 351 | * |
||
| 352 | * @throws Exception if any member of $values fails filtering |
||
| 353 | */ |
||
| 354 | public static function ofArrays(array $values, array $spec) : array |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Filter $value by using a Filterer $spec and Filterer's default options. |
||
| 382 | * |
||
| 383 | * @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you |
||
| 384 | * pass into Filterer |
||
| 385 | * @param array $spec spec to apply to $value, specified the same as in @see self::filter. |
||
| 386 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
| 387 | * |
||
| 388 | * @return array the filtered $value |
||
| 389 | * |
||
| 390 | * @throws FilterException if $value fails filtering |
||
| 391 | */ |
||
| 392 | public static function ofArray(array $value, array $spec) : array |
||
| 401 | |||
| 402 | private static function assertIfStringOrInt($alias) |
||
| 408 | |||
| 409 | private static function assertIfAliasExists($alias, bool $overwrite) |
||
| 415 | |||
| 416 | private static function checkForUnknowns(array $leftOverInput, array $errors) : array |
||
| 424 | |||
| 425 | private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOverInput, array $errors) : array |
||
| 433 | |||
| 434 | private static function handleRequiredFields(bool $required, string $field, array $errors) : array |
||
| 441 | |||
| 442 | private static function getRequired($filters, $defaultRequired, $field) : bool |
||
| 451 | |||
| 452 | private static function assertFiltersIsAnArray($filters, string $field) |
||
| 458 | |||
| 459 | private static function handleCustomError( |
||
| 478 | |||
| 479 | private static function assertFunctionIsCallable($function, string $field) |
||
| 487 | |||
| 488 | private static function handleFilterAliases($function, $filterAliases) |
||
| 496 | |||
| 497 | private static function assertFilterIsNotArray($filter, string $field) |
||
| 503 | |||
| 504 | private static function validateCustomError(array &$filters, string $field) |
||
| 518 | |||
| 519 | private static function getAllowUnknowns(array $options) : bool |
||
| 528 | |||
| 529 | private static function getDefaultRequired(array $options) : bool |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param string $responseType The type of object that should be returned. |
||
| 541 | * @param array $filteredValue The filtered input to return. |
||
| 542 | * @param array $errors The errors to return. |
||
| 543 | * @param array $unknowns The unknowns to return. |
||
| 544 | * |
||
| 545 | * @return array|FilterResponse |
||
| 546 | * |
||
| 547 | * @see filter For more information on how responseType is handled and returns are structured. |
||
| 548 | */ |
||
| 549 | private static function generateFilterResponse( |
||
| 572 | } |
||
| 573 |