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) |
||
133 | |||
134 | public static function isValidSerializedData($cache) |
||
141 | |||
142 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
151 | |||
152 | protected function constructFromCache($info_array) |
||
194 | |||
195 | public function serialize() |
||
258 | |||
259 | /** |
||
260 | * Default data for serialization. |
||
261 | * @return array |
||
262 | */ |
||
263 | protected function defaultSerializationData() |
||
280 | |||
281 | /** |
||
282 | * Recover the method name provided to the constructor. |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getMethodName() |
||
290 | |||
291 | /** |
||
292 | * Return the primary name for this command. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getName() |
||
301 | |||
302 | /** |
||
303 | * Set the primary name for this command. |
||
304 | * |
||
305 | * @param string $name |
||
306 | */ |
||
307 | public function setName($name) |
||
312 | |||
313 | public function getReturnType() |
||
318 | |||
319 | public function setReturnType($returnType) |
||
324 | |||
325 | /** |
||
326 | * Get any annotations included in the docblock comment for the |
||
327 | * implementation method of this command that are not already |
||
328 | * handled by the primary methods of this class. |
||
329 | * |
||
330 | * @return AnnotationData |
||
331 | */ |
||
332 | public function getRawAnnotations() |
||
337 | |||
338 | /** |
||
339 | * Get any annotations included in the docblock comment, |
||
340 | * also including default values such as @command. We add |
||
341 | * in the default @command annotation late, and only in a |
||
342 | * copy of the annotation data because we use the existance |
||
343 | * of a @command to indicate that this CommandInfo is |
||
344 | * a command, and not a hook or anything else. |
||
345 | * |
||
346 | * @return AnnotationData |
||
347 | */ |
||
348 | public function getAnnotations() |
||
357 | |||
358 | /** |
||
359 | * Return a specific named annotation for this command. |
||
360 | * |
||
361 | * @param string $annotation The name of the annotation. |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getAnnotation($annotation) |
||
372 | |||
373 | /** |
||
374 | * Check to see if the specified annotation exists for this command. |
||
375 | * |
||
376 | * @param string $annotation The name of the annotation. |
||
377 | * @return boolean |
||
378 | */ |
||
379 | public function hasAnnotation($annotation) |
||
384 | |||
385 | /** |
||
386 | * Save any tag that we do not explicitly recognize in the |
||
387 | * 'otherAnnotations' map. |
||
388 | */ |
||
389 | public function addAnnotation($name, $content) |
||
393 | |||
394 | /** |
||
395 | * Remove an annotation that was previoudly set. |
||
396 | */ |
||
397 | public function removeAnnotation($name) |
||
401 | |||
402 | /** |
||
403 | * Get the synopsis of the command (~first line). |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getDescription() |
||
412 | |||
413 | /** |
||
414 | * Set the command description. |
||
415 | * |
||
416 | * @param string $description The description to set. |
||
417 | */ |
||
418 | public function setDescription($description) |
||
423 | |||
424 | /** |
||
425 | * Get the help text of the command (the description) |
||
426 | */ |
||
427 | public function getHelp() |
||
432 | /** |
||
433 | * Set the help text for this command. |
||
434 | * |
||
435 | * @param string $help The help text. |
||
436 | */ |
||
437 | public function setHelp($help) |
||
442 | |||
443 | /** |
||
444 | * Return the list of aliases for this command. |
||
445 | * @return string[] |
||
446 | */ |
||
447 | public function getAliases() |
||
452 | |||
453 | /** |
||
454 | * Set aliases that can be used in place of the command's primary name. |
||
455 | * |
||
456 | * @param string|string[] $aliases |
||
457 | */ |
||
458 | public function setAliases($aliases) |
||
466 | |||
467 | /** |
||
468 | * Return the examples for this command. This is @usage instead of |
||
469 | * @example because the later is defined by the phpdoc standard to |
||
470 | * be example method calls. |
||
471 | * |
||
472 | * @return string[] |
||
473 | */ |
||
474 | public function getExampleUsages() |
||
479 | |||
480 | /** |
||
481 | * Add an example usage for this command. |
||
482 | * |
||
483 | * @param string $usage An example of the command, including the command |
||
484 | * name and all of its example arguments and options. |
||
485 | * @param string $description An explanation of what the example does. |
||
486 | */ |
||
487 | public function setExampleUsage($usage, $description) |
||
492 | |||
493 | /** |
||
494 | * Return the topics for this command. |
||
495 | * |
||
496 | * @return string[] |
||
497 | */ |
||
498 | public function getTopics() |
||
506 | |||
507 | /** |
||
508 | * Return the list of refleaction parameters. |
||
509 | * |
||
510 | * @return ReflectionParameter[] |
||
511 | */ |
||
512 | public function getParameters() |
||
516 | |||
517 | /** |
||
518 | * Descriptions of commandline arguements for this command. |
||
519 | * |
||
520 | * @return DefaultsWithDescriptions |
||
521 | */ |
||
522 | public function arguments() |
||
526 | |||
527 | /** |
||
528 | * Descriptions of commandline options for this command. |
||
529 | * |
||
530 | * @return DefaultsWithDescriptions |
||
531 | */ |
||
532 | public function options() |
||
536 | |||
537 | /** |
||
538 | * Get the inputOptions for the options associated with this CommandInfo |
||
539 | * object, e.g. via @option annotations, or from |
||
540 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
541 | * parameter list. |
||
542 | * |
||
543 | * @return InputOption[] |
||
544 | */ |
||
545 | public function inputOptions() |
||
552 | |||
553 | protected function createInputOptions() |
||
578 | |||
579 | /** |
||
580 | * An option might have a name such as 'silent|s'. In this |
||
581 | * instance, we will allow the @option or @default tag to |
||
582 | * reference the option only by name (e.g. 'silent' or 's' |
||
583 | * instead of 'silent|s'). |
||
584 | * |
||
585 | * @param string $optionName |
||
586 | * @return string |
||
587 | */ |
||
588 | public function findMatchingOption($optionName) |
||
600 | |||
601 | /** |
||
602 | * @param string $optionName |
||
603 | * @return string |
||
604 | */ |
||
605 | protected function findOptionAmongAlternatives($optionName) |
||
620 | |||
621 | /** |
||
622 | * @param string $optionName |
||
623 | * @return string|null |
||
624 | */ |
||
625 | protected function findExistingOption($optionName) |
||
636 | |||
637 | /** |
||
638 | * Examine the parameters of the method for this command, and |
||
639 | * build a list of commandline arguements for them. |
||
640 | * |
||
641 | * @return array |
||
642 | */ |
||
643 | protected function determineAgumentClassifications() |
||
656 | |||
657 | /** |
||
658 | * Examine the provided parameter, and determine whether it |
||
659 | * is a parameter that will be filled in with a positional |
||
660 | * commandline argument. |
||
661 | */ |
||
662 | protected function addParameterToResult($result, $param) |
||
679 | |||
680 | /** |
||
681 | * Examine the parameters of the method for this command, and determine |
||
682 | * the disposition of the options from them. |
||
683 | * |
||
684 | * @return array |
||
685 | */ |
||
686 | protected function determineOptionsFromParameters() |
||
701 | |||
702 | /** |
||
703 | * Helper; determine if an array is associative or not. An array |
||
704 | * is not associative if its keys are numeric, and numbered sequentially |
||
705 | * from zero. All other arrays are considered to be associative. |
||
706 | * |
||
707 | * @param arrau $arr The array |
||
708 | * @return boolean |
||
709 | */ |
||
710 | protected function isAssoc($arr) |
||
717 | |||
718 | /** |
||
719 | * Convert from a method name to the corresponding command name. A |
||
720 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
721 | * become 'foo:bar-baz-boz'. |
||
722 | * |
||
723 | * @param string $camel method name. |
||
724 | * @return string |
||
725 | */ |
||
726 | protected function convertName($camel) |
||
733 | |||
734 | /** |
||
735 | * Parse the docBlock comment for this command, and set the |
||
736 | * fields of this class with the data thereby obtained. |
||
737 | */ |
||
738 | protected function parseDocBlock() |
||
747 | |||
748 | /** |
||
749 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
750 | * convert the data into the last of these forms. |
||
751 | */ |
||
752 | protected static function convertListToCommaSeparated($text) |
||
756 | } |
||
757 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.