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 |
||
| 24 | /* final */ class ParamDefinition implements IParamDefinition { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Indicates whether parameters that are provided more then once should be accepted, |
||
| 28 | * and use the first provided value, or not, and generate an error. |
||
| 29 | * |
||
| 30 | * @deprected since 1.7 |
||
| 31 | * |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | public static $acceptOverriding = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Indicates whether parameters not found in the criteria list |
||
| 38 | * should be stored in case they are not accepted. The default is false. |
||
| 39 | * |
||
| 40 | * @deprected since 1.7 |
||
| 41 | * |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | public static $accumulateParameterErrors = false; |
||
| 45 | |||
| 46 | protected $type; |
||
| 47 | protected $name; |
||
| 48 | protected $default; |
||
| 49 | protected $isList; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * A message that acts as description for the parameter or false when there is none. |
||
| 53 | * Can be obtained via getMessage and set via setMessage. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $message; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Indicates if the parameter value should trimmed during the clean process. |
||
| 61 | * @var boolean|null |
||
| 62 | */ |
||
| 63 | protected $trimValue = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Indicates if the parameter manipulations should be applied to the default value. |
||
| 67 | * @var boolean |
||
| 68 | */ |
||
| 69 | protected $applyManipulationsToDefault = true; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Dependency list containing parameters that need to be handled before this one. |
||
| 73 | * @var string[] |
||
| 74 | */ |
||
| 75 | protected $dependencies = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $delimiter = ','; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * List of aliases for the parameter name. |
||
| 84 | * |
||
| 85 | * @var string[] |
||
| 86 | */ |
||
| 87 | protected $aliases = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Original array definition of the parameter |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $options = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var ValueParser|null |
||
| 97 | */ |
||
| 98 | protected $parser = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var ValueValidator|null |
||
| 102 | */ |
||
| 103 | protected $validator = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var callable|null |
||
| 107 | */ |
||
| 108 | protected $validationFunction = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $type |
||
| 112 | * @param string $name |
||
| 113 | * @param mixed $default Use null for no default (which makes the parameter required) |
||
| 114 | * @param string $message |
||
| 115 | * @param boolean $isList |
||
| 116 | */ |
||
| 117 | 56 | public function __construct( string $type, string $name, $default = null, string $message = null, bool $isList = false ) { |
|
| 118 | 56 | $this->type = $type; |
|
| 119 | 56 | $this->name = $name; |
|
| 120 | 56 | $this->default = $default; |
|
| 121 | 56 | $this->message = $message ?? 'validator-message-nodesc'; |
|
| 122 | 56 | $this->isList = $isList; |
|
| 123 | |||
| 124 | 56 | $this->postConstruct(); |
|
| 125 | 56 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Allows deriving classed to do additional stuff on instance construction |
||
| 129 | * without having to get and pass all the constructor arguments. |
||
| 130 | * |
||
| 131 | * @since 1.0 |
||
| 132 | */ |
||
| 133 | 56 | protected function postConstruct() { |
|
| 134 | |||
| 135 | 56 | } |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Returns if the value should be trimmed before validation and any further processing. |
||
| 139 | * - true: always trim |
||
| 140 | * - false: never trim |
||
| 141 | * - null: trim based on context settings |
||
| 142 | */ |
||
| 143 | 78 | public function trimDuringClean(): ?bool { |
|
| 144 | 78 | return $this->trimValue; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the parameter name aliases. |
||
| 149 | * @return string[] |
||
| 150 | */ |
||
| 151 | public function getAliases(): array { |
||
| 152 | return $this->aliases; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function hasAlias( string $alias ): bool { |
||
| 156 | return in_array( $alias, $this->getAliases() ); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @deprecated since 1.7 |
||
| 161 | * Returns if the parameter has a certain dependency. |
||
| 162 | */ |
||
| 163 | public function hasDependency( string $dependency ): bool { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the list of allowed values, or an empty array if there is no such restriction. |
||
| 169 | */ |
||
| 170 | public function getAllowedValues(): array { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @deprecated since 1.7 |
||
| 190 | * |
||
| 191 | * @param mixed $default |
||
| 192 | * @param boolean $manipulate Should the default be manipulated or not? Since 0.4.6. |
||
| 193 | */ |
||
| 194 | public function setDefault( $default, $manipulate = true ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the default value. |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 1 | public function getDefault() { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Returns a message describing the parameter. |
||
| 209 | */ |
||
| 210 | 5 | public function getMessage(): string { |
|
| 213 | |||
| 214 | /** |
||
| 215 | * This should be a message key, ie something that can be passed |
||
| 216 | * to wfMsg. Not an actual text. |
||
| 217 | */ |
||
| 218 | public function setMessage( string $message ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set if the parameter manipulations should be applied to the default value. |
||
| 224 | */ |
||
| 225 | public function setDoManipulationOfDefault( bool $doOrDoNotThereIsNoTry ) { |
||
| 228 | |||
| 229 | 62 | public function shouldManipulateDefault(): bool { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Adds one or more aliases for the parameter name. |
||
| 235 | * |
||
| 236 | * @param string|string[] $aliases |
||
| 237 | */ |
||
| 238 | public function addAliases( $aliases ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Adds one or more dependencies. There are the names of parameters |
||
| 245 | * that need to be validated and formatted before this one. |
||
| 246 | * |
||
| 247 | * @param string|string[] $dependencies |
||
| 248 | */ |
||
| 249 | public function addDependencies( $dependencies ) { |
||
| 253 | |||
| 254 | 82 | public function getName(): string { |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Returns a message key for a message describing the parameter type. |
||
| 260 | */ |
||
| 261 | public function getTypeMessage(): string { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @deprecated since 1.7 |
||
| 273 | * Returns a list of dependencies the parameter has, in the form of |
||
| 274 | * other parameter names. |
||
| 275 | * |
||
| 276 | * @return string[] |
||
| 277 | */ |
||
| 278 | 14 | public function getDependencies(): array { |
|
| 279 | 14 | return $this->dependencies; |
|
| 280 | } |
||
| 281 | |||
| 282 | 30 | public function isRequired(): bool { |
|
| 285 | |||
| 286 | 78 | public function isList(): bool { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the delimiter to use to split the raw value in case the |
||
| 292 | * parameter is a list. |
||
| 293 | */ |
||
| 294 | public function getDelimiter(): string { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sets the delimiter to use to split the raw value in case the |
||
| 300 | * parameter is a list. |
||
| 301 | */ |
||
| 302 | public function setDelimiter( string $delimiter ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Sets the parameter definition values contained in the provided array. |
||
| 308 | * |
||
| 309 | * @deprecated since 1.7 |
||
| 310 | * TODO: provide alternative in ParamDefinitionFactory |
||
| 311 | */ |
||
| 312 | 50 | public function setArrayValues( array $param ) { |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @see IParamDefinition::format |
||
| 338 | * |
||
| 339 | * @deprecated |
||
| 340 | * |
||
| 341 | * @param IParam $param |
||
| 342 | * @param IParamDefinition[] $definitions |
||
| 343 | * @param IParam[] $params |
||
| 344 | */ |
||
| 345 | 62 | public function format( IParam $param, array &$definitions, array $params ) { |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Formats the parameters values to their final result. |
||
| 378 | * |
||
| 379 | * @since 1.0 |
||
| 380 | * @deprecated |
||
| 381 | * |
||
| 382 | * @param $param IParam |
||
| 383 | * @param $definitions array of IParamDefinition |
||
| 384 | * @param $params array of IParam |
||
| 385 | */ |
||
| 386 | protected function formatList( IParam $param, array &$definitions, array $params ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Formats the parameter value to it's final result. |
||
| 391 | * |
||
| 392 | * @since 1.0 |
||
| 393 | * @deprecated |
||
| 394 | * |
||
| 395 | * @param mixed $value |
||
| 396 | * @param IParam $param |
||
| 397 | * @param IParamDefinition[] $definitions |
||
| 398 | * @param IParam[] $params |
||
| 399 | * |
||
| 400 | * @return mixed |
||
| 401 | */ |
||
| 402 | 42 | protected function formatValue( $value, IParam $param, array &$definitions, array $params ) { |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Returns a cleaned version of the list of parameter definitions. |
||
| 408 | * This includes having converted all supported definition types to |
||
| 409 | * ParamDefinition classes and having all keys set to the names of the |
||
| 410 | * corresponding parameters. |
||
| 411 | * |
||
| 412 | * @deprecated since 1.7 - use ParamDefinitionFactory |
||
| 413 | * |
||
| 414 | * @param ParamDefinition[] $definitions |
||
| 415 | * |
||
| 416 | * @return ParamDefinition[] |
||
| 417 | * @throws Exception |
||
| 418 | */ |
||
| 419 | 70 | public static function getCleanDefinitions( array $definitions ): array { |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Returns an identifier for the type of the parameter. |
||
| 425 | */ |
||
| 426 | 94 | public function getType(): string { |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Returns a ValueParser object to parse the parameters value. |
||
| 432 | */ |
||
| 433 | 78 | public function getValueParser(): ValueParser { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Returns a ValueValidator that can be used to validate the parameters value. |
||
| 443 | */ |
||
| 444 | 78 | public function getValueValidator(): ValueValidator { |
|
| 451 | |||
| 452 | public function setValueParser( ValueParser $parser ) { |
||
| 455 | |||
| 456 | 50 | public function setValueValidator( ValueValidator $validator ) { |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Sets a validation function that will be run before the ValueValidator. |
||
| 462 | * |
||
| 463 | * This can be used instead of a ValueValidator where validation is very |
||
| 464 | * trivial, ie checking if something is a boolean can be done with is_bool. |
||
| 465 | */ |
||
| 466 | 36 | public function setValidationCallback( ?callable $validationFunction ) { |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @see setValidationCallback |
||
| 472 | */ |
||
| 473 | 78 | public function getValidationCallback(): ?callable { |
|
| 476 | |||
| 477 | 78 | public function getOptions(): array { |
|
| 480 | |||
| 481 | } |
||
| 482 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: