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 |
||
16 | class Processor { |
||
17 | |||
18 | /** |
||
19 | * Flag for unnamed default parameters used in Processor::setFunctionParams() to determine that |
||
20 | * a parameter should not have a named fallback. |
||
21 | * |
||
22 | * @since 0.4.13 |
||
23 | */ |
||
24 | const PARAM_UNNAMED = 1; |
||
25 | |||
26 | /** |
||
27 | * @var Param[] |
||
28 | */ |
||
29 | private $params; |
||
30 | |||
31 | /** |
||
32 | * Associative array containing parameter names (keys) and their user-provided data (values). |
||
33 | * This list is needed because additional parameter definitions can be added to the $parameters |
||
34 | * field during validation, so we can't determine in advance if a parameter is unknown. |
||
35 | * @var string[] |
||
36 | */ |
||
37 | private $rawParameters = []; |
||
38 | |||
39 | /** |
||
40 | * Array containing the names of the parameters to handle, ordered by priority. |
||
41 | * @var string[] |
||
42 | */ |
||
43 | private $paramsToHandle = []; |
||
44 | |||
45 | /** |
||
46 | * @var IParamDefinition[] |
||
47 | */ |
||
48 | private $paramDefinitions = []; |
||
49 | |||
50 | /** |
||
51 | * @var ProcessingError[] |
||
52 | */ |
||
53 | private $errors = []; |
||
54 | |||
55 | private $options; |
||
56 | |||
57 | 18 | public function __construct( Options $options ) { |
|
58 | 18 | $this->options = $options; |
|
59 | 18 | } |
|
60 | |||
61 | /** |
||
62 | * Constructs and returns a Validator object based on the default options. |
||
63 | */ |
||
64 | 7 | public static function newDefault(): self { |
|
65 | 7 | return new Processor( new Options() ); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Constructs and returns a Validator object based on the provided options. |
||
70 | */ |
||
71 | 11 | public static function newFromOptions( Options $options ): self { |
|
72 | 11 | return new Processor( $options ); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns the options used by this Validator object. |
||
77 | */ |
||
78 | 1 | public function getOptions(): Options { |
|
81 | |||
82 | /** |
||
83 | * Determines the names and values of all parameters. Also takes care of default parameters. |
||
84 | * After that the resulting parameter list is passed to Processor::setParameters |
||
85 | * |
||
86 | * @since 0.4 |
||
87 | * |
||
88 | * @param string[] $rawParams |
||
89 | * @param ParamDefinition[]|array[] $parameterDefinitions DEPRECATED! Use @see setParameterDefinitions instead |
||
90 | * @param array $defaultParams array of strings or array of arrays to define which parameters can be used unnamed. |
||
91 | * The second value in array-form is reserved for flags. Currently, Processor::PARAM_UNNAMED determines that |
||
92 | * the parameter has no name which can be used to set it. Therefore all these parameters must be set before |
||
93 | * any named parameter. The effect is, that '=' within the string won't confuse the parameter anymore like |
||
94 | * it would happen with default parameters that still have a name as well. |
||
95 | */ |
||
96 | 4 | public function setFunctionParams( array $rawParams, array $parameterDefinitions = [], array $defaultParams = [] ) { |
|
169 | |||
170 | /** |
||
171 | * @since 1.6.0 |
||
172 | * @param ParamDefinition[] $paramDefinitions |
||
173 | */ |
||
174 | 1 | public function setParameterDefinitions( array $paramDefinitions ) { |
|
177 | |||
178 | /** |
||
179 | * Loops through a list of provided parameters, resolves aliasing and stores errors |
||
180 | * for unknown parameters and optionally for parameter overriding. |
||
181 | * |
||
182 | * @param array $parameters Parameter name as key, parameter value as value |
||
183 | * @param ParamDefinition[]|array[] $paramDefinitions DEPRECATED! Use @see setParameterDefinitions instead |
||
184 | */ |
||
185 | 16 | public function setParameters( array $parameters, array $paramDefinitions = [] ) { |
|
203 | |||
204 | /** |
||
205 | * @param string $message |
||
206 | * @param string[] $tags |
||
207 | * @param integer $severity |
||
208 | */ |
||
209 | 5 | private function registerNewError( string $message, array $tags = [], int $severity = ProcessingError::SEVERITY_NORMAL ) { |
|
219 | |||
220 | 8 | private function registerError( ProcessingError $error ) { |
|
225 | |||
226 | /** |
||
227 | * Validates and formats all the parameters (but aborts when a fatal error occurs). |
||
228 | * |
||
229 | * @since 0.4 |
||
230 | * @deprecated since 1.0, use processParameters |
||
231 | */ |
||
232 | public function validateParameters() { |
||
235 | |||
236 | 12 | public function processParameters(): ProcessingResult { |
|
252 | |||
253 | 12 | private function newProcessingResult(): ProcessingResult { |
|
283 | |||
284 | 12 | private function doParamProcessing() { |
|
293 | |||
294 | 11 | private function processOneParam() { |
|
328 | |||
329 | /** |
||
330 | * Gets an ordered list of parameters to process. |
||
331 | * @throws \UnexpectedValueException |
||
332 | */ |
||
333 | 12 | private function getParamsToProcess( array $initialParamSet, array $resultingParamSet ) { |
|
351 | |||
352 | /** |
||
353 | * @param IParamDefinition[] $paramDefinitions |
||
354 | * @param string[] $paramsToHandle |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | 12 | private function getParameterNamesInEvaluationOrder( array $paramDefinitions, array $paramsToHandle ): array { |
|
386 | |||
387 | /** |
||
388 | * Tries to find a matching user provided value and, when found, assigns it |
||
389 | * to the parameter, and removes it from the raw values. Returns a boolean |
||
390 | * indicating if there was any user value set or not. |
||
391 | */ |
||
392 | 11 | private function attemptToSetUserValue( Param $param ): bool { |
|
410 | |||
411 | /** |
||
412 | * @deprecated since 1.0 |
||
413 | * @return Param[] |
||
414 | */ |
||
415 | public function getParameters(): array { |
||
418 | |||
419 | /** |
||
420 | * @deprecated since 1.0 |
||
421 | */ |
||
422 | public function getParameter( string $parameterName ): Param { |
||
425 | |||
426 | /** |
||
427 | * Returns an associative array with the parameter names as key and their |
||
428 | * corresponding values as value. |
||
429 | * @deprecated since 1.7 - use processParameters() return value |
||
430 | */ |
||
431 | public function getParameterValues(): array { |
||
440 | |||
441 | /** |
||
442 | * @deprecated since 1.7 - use processParameters() return value |
||
443 | * @return ProcessingError[] |
||
444 | */ |
||
445 | 12 | public function getErrors(): array { |
|
448 | |||
449 | /** |
||
450 | * @deprecated since 1.7 - use processParameters() return value |
||
451 | * @return string[] |
||
452 | */ |
||
453 | public function getErrorMessages(): array { |
||
462 | |||
463 | /** |
||
464 | * @deprecated since 1.7 - use processParameters() return value |
||
465 | */ |
||
466 | public function hasErrors(): bool { |
||
469 | |||
470 | /** |
||
471 | * @deprecated since 1.7 - use processParameters() return value |
||
472 | * @return ProcessingError|boolean false |
||
473 | */ |
||
474 | 12 | public function hasFatalError() { |
|
483 | |||
484 | } |
||
485 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: