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) |
||
| 120 | { |
||
| 121 | $classNameOrInstance = $cache['class']; |
||
| 122 | $methodName = $cache['method_name']; |
||
| 123 | |||
| 124 | // If the cache came from a newer version, ignore it and |
||
| 125 | // regenerate the cached information. |
||
| 126 | if (!isset($cache['schema']) || ($cache['schema'] > self::SERIALIZATION_SCHEMA_VERSION)) { |
||
| 127 | return self::create($classNameOrInstance, $methodName); |
||
| 128 | } |
||
| 129 | return new self($classNameOrInstance, $methodName, $cache); |
||
| 130 | } |
||
| 131 | |||
| 132 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
| 141 | |||
| 142 | protected function constructFromCache($info_array) |
||
| 184 | |||
| 185 | public function serialize() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Default data for serialization. |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | protected function defaultSerializationData() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Recover the method name provided to the constructor. |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getMethodName() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Return the primary name for this command. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getName() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Set the primary name for this command. |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | */ |
||
| 297 | public function setName($name) |
||
| 302 | |||
| 303 | public function getReturnType() |
||
| 308 | |||
| 309 | public function setReturnType($returnType) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get any annotations included in the docblock comment for the |
||
| 317 | * implementation method of this command that are not already |
||
| 318 | * handled by the primary methods of this class. |
||
| 319 | * |
||
| 320 | * @return AnnotationData |
||
| 321 | */ |
||
| 322 | public function getRawAnnotations() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get any annotations included in the docblock comment, |
||
| 330 | * also including default values such as @command. We add |
||
| 331 | * in the default @command annotation late, and only in a |
||
| 332 | * copy of the annotation data because we use the existance |
||
| 333 | * of a @command to indicate that this CommandInfo is |
||
| 334 | * a command, and not a hook or anything else. |
||
| 335 | * |
||
| 336 | * @return AnnotationData |
||
| 337 | */ |
||
| 338 | public function getAnnotations() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return a specific named annotation for this command. |
||
| 350 | * |
||
| 351 | * @param string $annotation The name of the annotation. |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getAnnotation($annotation) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Check to see if the specified annotation exists for this command. |
||
| 365 | * |
||
| 366 | * @param string $annotation The name of the annotation. |
||
| 367 | * @return boolean |
||
| 368 | */ |
||
| 369 | public function hasAnnotation($annotation) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Save any tag that we do not explicitly recognize in the |
||
| 377 | * 'otherAnnotations' map. |
||
| 378 | */ |
||
| 379 | public function addAnnotation($name, $content) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Remove an annotation that was previoudly set. |
||
| 386 | */ |
||
| 387 | public function removeAnnotation($name) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the synopsis of the command (~first line). |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function getDescription() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set the command description. |
||
| 405 | * |
||
| 406 | * @param string $description The description to set. |
||
| 407 | */ |
||
| 408 | public function setDescription($description) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get the help text of the command (the description) |
||
| 416 | */ |
||
| 417 | public function getHelp() |
||
| 422 | /** |
||
| 423 | * Set the help text for this command. |
||
| 424 | * |
||
| 425 | * @param string $help The help text. |
||
| 426 | */ |
||
| 427 | public function setHelp($help) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Return the list of aliases for this command. |
||
| 435 | * @return string[] |
||
| 436 | */ |
||
| 437 | public function getAliases() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set aliases that can be used in place of the command's primary name. |
||
| 445 | * |
||
| 446 | * @param string|string[] $aliases |
||
| 447 | */ |
||
| 448 | public function setAliases($aliases) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Return the examples for this command. This is @usage instead of |
||
| 459 | * @example because the later is defined by the phpdoc standard to |
||
| 460 | * be example method calls. |
||
| 461 | * |
||
| 462 | * @return string[] |
||
| 463 | */ |
||
| 464 | public function getExampleUsages() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Add an example usage for this command. |
||
| 472 | * |
||
| 473 | * @param string $usage An example of the command, including the command |
||
| 474 | * name and all of its example arguments and options. |
||
| 475 | * @param string $description An explanation of what the example does. |
||
| 476 | */ |
||
| 477 | public function setExampleUsage($usage, $description) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Return the topics for this command. |
||
| 485 | * |
||
| 486 | * @return string[] |
||
| 487 | */ |
||
| 488 | public function getTopics() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return the list of refleaction parameters. |
||
| 499 | * |
||
| 500 | * @return ReflectionParameter[] |
||
| 501 | */ |
||
| 502 | public function getParameters() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Descriptions of commandline arguements for this command. |
||
| 509 | * |
||
| 510 | * @return DefaultsWithDescriptions |
||
| 511 | */ |
||
| 512 | public function arguments() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Descriptions of commandline options for this command. |
||
| 519 | * |
||
| 520 | * @return DefaultsWithDescriptions |
||
| 521 | */ |
||
| 522 | public function options() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Get the inputOptions for the options associated with this CommandInfo |
||
| 529 | * object, e.g. via @option annotations, or from |
||
| 530 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
| 531 | * parameter list. |
||
| 532 | * |
||
| 533 | * @return InputOption[] |
||
| 534 | */ |
||
| 535 | public function inputOptions() |
||
| 542 | |||
| 543 | protected function createInputOptions() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * An option might have a name such as 'silent|s'. In this |
||
| 571 | * instance, we will allow the @option or @default tag to |
||
| 572 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 573 | * instead of 'silent|s'). |
||
| 574 | * |
||
| 575 | * @param string $optionName |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function findMatchingOption($optionName) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param string $optionName |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | protected function findOptionAmongAlternatives($optionName) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $optionName |
||
| 613 | * @return string|null |
||
| 614 | */ |
||
| 615 | protected function findExistingOption($optionName) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Examine the parameters of the method for this command, and |
||
| 629 | * build a list of commandline arguements for them. |
||
| 630 | * |
||
| 631 | * @return array |
||
| 632 | */ |
||
| 633 | protected function determineAgumentClassifications() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Examine the provided parameter, and determine whether it |
||
| 649 | * is a parameter that will be filled in with a positional |
||
| 650 | * commandline argument. |
||
| 651 | */ |
||
| 652 | protected function addParameterToResult($result, $param) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Examine the parameters of the method for this command, and determine |
||
| 672 | * the disposition of the options from them. |
||
| 673 | * |
||
| 674 | * @return array |
||
| 675 | */ |
||
| 676 | protected function determineOptionsFromParameters() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Helper; determine if an array is associative or not. An array |
||
| 694 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 695 | * from zero. All other arrays are considered to be associative. |
||
| 696 | * |
||
| 697 | * @param arrau $arr The array |
||
| 698 | * @return boolean |
||
| 699 | */ |
||
| 700 | protected function isAssoc($arr) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Convert from a method name to the corresponding command name. A |
||
| 710 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 711 | * become 'foo:bar-baz-boz'. |
||
| 712 | * |
||
| 713 | * @param string $camel method name. |
||
| 714 | * @return string |
||
| 715 | */ |
||
| 716 | protected function convertName($camel) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Parse the docBlock comment for this command, and set the |
||
| 726 | * fields of this class with the data thereby obtained. |
||
| 727 | */ |
||
| 728 | protected function parseDocBlock() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 740 | * convert the data into the last of these forms. |
||
| 741 | */ |
||
| 742 | protected static function convertListToCommaSeparated($text) |
||
| 746 | } |
||
| 747 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.