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 = []) |
||
115 | |||
116 | public static function create($classNameOrInstance, $methodName) |
||
120 | |||
121 | public static function deserialize($cache) |
||
130 | |||
131 | public static function isValidSerializedData($cache) |
||
140 | |||
141 | public function cachedFileIsModified($cache) |
||
146 | |||
147 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
156 | |||
157 | protected function constructFromCache($info_array) |
||
199 | |||
200 | public function serialize() |
||
266 | |||
267 | /** |
||
268 | * Default data for serialization. |
||
269 | * @return array |
||
270 | */ |
||
271 | protected function defaultSerializationData() |
||
288 | |||
289 | /** |
||
290 | * Recover the method name provided to the constructor. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getMethodName() |
||
298 | |||
299 | /** |
||
300 | * Return the primary name for this command. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getName() |
||
309 | |||
310 | /** |
||
311 | * Set the primary name for this command. |
||
312 | * |
||
313 | * @param string $name |
||
314 | */ |
||
315 | public function setName($name) |
||
320 | |||
321 | public function getReturnType() |
||
326 | |||
327 | public function setReturnType($returnType) |
||
332 | |||
333 | /** |
||
334 | * Get any annotations included in the docblock comment for the |
||
335 | * implementation method of this command that are not already |
||
336 | * handled by the primary methods of this class. |
||
337 | * |
||
338 | * @return AnnotationData |
||
339 | */ |
||
340 | public function getRawAnnotations() |
||
345 | |||
346 | /** |
||
347 | * Get any annotations included in the docblock comment, |
||
348 | * also including default values such as @command. We add |
||
349 | * in the default @command annotation late, and only in a |
||
350 | * copy of the annotation data because we use the existance |
||
351 | * of a @command to indicate that this CommandInfo is |
||
352 | * a command, and not a hook or anything else. |
||
353 | * |
||
354 | * @return AnnotationData |
||
355 | */ |
||
356 | public function getAnnotations() |
||
369 | |||
370 | /** |
||
371 | * Return a specific named annotation for this command. |
||
372 | * |
||
373 | * @param string $annotation The name of the annotation. |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getAnnotation($annotation) |
||
384 | |||
385 | /** |
||
386 | * Check to see if the specified annotation exists for this command. |
||
387 | * |
||
388 | * @param string $annotation The name of the annotation. |
||
389 | * @return boolean |
||
390 | */ |
||
391 | public function hasAnnotation($annotation) |
||
396 | |||
397 | /** |
||
398 | * Save any tag that we do not explicitly recognize in the |
||
399 | * 'otherAnnotations' map. |
||
400 | */ |
||
401 | public function addAnnotation($name, $content) |
||
405 | |||
406 | /** |
||
407 | * Remove an annotation that was previoudly set. |
||
408 | */ |
||
409 | public function removeAnnotation($name) |
||
413 | |||
414 | /** |
||
415 | * Get the synopsis of the command (~first line). |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public function getDescription() |
||
424 | |||
425 | /** |
||
426 | * Set the command description. |
||
427 | * |
||
428 | * @param string $description The description to set. |
||
429 | */ |
||
430 | public function setDescription($description) |
||
435 | |||
436 | /** |
||
437 | * Get the help text of the command (the description) |
||
438 | */ |
||
439 | public function getHelp() |
||
444 | /** |
||
445 | * Set the help text for this command. |
||
446 | * |
||
447 | * @param string $help The help text. |
||
448 | */ |
||
449 | public function setHelp($help) |
||
454 | |||
455 | /** |
||
456 | * Return the list of aliases for this command. |
||
457 | * @return string[] |
||
458 | */ |
||
459 | public function getAliases() |
||
464 | |||
465 | /** |
||
466 | * Set aliases that can be used in place of the command's primary name. |
||
467 | * |
||
468 | * @param string|string[] $aliases |
||
469 | */ |
||
470 | public function setAliases($aliases) |
||
478 | |||
479 | /** |
||
480 | * Return the examples for this command. This is @usage instead of |
||
481 | * @example because the later is defined by the phpdoc standard to |
||
482 | * be example method calls. |
||
483 | * |
||
484 | * @return string[] |
||
485 | */ |
||
486 | public function getExampleUsages() |
||
491 | |||
492 | /** |
||
493 | * Add an example usage for this command. |
||
494 | * |
||
495 | * @param string $usage An example of the command, including the command |
||
496 | * name and all of its example arguments and options. |
||
497 | * @param string $description An explanation of what the example does. |
||
498 | */ |
||
499 | public function setExampleUsage($usage, $description) |
||
504 | |||
505 | /** |
||
506 | * Return the topics for this command. |
||
507 | * |
||
508 | * @return string[] |
||
509 | */ |
||
510 | public function getTopics() |
||
518 | |||
519 | /** |
||
520 | * Return the list of refleaction parameters. |
||
521 | * |
||
522 | * @return ReflectionParameter[] |
||
523 | */ |
||
524 | public function getParameters() |
||
528 | |||
529 | /** |
||
530 | * Descriptions of commandline arguements for this command. |
||
531 | * |
||
532 | * @return DefaultsWithDescriptions |
||
533 | */ |
||
534 | public function arguments() |
||
538 | |||
539 | /** |
||
540 | * Descriptions of commandline options for this command. |
||
541 | * |
||
542 | * @return DefaultsWithDescriptions |
||
543 | */ |
||
544 | public function options() |
||
548 | |||
549 | /** |
||
550 | * Get the inputOptions for the options associated with this CommandInfo |
||
551 | * object, e.g. via @option annotations, or from |
||
552 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
553 | * parameter list. |
||
554 | * |
||
555 | * @return InputOption[] |
||
556 | */ |
||
557 | public function inputOptions() |
||
564 | |||
565 | protected function createInputOptions() |
||
599 | |||
600 | /** |
||
601 | * An option might have a name such as 'silent|s'. In this |
||
602 | * instance, we will allow the @option or @default tag to |
||
603 | * reference the option only by name (e.g. 'silent' or 's' |
||
604 | * instead of 'silent|s'). |
||
605 | * |
||
606 | * @param string $optionName |
||
607 | * @return string |
||
608 | */ |
||
609 | public function findMatchingOption($optionName) |
||
621 | |||
622 | /** |
||
623 | * @param string $optionName |
||
624 | * @return string |
||
625 | */ |
||
626 | protected function findOptionAmongAlternatives($optionName) |
||
641 | |||
642 | /** |
||
643 | * @param string $optionName |
||
644 | * @return string|null |
||
645 | */ |
||
646 | protected function findExistingOption($optionName) |
||
657 | |||
658 | /** |
||
659 | * Examine the parameters of the method for this command, and |
||
660 | * build a list of commandline arguements for them. |
||
661 | * |
||
662 | * @return array |
||
663 | */ |
||
664 | protected function determineAgumentClassifications() |
||
677 | |||
678 | /** |
||
679 | * Examine the provided parameter, and determine whether it |
||
680 | * is a parameter that will be filled in with a positional |
||
681 | * commandline argument. |
||
682 | */ |
||
683 | protected function addParameterToResult($result, $param) |
||
700 | |||
701 | /** |
||
702 | * Examine the parameters of the method for this command, and determine |
||
703 | * the disposition of the options from them. |
||
704 | * |
||
705 | * @return array |
||
706 | */ |
||
707 | protected function determineOptionsFromParameters() |
||
722 | |||
723 | /** |
||
724 | * Helper; determine if an array is associative or not. An array |
||
725 | * is not associative if its keys are numeric, and numbered sequentially |
||
726 | * from zero. All other arrays are considered to be associative. |
||
727 | * |
||
728 | * @param array $arr The array |
||
729 | * @return boolean |
||
730 | */ |
||
731 | protected function isAssoc($arr) |
||
738 | |||
739 | /** |
||
740 | * Convert from a method name to the corresponding command name. A |
||
741 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
742 | * become 'foo:bar-baz-boz'. |
||
743 | * |
||
744 | * @param string $camel method name. |
||
745 | * @return string |
||
746 | */ |
||
747 | protected function convertName($camel) |
||
754 | |||
755 | /** |
||
756 | * Parse the docBlock comment for this command, and set the |
||
757 | * fields of this class with the data thereby obtained. |
||
758 | */ |
||
759 | protected function parseDocBlock() |
||
768 | |||
769 | /** |
||
770 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
771 | * convert the data into the last of these forms. |
||
772 | */ |
||
773 | protected static function convertListToCommaSeparated($text) |
||
777 | } |
||
778 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.