Complex classes like ParamDefinition 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 ParamDefinition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class ParamDefinition implements IParamDefinition { |
||
|
|||
26 | |||
27 | /** |
||
28 | * Indicates whether parameters that are provided more then once should be accepted, |
||
29 | * and use the first provided value, or not, and generate an error. |
||
30 | * |
||
31 | * @since 1.0 |
||
32 | * |
||
33 | * @var boolean |
||
34 | */ |
||
35 | public static $acceptOverriding = false; |
||
36 | |||
37 | /** |
||
38 | * Indicates whether parameters not found in the criteria list |
||
39 | * should be stored in case they are not accepted. The default is false. |
||
40 | * |
||
41 | * @since 1.0 |
||
42 | * |
||
43 | * @var boolean |
||
44 | */ |
||
45 | public static $accumulateParameterErrors = false; |
||
46 | |||
47 | protected $type; |
||
48 | protected $name; |
||
49 | protected $default; |
||
50 | protected $isList; |
||
51 | |||
52 | /** |
||
53 | * A message that acts as description for the parameter or false when there is none. |
||
54 | * Can be obtained via getMessage and set via setMessage. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $message = 'validator-message-nodesc'; |
||
59 | |||
60 | /** |
||
61 | * Indicates if the parameter value should trimmed during the clean process. |
||
62 | * |
||
63 | * @since 1.0 |
||
64 | * |
||
65 | * @var boolean|null |
||
66 | */ |
||
67 | protected $trimValue = null; |
||
68 | |||
69 | /** |
||
70 | * Indicates if the parameter manipulations should be applied to the default value. |
||
71 | * |
||
72 | * @since 1.0 |
||
73 | * |
||
74 | * @var boolean |
||
75 | */ |
||
76 | protected $applyManipulationsToDefault = true; |
||
77 | |||
78 | /** |
||
79 | * Dependency list containing parameters that need to be handled before this one. |
||
80 | * |
||
81 | * @since 1.0 |
||
82 | * |
||
83 | * @var string[] |
||
84 | */ |
||
85 | protected $dependencies = []; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $delimiter = ','; |
||
91 | |||
92 | /** |
||
93 | * List of aliases for the parameter name. |
||
94 | * |
||
95 | * @var string[] |
||
96 | */ |
||
97 | protected $aliases = []; |
||
98 | |||
99 | /** |
||
100 | * Original array definition of the parameter |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $options = []; |
||
104 | |||
105 | /** |
||
106 | * @var ValueParser|null |
||
107 | */ |
||
108 | protected $parser = null; |
||
109 | |||
110 | /** |
||
111 | * @var ValueValidator|null |
||
112 | */ |
||
113 | protected $validator = null; |
||
114 | |||
115 | /** |
||
116 | * @var callable|null |
||
117 | */ |
||
118 | protected $validationFunction = null; |
||
119 | |||
120 | /** |
||
121 | * @param string $type |
||
122 | * @param string $name |
||
123 | * @param mixed $default Use null for no default (which makes the parameter required) |
||
124 | * @param string $message |
||
125 | * @param boolean $isList |
||
126 | */ |
||
127 | 36 | public function __construct( string $type, string $name, $default = null, string $message = null, bool $isList = false ) { |
|
136 | |||
137 | /** |
||
138 | * Allows deriving classed to do additional stuff on instance construction |
||
139 | * without having to get and pass all the constructor arguments. |
||
140 | * |
||
141 | * @since 1.0 |
||
142 | */ |
||
143 | 36 | protected function postConstruct() { |
|
146 | |||
147 | /** |
||
148 | * Returns if the value should be trimmed before validation and any further processing. |
||
149 | * @return boolean|null |
||
150 | */ |
||
151 | 64 | public function trimDuringClean() { |
|
154 | |||
155 | /** |
||
156 | * Returns the parameter name aliases. |
||
157 | * @return string[] |
||
158 | */ |
||
159 | public function getAliases(): array { |
||
162 | |||
163 | public function hasAlias( string $alias ): bool { |
||
166 | |||
167 | /** |
||
168 | * @see IParamDefinition::hasDependency |
||
169 | * |
||
170 | * @since 1.0 |
||
171 | * |
||
172 | * @param string $dependency |
||
173 | * |
||
174 | * @return boolean |
||
175 | */ |
||
176 | public function hasDependency( string $dependency ): bool { |
||
179 | |||
180 | /** |
||
181 | * Returns the list of allowed values, or an empty array if there is no such restriction. |
||
182 | * |
||
183 | * @since 1.0 |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | public function getAllowedValues() { |
||
204 | |||
205 | /** |
||
206 | * @see IParamDefinition::setDefault |
||
207 | * |
||
208 | * @since 1.0 |
||
209 | * |
||
210 | * @param mixed $default |
||
211 | * @param boolean $manipulate Should the default be manipulated or not? Since 0.4.6. |
||
212 | */ |
||
213 | public function setDefault( $default, $manipulate = true ) { |
||
217 | |||
218 | /** |
||
219 | * @see IParamDefinition::getDefault |
||
220 | * |
||
221 | * @since 1.0 |
||
222 | * |
||
223 | * @return mixed |
||
224 | */ |
||
225 | 1 | public function getDefault() { |
|
228 | |||
229 | /** |
||
230 | * Returns a message describing the parameter. |
||
231 | */ |
||
232 | public function getMessage(): string { |
||
235 | |||
236 | /** |
||
237 | * This should be a message key, ie something that can be passed |
||
238 | * to wfMsg. Not an actual text. |
||
239 | */ |
||
240 | public function setMessage( string $message ) { |
||
243 | |||
244 | /** |
||
245 | * Set if the parameter manipulations should be applied to the default value. |
||
246 | */ |
||
247 | public function setDoManipulationOfDefault( bool $doOrDoNotThereIsNoTry ) { |
||
250 | |||
251 | 56 | public function shouldManipulateDefault(): bool { |
|
254 | |||
255 | /** |
||
256 | * Adds one or more aliases for the parameter name. |
||
257 | * |
||
258 | * @param string|string[] $aliases |
||
259 | */ |
||
260 | public function addAliases( $aliases ) { |
||
264 | |||
265 | /** |
||
266 | * Adds one or more dependencies. There are the names of parameters |
||
267 | * that need to be validated and formatted before this one. |
||
268 | * |
||
269 | * @param string|string[] $dependencies |
||
270 | */ |
||
271 | public function addDependencies( $dependencies ) { |
||
275 | |||
276 | 68 | public function getName(): string { |
|
279 | |||
280 | /** |
||
281 | * Returns a message key for a message describing the parameter type. |
||
282 | */ |
||
283 | public function getTypeMessage(): string { |
||
292 | |||
293 | /** |
||
294 | * Returns a list of dependencies the parameter has, in the form of |
||
295 | * other parameter names. |
||
296 | * |
||
297 | * @return string[] |
||
298 | */ |
||
299 | public function getDependencies(): array { |
||
302 | |||
303 | 21 | public function isRequired(): bool { |
|
306 | |||
307 | 64 | public function isList(): bool { |
|
310 | |||
311 | /** |
||
312 | * Returns the delimiter to use to split the raw value in case the |
||
313 | * parameter is a list. |
||
314 | */ |
||
315 | public function getDelimiter(): string { |
||
318 | |||
319 | /** |
||
320 | * Sets the delimiter to use to split the raw value in case the |
||
321 | * parameter is a list. |
||
322 | * |
||
323 | * @param $delimiter string |
||
324 | */ |
||
325 | public function setDelimiter( $delimiter ) { |
||
328 | |||
329 | /** |
||
330 | * Sets the parameter definition values contained in the provided array. |
||
331 | * |
||
332 | * @param array $param |
||
333 | */ |
||
334 | 36 | public function setArrayValues( array $param ) { |
|
357 | |||
358 | /** |
||
359 | * @see IParamDefinition::format |
||
360 | * |
||
361 | * @since 1.0 |
||
362 | * @deprecated |
||
363 | * |
||
364 | * @param IParam $param |
||
365 | * @param IParamDefinition[] $definitions |
||
366 | * @param IParam[] $params |
||
367 | */ |
||
368 | 56 | public function format( IParam $param, array &$definitions, array $params ) { |
|
398 | |||
399 | /** |
||
400 | * Formats the parameters values to their final result. |
||
401 | * |
||
402 | * @since 1.0 |
||
403 | * @deprecated |
||
404 | * |
||
405 | * @param $param IParam |
||
406 | * @param $definitions array of IParamDefinition |
||
407 | * @param $params array of IParam |
||
408 | */ |
||
409 | protected function formatList( IParam $param, array &$definitions, array $params ) { |
||
411 | |||
412 | /** |
||
413 | * Formats the parameter value to it's final result. |
||
414 | * |
||
415 | * @since 1.0 |
||
416 | * @deprecated |
||
417 | * |
||
418 | * @param mixed $value |
||
419 | * @param IParam $param |
||
420 | * @param IParamDefinition[] $definitions |
||
421 | * @param IParam[] $params |
||
422 | * |
||
423 | * @return mixed |
||
424 | */ |
||
425 | 42 | protected function formatValue( $value, IParam $param, array &$definitions, array $params ) { |
|
428 | |||
429 | /** |
||
430 | * Returns a cleaned version of the list of parameter definitions. |
||
431 | * This includes having converted all supported definition types to |
||
432 | * ParamDefinition classes and having all keys set to the names of the |
||
433 | * corresponding parameters. |
||
434 | * |
||
435 | * @since 1.0 |
||
436 | * |
||
437 | * @param ParamDefinition[] $definitions |
||
438 | * |
||
439 | * @return ParamDefinition[] |
||
440 | * @throws Exception |
||
441 | */ |
||
442 | 56 | public static function getCleanDefinitions( array $definitions ): array { |
|
463 | |||
464 | /** |
||
465 | * Returns an identifier for the type of the parameter. |
||
466 | */ |
||
467 | 80 | public function getType(): string { |
|
470 | |||
471 | /** |
||
472 | * Returns a ValueParser object to parse the parameters value. |
||
473 | */ |
||
474 | 64 | public function getValueParser(): ValueParser { |
|
481 | |||
482 | /** |
||
483 | * Returns a ValueValidator that can be used to validate the parameters value. |
||
484 | */ |
||
485 | 64 | public function getValueValidator(): ValueValidator { |
|
486 | 64 | if ( $this->validator === null ) { |
|
487 | 2 | $this->validator = new NullValidator(); |
|
488 | } |
||
489 | |||
490 | 64 | return $this->validator; |
|
491 | } |
||
492 | |||
493 | public function setValueParser( ValueParser $parser ) { |
||
496 | |||
497 | 36 | public function setValueValidator( ValueValidator $validator ) { |
|
500 | |||
501 | /** |
||
502 | * Sets a validation function that will be run before the ValueValidator. |
||
503 | * |
||
504 | * This can be used instead of a ValueValidator where validation is very |
||
505 | * trivial, ie checking if something is a boolean can be done with is_bool. |
||
506 | */ |
||
507 | 36 | public function setValidationCallback( ?callable $validationFunction ) { |
|
510 | |||
511 | /** |
||
512 | * @see setValidationCallback |
||
513 | */ |
||
514 | 64 | public function getValidationCallback(): ?callable { |
|
517 | |||
518 | 64 | public function getOptions(): array { |
|
521 | |||
522 | } |
||
523 |
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.