Complex classes like Param 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 Param, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Param implements IParam { |
||
|
|||
27 | |||
28 | /** |
||
29 | * Indicates whether parameters not found in the criteria list |
||
30 | * should be stored in case they are not accepted. The default is false. |
||
31 | * |
||
32 | * @deprecated since 1.7 |
||
33 | */ |
||
34 | public static $accumulateParameterErrors = false; |
||
35 | |||
36 | /** |
||
37 | * The original parameter name as provided by the user. This can be the |
||
38 | * main name or an alias. |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $originalName; |
||
42 | |||
43 | /** |
||
44 | * The original value as provided by the user. This is mainly retained for |
||
45 | * usage in error messages when the parameter turns out to be invalid. |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $originalValue; |
||
49 | |||
50 | /** |
||
51 | * @var mixed |
||
52 | */ |
||
53 | protected $value; |
||
54 | |||
55 | /** |
||
56 | * Keeps track of how many times the parameter has been set by the user. |
||
57 | * This is used to detect overrides and for figuring out a parameter is missing. |
||
58 | * @var integer |
||
59 | */ |
||
60 | protected $setCount = 0; |
||
61 | |||
62 | /** |
||
63 | * @var ProcessingError[] |
||
64 | */ |
||
65 | protected $errors = []; |
||
66 | |||
67 | /** |
||
68 | * Indicates if the parameter was set to it's default. |
||
69 | * @var boolean |
||
70 | */ |
||
71 | protected $defaulted = false; |
||
72 | |||
73 | /** |
||
74 | * @var ParamDefinition |
||
75 | */ |
||
76 | protected $definition; |
||
77 | |||
78 | 107 | public function __construct( IParamDefinition $definition ) { |
|
81 | |||
82 | /** |
||
83 | * Sets and cleans the original value and name. |
||
84 | */ |
||
85 | 105 | public function setUserValue( string $paramName, $paramValue, Options $options ): bool { |
|
100 | |||
101 | /** |
||
102 | * @param mixed $value |
||
103 | */ |
||
104 | 93 | public function setValue( $value ) { |
|
107 | |||
108 | /** |
||
109 | * Sets the $value to a cleaned value of $originalValue. |
||
110 | */ |
||
111 | 105 | protected function cleanValue( Options $options ) { |
|
127 | |||
128 | 105 | private function shouldTrim( Options $options ): bool { |
|
137 | |||
138 | 105 | private function trimValue() { |
|
150 | |||
151 | 105 | private function shouldLowercase( Options $options ): bool { |
|
160 | |||
161 | private function lowercaseValue() { |
||
173 | |||
174 | /** |
||
175 | * Parameter processing entry point. |
||
176 | * Processes the parameter. This includes parsing, validation and additional formatting. |
||
177 | * |
||
178 | * @param ParamDefinition[] $definitions |
||
179 | * @param Param[] $params |
||
180 | * @param Options $options |
||
181 | * |
||
182 | * @throws Exception |
||
183 | */ |
||
184 | 105 | public function process( array &$definitions, array $params, Options $options ) { |
|
202 | |||
203 | 104 | public function getValueParser( Options $options ): ValueParser { |
|
217 | |||
218 | 104 | protected function parseAndValidate( Options $options ) { |
|
244 | |||
245 | /** |
||
246 | * Parses and validates the provided with with specified parser. |
||
247 | * The result is returned in an array on success. On fail, false is returned. |
||
248 | * The result is wrapped in an array since we need to be able to distinguish |
||
249 | * between the method returning false and the value being false. |
||
250 | * |
||
251 | * Parsing and validation errors get added to $this->errors. |
||
252 | * |
||
253 | * @since 1.0 |
||
254 | * |
||
255 | * @param ValueParser $parser |
||
256 | * @param mixed $value |
||
257 | * |
||
258 | * @return array|bool |
||
259 | */ |
||
260 | 104 | protected function parseAndValidateValue( ValueParser $parser, $value ) { |
|
277 | |||
278 | 35 | protected function registerProcessingError( string $message ) { |
|
281 | |||
282 | 35 | protected function newProcessingError( string $message ): ProcessingError { |
|
286 | |||
287 | /** |
||
288 | * @param mixed $value |
||
289 | */ |
||
290 | 99 | protected function validateValue( $value ) { |
|
310 | |||
311 | /** |
||
312 | * Sets the parameter value to the default if needed. |
||
313 | */ |
||
314 | 104 | protected function setToDefaultIfNeeded() { |
|
319 | |||
320 | 104 | private function shouldSetToDefault(): bool { |
|
331 | |||
332 | /** |
||
333 | * Returns the original use-provided name. |
||
334 | * |
||
335 | * @throws Exception |
||
336 | * @return string |
||
337 | */ |
||
338 | 36 | public function getOriginalName(): string { |
|
339 | 36 | if ( $this->setCount == 0 ) { |
|
340 | throw new Exception( 'No user input set to the parameter yet, so the original name does not exist' ); |
||
341 | } |
||
342 | 36 | return $this->originalName; |
|
343 | } |
||
344 | |||
345 | /** |
||
346 | * Returns the original use-provided value. |
||
347 | * |
||
348 | * @throws Exception |
||
349 | * @return mixed |
||
350 | */ |
||
351 | 36 | public function getOriginalValue() { |
|
352 | 36 | if ( $this->setCount == 0 ) { |
|
353 | throw new Exception( 'No user input set to the parameter yet, so the original value does not exist' ); |
||
354 | } |
||
355 | 36 | return $this->originalValue; |
|
356 | } |
||
357 | |||
358 | /** |
||
359 | * Returns all validation errors that occurred so far. |
||
360 | * |
||
361 | * @return ProcessingError[] |
||
362 | */ |
||
363 | 95 | public function getErrors(): array { |
|
366 | |||
367 | /** |
||
368 | * Sets the parameter value to the default. |
||
369 | */ |
||
370 | 16 | protected function setToDefault() { |
|
374 | |||
375 | 49 | public function wasSetToDefault(): bool { |
|
376 | 49 | return $this->defaulted; |
|
377 | } |
||
378 | |||
379 | 105 | public function hasFatalError(): bool { |
|
388 | |||
389 | /** |
||
390 | * Returns the ParamDefinition this Param was constructed from. |
||
391 | */ |
||
392 | public function getDefinition(): ParamDefinition { |
||
395 | |||
396 | /** |
||
397 | * @return mixed |
||
398 | */ |
||
399 | 105 | public function &getValue() { |
|
402 | |||
403 | 36 | public function isRequired(): bool { |
|
406 | |||
407 | 49 | public function getName(): string { |
|
408 | 49 | return $this->definition->getName(); |
|
409 | } |
||
410 | |||
411 | /** |
||
412 | * @return string[] |
||
413 | */ |
||
414 | public function getAliases(): array { |
||
417 | |||
418 | } |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.