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 | 17 | 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 | 6 | 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 array $parameterDefinitions |
||
| 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 | 3 | public function setFunctionParams( array $rawParams, array $parameterDefinitions, array $defaultParams = [] ) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Loops through a list of provided parameters, resolves aliasing and stores errors |
||
| 184 | * for unknown parameters and optionally for parameter overriding. |
||
| 185 | * |
||
| 186 | * @param array $parameters Parameter name as key, parameter value as value |
||
| 187 | * @param IParamDefinition[] $paramDefinitions List of parameter definitions. Either ParamDefinition objects or equivalent arrays. |
||
| 188 | */ |
||
| 189 | 15 | public function setParameters( array $parameters, array $paramDefinitions ) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $message |
||
| 210 | * @param mixed $tags string or array |
||
| 211 | * @param integer $severity |
||
| 212 | */ |
||
| 213 | 4 | private function registerNewError( $message, $tags = [], $severity = ProcessingError::SEVERITY_NORMAL ) { |
|
| 223 | |||
| 224 | 7 | private function registerError( ProcessingError $error ) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Validates and formats all the parameters (but aborts when a fatal error occurs). |
||
| 232 | * |
||
| 233 | * @since 0.4 |
||
| 234 | * @deprecated since 1.0, use processParameters |
||
| 235 | */ |
||
| 236 | public function validateParameters() { |
||
| 239 | |||
| 240 | 11 | public function processParameters(): ProcessingResult { |
|
| 256 | |||
| 257 | 11 | private function newProcessingResult(): ProcessingResult { |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Does the actual parameter processing. |
||
| 293 | */ |
||
| 294 | 11 | private function doParamProcessing() { |
|
| 303 | |||
| 304 | 10 | private function processOneParam() { |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Gets an ordered list of parameters to process. |
||
| 341 | * @throws \UnexpectedValueException |
||
| 342 | */ |
||
| 343 | 11 | private function getParamsToProcess( array $initialParamSet, array $resultingParamSet ) { |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param IParamDefinition[] $paramDefinitions |
||
| 364 | * @param string[] $paramsToHandle |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | 11 | private function getParameterNamesInEvaluationOrder( array $paramDefinitions, array $paramsToHandle ): array { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Tries to find a matching user provided value and, when found, assigns it |
||
| 399 | * to the parameter, and removes it from the raw values. Returns a boolean |
||
| 400 | * indicating if there was any user value set or not. |
||
| 401 | */ |
||
| 402 | 10 | private function attemptToSetUserValue( Param $param ): bool { |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the parameters. |
||
| 423 | * |
||
| 424 | * @since 0.4 |
||
| 425 | * @deprecated since 1.0 |
||
| 426 | * |
||
| 427 | * @return IParam[] |
||
| 428 | */ |
||
| 429 | public function getParameters(): array { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns a single parameter. |
||
| 435 | * |
||
| 436 | * @since 0.4 |
||
| 437 | * @deprecated since 1.0 |
||
| 438 | * |
||
| 439 | * @param string $parameterName The name of the parameter to return |
||
| 440 | * |
||
| 441 | * @return IParam |
||
| 442 | */ |
||
| 443 | public function getParameter( string $parameterName ): IParam { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Returns an associative array with the parameter names as key and their |
||
| 449 | * corresponding values as value. |
||
| 450 | */ |
||
| 451 | public function getParameterValues(): array { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return ProcessingError[] |
||
| 463 | */ |
||
| 464 | 11 | public function getErrors(): array { |
|
| 467 | |||
| 468 | /** |
||
| 469 | * @return string[] |
||
| 470 | */ |
||
| 471 | public function getErrorMessages(): array { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Returns if there where any errors during validation. |
||
| 483 | */ |
||
| 484 | public function hasErrors(): bool { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Returns false when there are no fatal errors or an ProcessingError when one is found. |
||
| 490 | * |
||
| 491 | * @return ProcessingError|boolean false |
||
| 492 | */ |
||
| 493 | 11 | public function hasFatalError() { |
|
| 502 | |||
| 503 | } |
||
| 504 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..