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 CommandInfo 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 CommandInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class CommandInfo |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Serialization schema version. Incremented every time the serialization schema changes. |
||
| 22 | */ |
||
| 23 | const SERIALIZATION_SCHEMA_VERSION = 1; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \ReflectionMethod |
||
| 27 | */ |
||
| 28 | protected $reflection; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var boolean |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $docBlockIsParsed = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $name; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $description = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $help = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var DefaultsWithDescriptions |
||
| 53 | */ |
||
| 54 | protected $options; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var DefaultsWithDescriptions |
||
| 58 | */ |
||
| 59 | protected $arguments; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $exampleUsage = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var AnnotationData |
||
| 68 | */ |
||
| 69 | protected $otherAnnotations; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $aliases = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var InputOption[] |
||
| 78 | */ |
||
| 79 | protected $inputOptions; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $methodName; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $returnType; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create a new CommandInfo class for a particular method of a class. |
||
| 93 | * |
||
| 94 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 95 | * instance of it, or an array of cached data. |
||
| 96 | * @param string $methodName The name of the method to get info about. |
||
| 97 | * @param array $cache Cached data |
||
| 98 | * @deprecated Use CommandInfo::create() or CommandInfo::deserialize() |
||
| 99 | * instead. In the future, this constructor will be protected. |
||
| 100 | */ |
||
| 101 | public function __construct($classNameOrInstance, $methodName, $cache = []) |
||
| 113 | |||
| 114 | public static function create($classNameOrInstance, $methodName) |
||
| 118 | |||
| 119 | public static function deserialize($cache) |
||
| 131 | |||
| 132 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
| 141 | |||
| 142 | protected function constructFromCache($info_array) |
||
| 182 | |||
| 183 | public function serialize() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Recover the method name provided to the constructor. |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getMethodName() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return the primary name for this command. |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function getName() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set the primary name for this command. |
||
| 274 | * |
||
| 275 | * @param string $name |
||
| 276 | */ |
||
| 277 | public function setName($name) |
||
| 282 | |||
| 283 | public function getReturnType() |
||
| 288 | |||
| 289 | public function setReturnType($returnType) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get any annotations included in the docblock comment for the |
||
| 297 | * implementation method of this command that are not already |
||
| 298 | * handled by the primary methods of this class. |
||
| 299 | * |
||
| 300 | * @return AnnotationData |
||
| 301 | */ |
||
| 302 | public function getRawAnnotations() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get any annotations included in the docblock comment, |
||
| 310 | * also including default values such as @command. We add |
||
| 311 | * in the default @command annotation late, and only in a |
||
| 312 | * copy of the annotation data because we use the existance |
||
| 313 | * of a @command to indicate that this CommandInfo is |
||
| 314 | * a command, and not a hook or anything else. |
||
| 315 | * |
||
| 316 | * @return AnnotationData |
||
| 317 | */ |
||
| 318 | public function getAnnotations() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Return a specific named annotation for this command. |
||
| 330 | * |
||
| 331 | * @param string $annotation The name of the annotation. |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getAnnotation($annotation) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Check to see if the specified annotation exists for this command. |
||
| 345 | * |
||
| 346 | * @param string $annotation The name of the annotation. |
||
| 347 | * @return boolean |
||
| 348 | */ |
||
| 349 | public function hasAnnotation($annotation) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Save any tag that we do not explicitly recognize in the |
||
| 357 | * 'otherAnnotations' map. |
||
| 358 | */ |
||
| 359 | public function addAnnotation($name, $content) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Remove an annotation that was previoudly set. |
||
| 366 | */ |
||
| 367 | public function removeAnnotation($name) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the synopsis of the command (~first line). |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getDescription() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set the command description. |
||
| 385 | * |
||
| 386 | * @param string $description The description to set. |
||
| 387 | */ |
||
| 388 | public function setDescription($description) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get the help text of the command (the description) |
||
| 396 | */ |
||
| 397 | public function getHelp() |
||
| 402 | /** |
||
| 403 | * Set the help text for this command. |
||
| 404 | * |
||
| 405 | * @param string $help The help text. |
||
| 406 | */ |
||
| 407 | public function setHelp($help) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return the list of aliases for this command. |
||
| 415 | * @return string[] |
||
| 416 | */ |
||
| 417 | public function getAliases() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Set aliases that can be used in place of the command's primary name. |
||
| 425 | * |
||
| 426 | * @param string|string[] $aliases |
||
| 427 | */ |
||
| 428 | public function setAliases($aliases) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Return the examples for this command. This is @usage instead of |
||
| 439 | * @example because the later is defined by the phpdoc standard to |
||
| 440 | * be example method calls. |
||
| 441 | * |
||
| 442 | * @return string[] |
||
| 443 | */ |
||
| 444 | public function getExampleUsages() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Add an example usage for this command. |
||
| 452 | * |
||
| 453 | * @param string $usage An example of the command, including the command |
||
| 454 | * name and all of its example arguments and options. |
||
| 455 | * @param string $description An explanation of what the example does. |
||
| 456 | */ |
||
| 457 | public function setExampleUsage($usage, $description) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Return the topics for this command. |
||
| 465 | * |
||
| 466 | * @return string[] |
||
| 467 | */ |
||
| 468 | public function getTopics() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Return the list of refleaction parameters. |
||
| 479 | * |
||
| 480 | * @return ReflectionParameter[] |
||
| 481 | */ |
||
| 482 | public function getParameters() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Descriptions of commandline arguements for this command. |
||
| 489 | * |
||
| 490 | * @return DefaultsWithDescriptions |
||
| 491 | */ |
||
| 492 | public function arguments() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Descriptions of commandline options for this command. |
||
| 499 | * |
||
| 500 | * @return DefaultsWithDescriptions |
||
| 501 | */ |
||
| 502 | public function options() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get the inputOptions for the options associated with this CommandInfo |
||
| 509 | * object, e.g. via @option annotations, or from |
||
| 510 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
| 511 | * parameter list. |
||
| 512 | * |
||
| 513 | * @return InputOption[] |
||
| 514 | */ |
||
| 515 | public function inputOptions() |
||
| 522 | |||
| 523 | protected function createInputOptions() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * An option might have a name such as 'silent|s'. In this |
||
| 551 | * instance, we will allow the @option or @default tag to |
||
| 552 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 553 | * instead of 'silent|s'). |
||
| 554 | * |
||
| 555 | * @param string $optionName |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | public function findMatchingOption($optionName) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param string $optionName |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | protected function findOptionAmongAlternatives($optionName) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param string $optionName |
||
| 593 | * @return string|null |
||
| 594 | */ |
||
| 595 | protected function findExistingOption($optionName) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Examine the parameters of the method for this command, and |
||
| 609 | * build a list of commandline arguements for them. |
||
| 610 | * |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | protected function determineAgumentClassifications() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Examine the provided parameter, and determine whether it |
||
| 629 | * is a parameter that will be filled in with a positional |
||
| 630 | * commandline argument. |
||
| 631 | */ |
||
| 632 | protected function addParameterToResult($result, $param) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Examine the parameters of the method for this command, and determine |
||
| 652 | * the disposition of the options from them. |
||
| 653 | * |
||
| 654 | * @return array |
||
| 655 | */ |
||
| 656 | protected function determineOptionsFromParameters() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Helper; determine if an array is associative or not. An array |
||
| 674 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 675 | * from zero. All other arrays are considered to be associative. |
||
| 676 | * |
||
| 677 | * @param arrau $arr The array |
||
| 678 | * @return boolean |
||
| 679 | */ |
||
| 680 | protected function isAssoc($arr) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Convert from a method name to the corresponding command name. A |
||
| 690 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 691 | * become 'foo:bar-baz-boz'. |
||
| 692 | * |
||
| 693 | * @param string $camel method name. |
||
| 694 | * @return string |
||
| 695 | */ |
||
| 696 | protected function convertName($camel) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Parse the docBlock comment for this command, and set the |
||
| 706 | * fields of this class with the data thereby obtained. |
||
| 707 | */ |
||
| 708 | protected function parseDocBlock() |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 720 | * convert the data into the last of these forms. |
||
| 721 | */ |
||
| 722 | protected static function convertListToCommaSeparated($text) |
||
| 726 | } |
||
| 727 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.