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 |
||
21 | final class Param implements IParam { |
||
|
|||
22 | |||
23 | /** |
||
24 | * Indicates whether parameters not found in the criteria list |
||
25 | * should be stored in case they are not accepted. The default is false. |
||
26 | * |
||
27 | * @since 1.0 |
||
28 | * |
||
29 | * @var boolean |
||
30 | */ |
||
31 | public static $accumulateParameterErrors = false; |
||
32 | |||
33 | /** |
||
34 | * The original parameter name as provided by the user. This can be the |
||
35 | * main name or an alias. |
||
36 | * |
||
37 | * @since 1.0 |
||
38 | * |
||
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 | * |
||
47 | * @since 1.0 |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $originalValue; |
||
52 | |||
53 | /** |
||
54 | * The value of the parameter. |
||
55 | * |
||
56 | * @since 1.0 |
||
57 | * |
||
58 | * @var mixed |
||
59 | */ |
||
60 | protected $value; |
||
61 | |||
62 | /** |
||
63 | * Keeps track of how many times the parameter has been set by the user. |
||
64 | * This is used to detect overrides and for figuring out a parameter is missing. |
||
65 | * |
||
66 | * @since 1.0 |
||
67 | * |
||
68 | * @var integer |
||
69 | */ |
||
70 | protected $setCount = 0; |
||
71 | |||
72 | /** |
||
73 | * List of validation errors for this parameter. |
||
74 | * |
||
75 | * @since 1.0 |
||
76 | * |
||
77 | * @var array of ProcessingError |
||
78 | */ |
||
79 | protected $errors = []; |
||
80 | |||
81 | /** |
||
82 | * Indicates if the parameter was set to it's default. |
||
83 | * |
||
84 | * @since 1.0 |
||
85 | * |
||
86 | * @var boolean |
||
87 | */ |
||
88 | protected $defaulted = false; |
||
89 | |||
90 | /** |
||
91 | * @since 1.0 |
||
92 | * |
||
93 | * @var ParamDefinition |
||
94 | */ |
||
95 | protected $definition; |
||
96 | |||
97 | /** |
||
98 | * Constructor. |
||
99 | * |
||
100 | * @since 1.0 |
||
101 | * |
||
102 | * @param IParamDefinition $definition |
||
103 | */ |
||
104 | 65 | public function __construct( IParamDefinition $definition ) { |
|
107 | |||
108 | /** |
||
109 | * Sets and cleans the original value and name. |
||
110 | * @see IParam::setUserValue |
||
111 | * |
||
112 | * @since 1.0 |
||
113 | * |
||
114 | * @param string $paramName |
||
115 | * @param string $paramValue |
||
116 | * @param Options $options |
||
117 | * |
||
118 | * @return boolean |
||
119 | */ |
||
120 | 64 | public function setUserValue( $paramName, $paramValue, Options $options ) { |
|
136 | |||
137 | /** |
||
138 | * Sets the value. |
||
139 | * |
||
140 | * @since 1.0 |
||
141 | * |
||
142 | * @param mixed $value |
||
143 | */ |
||
144 | 57 | public function setValue( $value ) { |
|
147 | |||
148 | /** |
||
149 | * Sets the $value to a cleaned value of $originalValue. |
||
150 | * |
||
151 | * TODO: the per-parameter lowercaseing and trimming here needs some thought |
||
152 | * |
||
153 | * @since 1.0 |
||
154 | * |
||
155 | * @param Options $options |
||
156 | */ |
||
157 | 64 | protected function cleanValue( Options $options ) { |
|
196 | |||
197 | /** |
||
198 | * Parameter processing entry point. |
||
199 | * Processes the parameter. This includes parsing, validation and additional formatting. |
||
200 | * |
||
201 | * @since 1.0 |
||
202 | * |
||
203 | * @param $definitions array of IParamDefinition |
||
204 | * @param $params array of IParam |
||
205 | * @param Options $options |
||
206 | * |
||
207 | * @throws Exception |
||
208 | */ |
||
209 | 65 | public function process( array &$definitions, array $params, Options $options ) { |
|
227 | |||
228 | /** |
||
229 | * @since 1.0 |
||
230 | * |
||
231 | * @param Options $options |
||
232 | * |
||
233 | * @return ValueParser |
||
234 | */ |
||
235 | 64 | public function getValueParser( Options $options ) { |
|
251 | |||
252 | /** |
||
253 | * @since 1.0 |
||
254 | * |
||
255 | * @param Options $options |
||
256 | */ |
||
257 | 64 | protected function parseAndValidate( Options $options ) { |
|
283 | |||
284 | /** |
||
285 | * Parses and validates the provided with with specified parser. |
||
286 | * The result is returned in an array on success. On fail, false is returned. |
||
287 | * The result is wrapped in an array since we need to be able to distinguish |
||
288 | * between the method returning false and the value being false. |
||
289 | * |
||
290 | * Parsing and validation errors get added to $this->errors. |
||
291 | * |
||
292 | * @since 1.0 |
||
293 | * |
||
294 | * @param ValueParser $parser |
||
295 | * @param mixed $value |
||
296 | * |
||
297 | * @return array|bool |
||
298 | */ |
||
299 | 64 | protected function parseAndValidateValue( ValueParser $parser, $value ) { |
|
316 | |||
317 | /** |
||
318 | * @since 1.0 |
||
319 | * |
||
320 | * @param string $message |
||
321 | */ |
||
322 | 21 | protected function registerProcessingError( $message ) { |
|
325 | |||
326 | /** |
||
327 | * @since 1.0 |
||
328 | * |
||
329 | * @param string $message |
||
330 | * |
||
331 | * @return ProcessingError |
||
332 | */ |
||
333 | 21 | protected function newProcessingError( $message ) { |
|
337 | |||
338 | /** |
||
339 | * @since 1.0 |
||
340 | * |
||
341 | * @param mixed $value |
||
342 | */ |
||
343 | 64 | protected function validateValue( $value ) { |
|
363 | |||
364 | /** |
||
365 | * Sets the parameter value to the default if needed. |
||
366 | * |
||
367 | * @since 1.0 |
||
368 | */ |
||
369 | 64 | protected function setToDefaultIfNeeded() { |
|
374 | |||
375 | 64 | private function shouldSetToDefault(): bool { |
|
386 | |||
387 | /** |
||
388 | * Returns the original use-provided name. |
||
389 | * |
||
390 | * @since 1.0 |
||
391 | * |
||
392 | * @throws Exception |
||
393 | * @return string |
||
394 | */ |
||
395 | public function getOriginalName() { |
||
401 | |||
402 | /** |
||
403 | * Returns the original use-provided value. |
||
404 | * |
||
405 | * @since 1.0 |
||
406 | * |
||
407 | * @throws Exception |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getOriginalValue() { |
||
416 | |||
417 | /** |
||
418 | * Returns all validation errors that occurred so far. |
||
419 | * |
||
420 | * @since 1.0 |
||
421 | * |
||
422 | * @return ProcessingError[] |
||
423 | */ |
||
424 | 50 | public function getErrors() { |
|
427 | |||
428 | /** |
||
429 | * Sets the parameter value to the default. |
||
430 | * |
||
431 | * @since 1.0 |
||
432 | */ |
||
433 | 2 | protected function setToDefault() { |
|
437 | |||
438 | /** |
||
439 | * Gets if the parameter was set to it's default. |
||
440 | * |
||
441 | * @since 1.0 |
||
442 | * |
||
443 | * @return boolean |
||
444 | */ |
||
445 | public function wasSetToDefault() { |
||
448 | |||
449 | /** |
||
450 | * @return boolean |
||
451 | */ |
||
452 | 65 | public function hasFatalError() { |
|
461 | |||
462 | /** |
||
463 | * Returns the IParamDefinition this IParam was constructed from. |
||
464 | * |
||
465 | * @since 1.0 |
||
466 | * |
||
467 | * @return IParamDefinition |
||
468 | */ |
||
469 | 64 | public function getDefinition() { |
|
472 | |||
473 | /** |
||
474 | * Returns the parameters value. |
||
475 | * |
||
476 | * @since 1.0 |
||
477 | * |
||
478 | * @return mixed |
||
479 | */ |
||
480 | 65 | public function &getValue() { |
|
483 | |||
484 | /** |
||
485 | * Returns if the parameter is required or not. |
||
486 | * |
||
487 | * @since 1.0 |
||
488 | * |
||
489 | * @return boolean |
||
490 | */ |
||
491 | 21 | public function isRequired() { |
|
494 | |||
495 | /** |
||
496 | * Returns if the name of the parameter. |
||
497 | * |
||
498 | * @since 1.0 |
||
499 | * |
||
500 | * @return boolean |
||
501 | */ |
||
502 | public function getName() { |
||
505 | |||
506 | /** |
||
507 | * Returns the parameter name aliases. |
||
508 | * |
||
509 | * @since 1.0 |
||
510 | * |
||
511 | * @return string[] |
||
512 | */ |
||
513 | public function getAliases() { |
||
516 | |||
517 | } |
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.