Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like InputDefinition 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 InputDefinition, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class InputDefinition |
||
| 33 | { |
||
| 34 | private $arguments; |
||
| 35 | private $requiredCount; |
||
| 36 | private $hasAnArrayArgument = false; |
||
| 37 | private $hasOptional; |
||
| 38 | private $options; |
||
| 39 | private $shortcuts; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor. |
||
| 43 | * |
||
| 44 | * @param array $definition An array of InputArgument and InputOption instance |
||
| 45 | * |
||
| 46 | * @api |
||
| 47 | */ |
||
| 48 | public function __construct(array $definition = array()) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Sets the definition of the input. |
||
| 55 | * |
||
| 56 | * @param array $definition The definition array |
||
| 57 | * |
||
| 58 | * @api |
||
| 59 | */ |
||
| 60 | public function setDefinition(array $definition) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Sets the InputArgument objects. |
||
| 78 | * |
||
| 79 | * @param InputArgument[] $arguments An array of InputArgument objects |
||
| 80 | * |
||
| 81 | * @api |
||
| 82 | */ |
||
| 83 | public function setArguments($arguments = array()) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Adds an array of InputArgument objects. |
||
| 94 | * |
||
| 95 | * @param InputArgument[] $arguments An array of InputArgument objects |
||
| 96 | * |
||
| 97 | * @api |
||
| 98 | */ |
||
| 99 | public function addArguments($arguments = array()) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Adds an InputArgument object. |
||
| 110 | * |
||
| 111 | * @param InputArgument $argument An InputArgument object |
||
| 112 | * |
||
| 113 | * @throws \LogicException When incorrect argument is given |
||
| 114 | * |
||
| 115 | * @api |
||
| 116 | */ |
||
| 117 | public function addArgument(InputArgument $argument) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns an InputArgument by name or by position. |
||
| 146 | * |
||
| 147 | * @param string|int $name The InputArgument name or position |
||
| 148 | * |
||
| 149 | * @return InputArgument An InputArgument object |
||
| 150 | * |
||
| 151 | * @throws \InvalidArgumentException When argument given doesn't exist |
||
| 152 | * |
||
| 153 | * @api |
||
| 154 | */ |
||
| 155 | public function getArgument($name) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns true if an InputArgument object exists by name or position. |
||
| 168 | * |
||
| 169 | * @param string|int $name The InputArgument name or position |
||
| 170 | * |
||
| 171 | * @return bool true if the InputArgument object exists, false otherwise |
||
| 172 | * |
||
| 173 | * @api |
||
| 174 | */ |
||
| 175 | public function hasArgument($name) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Gets the array of InputArgument objects. |
||
| 184 | * |
||
| 185 | * @return InputArgument[] An array of InputArgument objects |
||
| 186 | * |
||
| 187 | * @api |
||
| 188 | */ |
||
| 189 | public function getArguments() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the number of InputArguments. |
||
| 196 | * |
||
| 197 | * @return int The number of InputArguments |
||
| 198 | */ |
||
| 199 | public function getArgumentCount() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the number of required InputArguments. |
||
| 206 | * |
||
| 207 | * @return int The number of required InputArguments |
||
| 208 | */ |
||
| 209 | public function getArgumentRequiredCount() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the default values. |
||
| 216 | * |
||
| 217 | * @return array An array of default values |
||
| 218 | */ |
||
| 219 | public function getArgumentDefaults() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Sets the InputOption objects. |
||
| 231 | * |
||
| 232 | * @param InputOption[] $options An array of InputOption objects |
||
| 233 | * |
||
| 234 | * @api |
||
| 235 | */ |
||
| 236 | public function setOptions($options = array()) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Adds an array of InputOption objects. |
||
| 245 | * |
||
| 246 | * @param InputOption[] $options An array of InputOption objects |
||
| 247 | * |
||
| 248 | * @api |
||
| 249 | */ |
||
| 250 | public function addOptions($options = array()) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Adds an InputOption object. |
||
| 259 | * |
||
| 260 | * @param InputOption $option An InputOption object |
||
| 261 | * |
||
| 262 | * @throws \LogicException When option given already exist |
||
| 263 | * |
||
| 264 | * @api |
||
| 265 | */ |
||
| 266 | public function addOption(InputOption $option) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns an InputOption by name. |
||
| 290 | * |
||
| 291 | * @param string $name The InputOption name |
||
| 292 | * |
||
| 293 | * @return InputOption A InputOption object |
||
| 294 | * |
||
| 295 | * @throws \InvalidArgumentException When option given doesn't exist |
||
| 296 | * |
||
| 297 | * @api |
||
| 298 | */ |
||
| 299 | public function getOption($name) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns true if an InputOption object exists by name. |
||
| 310 | * |
||
| 311 | * @param string $name The InputOption name |
||
| 312 | * |
||
| 313 | * @return bool true if the InputOption object exists, false otherwise |
||
| 314 | * |
||
| 315 | * @api |
||
| 316 | */ |
||
| 317 | public function hasOption($name) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets the array of InputOption objects. |
||
| 324 | * |
||
| 325 | * @return InputOption[] An array of InputOption objects |
||
| 326 | * |
||
| 327 | * @api |
||
| 328 | */ |
||
| 329 | public function getOptions() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns true if an InputOption object exists by shortcut. |
||
| 336 | * |
||
| 337 | * @param string $name The InputOption shortcut |
||
| 338 | * |
||
| 339 | * @return bool true if the InputOption object exists, false otherwise |
||
| 340 | */ |
||
| 341 | public function hasShortcut($name) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Gets an InputOption by shortcut. |
||
| 348 | * |
||
| 349 | * @param string $shortcut the Shortcut name |
||
| 350 | * |
||
| 351 | * @return InputOption An InputOption object |
||
| 352 | */ |
||
| 353 | public function getOptionForShortcut($shortcut) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Gets an array of default values. |
||
| 360 | * |
||
| 361 | * @return array An array of all default values |
||
| 362 | */ |
||
| 363 | public function getOptionDefaults() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Returns the InputOption name given a shortcut. |
||
| 375 | * |
||
| 376 | * @param string $shortcut The shortcut |
||
| 377 | * |
||
| 378 | * @return string The InputOption name |
||
| 379 | * |
||
| 380 | * @throws \InvalidArgumentException When option given does not exist |
||
| 381 | */ |
||
| 382 | private function shortcutToName($shortcut) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Gets the synopsis. |
||
| 393 | * |
||
| 394 | * @return string The synopsis |
||
| 395 | */ |
||
| 396 | public function getSynopsis() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns a textual representation of the InputDefinition. |
||
| 417 | * |
||
| 418 | * @return string A string representing the InputDefinition |
||
| 419 | * |
||
| 420 | * @deprecated Deprecated since version 2.3, to be removed in 3.0. |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function asText() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Returns an XML representation of the InputDefinition. |
||
| 433 | * |
||
| 434 | * @param bool $asDom Whether to return a DOM or an XML string |
||
| 435 | * |
||
| 436 | * @return string|\DOMDocument An XML string representing the InputDefinition |
||
| 437 | * |
||
| 438 | * @deprecated Deprecated since version 2.3, to be removed in 3.0. |
||
| 439 | */ |
||
| 440 | View Code Duplication | public function asXml($asDom = false) |
|
| 453 | } |
||
| 454 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: