Complex classes like Processor 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 Processor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Processor { |
||
15 | |||
16 | /** |
||
17 | * Flag for unnamed default parameters used in Processor::setFunctionParams() to determine that |
||
18 | * a parameter should not have a named fallback. |
||
19 | * |
||
20 | * @since 0.4.13 |
||
21 | */ |
||
22 | const PARAM_UNNAMED = 1; |
||
23 | |||
24 | /** |
||
25 | * @var Param[] |
||
26 | */ |
||
27 | private $params; |
||
28 | |||
29 | /** |
||
30 | * Associative array containing parameter names (keys) and their user-provided data (values). |
||
31 | * This list is needed because additional parameter definitions can be added to the $parameters |
||
32 | * field during validation, so we can't determine in advance if a parameter is unknown. |
||
33 | * @var string[] |
||
34 | */ |
||
35 | private $rawParameters = []; |
||
36 | |||
37 | /** |
||
38 | * Array containing the names of the parameters to handle, ordered by priority. |
||
39 | * @var string[] |
||
40 | */ |
||
41 | private $paramsToHandle = []; |
||
42 | |||
43 | /** |
||
44 | * @var IParamDefinition[] |
||
45 | */ |
||
46 | private $paramDefinitions = []; |
||
47 | |||
48 | /** |
||
49 | * @var ProcessingError[] |
||
50 | */ |
||
51 | private $errors = []; |
||
52 | |||
53 | private $options; |
||
54 | |||
55 | 18 | public function __construct( Options $options ) { |
|
58 | |||
59 | /** |
||
60 | * Constructs and returns a Validator object based on the default options. |
||
61 | * |
||
62 | * @since 1.0 |
||
63 | * |
||
64 | * @return Processor |
||
65 | */ |
||
66 | 7 | public static function newDefault(): self { |
|
69 | |||
70 | /** |
||
71 | * Constructs and returns a Validator object based on the provided options. |
||
72 | * |
||
73 | * @since 1.0 |
||
74 | * |
||
75 | * @param Options $options |
||
76 | * |
||
77 | * @return Processor |
||
78 | */ |
||
79 | 11 | public static function newFromOptions( Options $options ): self { |
|
82 | |||
83 | /** |
||
84 | * Returns the options used by this Validator object. |
||
85 | * |
||
86 | * @since 1.0 |
||
87 | * |
||
88 | * @return Options |
||
89 | */ |
||
90 | 1 | public function getOptions(): Options { |
|
93 | |||
94 | /** |
||
95 | * Determines the names and values of all parameters. Also takes care of default parameters. |
||
96 | * After that the resulting parameter list is passed to Processor::setParameters |
||
97 | * |
||
98 | * @since 0.4 |
||
99 | * |
||
100 | * @param string[] $rawParams |
||
101 | * @param ParamDefinition[]|array[] $parameterDefinitions DEPRECATED! Use @see setParameterDefinitions instead |
||
102 | * @param array $defaultParams array of strings or array of arrays to define which parameters can be used unnamed. |
||
103 | * The second value in array-form is reserved for flags. Currently, Processor::PARAM_UNNAMED determines that |
||
104 | * the parameter has no name which can be used to set it. Therefore all these parameters must be set before |
||
105 | * any named parameter. The effect is, that '=' within the string won't confuse the parameter anymore like |
||
106 | * it would happen with default parameters that still have a name as well. |
||
107 | */ |
||
108 | 4 | public function setFunctionParams( array $rawParams, array $parameterDefinitions = [], array $defaultParams = [] ) { |
|
181 | |||
182 | /** |
||
183 | * @since 1.6.0 |
||
184 | * @param ParamDefinition[] $paramDefinitions |
||
185 | */ |
||
186 | 1 | public function setParameterDefinitions( array $paramDefinitions ) { |
|
189 | |||
190 | /** |
||
191 | * Loops through a list of provided parameters, resolves aliasing and stores errors |
||
192 | * for unknown parameters and optionally for parameter overriding. |
||
193 | * |
||
194 | * @param array $parameters Parameter name as key, parameter value as value |
||
195 | * @param ParamDefinition[]|array[] $paramDefinitions DEPRECATED! Use @see setParameterDefinitions instead |
||
196 | */ |
||
197 | 16 | public function setParameters( array $parameters, array $paramDefinitions = [] ) { |
|
215 | |||
216 | /** |
||
217 | * @param string $message |
||
218 | * @param mixed $tags string or array |
||
219 | * @param integer $severity |
||
220 | */ |
||
221 | 5 | private function registerNewError( $message, $tags = [], $severity = ProcessingError::SEVERITY_NORMAL ) { |
|
231 | |||
232 | 8 | private function registerError( ProcessingError $error ) { |
|
237 | |||
238 | /** |
||
239 | * Validates and formats all the parameters (but aborts when a fatal error occurs). |
||
240 | * |
||
241 | * @since 0.4 |
||
242 | * @deprecated since 1.0, use processParameters |
||
243 | */ |
||
244 | public function validateParameters() { |
||
247 | |||
248 | 12 | public function processParameters(): ProcessingResult { |
|
264 | |||
265 | 12 | private function newProcessingResult(): ProcessingResult { |
|
295 | |||
296 | /** |
||
297 | * Does the actual parameter processing. |
||
298 | */ |
||
299 | 12 | private function doParamProcessing() { |
|
308 | |||
309 | 11 | private function processOneParam() { |
|
343 | |||
344 | /** |
||
345 | * Gets an ordered list of parameters to process. |
||
346 | * @throws \UnexpectedValueException |
||
347 | */ |
||
348 | 12 | private function getParamsToProcess( array $initialParamSet, array $resultingParamSet ) { |
|
366 | |||
367 | /** |
||
368 | * @param IParamDefinition[] $paramDefinitions |
||
369 | * @param string[] $paramsToHandle |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | 12 | private function getParameterNamesInEvaluationOrder( array $paramDefinitions, array $paramsToHandle ): array { |
|
401 | |||
402 | /** |
||
403 | * Tries to find a matching user provided value and, when found, assigns it |
||
404 | * to the parameter, and removes it from the raw values. Returns a boolean |
||
405 | * indicating if there was any user value set or not. |
||
406 | */ |
||
407 | 11 | private function attemptToSetUserValue( Param $param ): bool { |
|
425 | |||
426 | /** |
||
427 | * Returns the parameters. |
||
428 | * |
||
429 | * @since 0.4 |
||
430 | * @deprecated since 1.0 |
||
431 | * |
||
432 | * @return IParam[] |
||
433 | */ |
||
434 | public function getParameters(): array { |
||
437 | |||
438 | /** |
||
439 | * Returns a single parameter. |
||
440 | * |
||
441 | * @since 0.4 |
||
442 | * @deprecated since 1.0 |
||
443 | * |
||
444 | * @param string $parameterName The name of the parameter to return |
||
445 | * |
||
446 | * @return IParam |
||
447 | */ |
||
448 | public function getParameter( string $parameterName ): IParam { |
||
451 | |||
452 | /** |
||
453 | * Returns an associative array with the parameter names as key and their |
||
454 | * corresponding values as value. |
||
455 | */ |
||
456 | public function getParameterValues(): array { |
||
465 | |||
466 | /** |
||
467 | * @return ProcessingError[] |
||
468 | */ |
||
469 | 12 | public function getErrors(): array { |
|
472 | |||
473 | /** |
||
474 | * @return string[] |
||
475 | */ |
||
476 | public function getErrorMessages(): array { |
||
485 | |||
486 | /** |
||
487 | * Returns if there where any errors during validation. |
||
488 | */ |
||
489 | public function hasErrors(): bool { |
||
492 | |||
493 | /** |
||
494 | * Returns false when there are no fatal errors or an ProcessingError when one is found. |
||
495 | * |
||
496 | * @return ProcessingError|boolean false |
||
497 | */ |
||
498 | 12 | public function hasFatalError() { |
|
507 | |||
508 | } |
||
509 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: