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() |
||
289 | |||
290 | /** |
||
291 | * Recover the method name provided to the constructor. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getMethodName() |
||
299 | |||
300 | /** |
||
301 | * Return the primary name for this command. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getName() |
||
310 | |||
311 | /** |
||
312 | * Set the primary name for this command. |
||
313 | * |
||
314 | * @param string $name |
||
315 | */ |
||
316 | public function setName($name) |
||
321 | |||
322 | public function getReturnType() |
||
327 | |||
328 | public function setReturnType($returnType) |
||
333 | |||
334 | /** |
||
335 | * Get any annotations included in the docblock comment for the |
||
336 | * implementation method of this command that are not already |
||
337 | * handled by the primary methods of this class. |
||
338 | * |
||
339 | * @return AnnotationData |
||
340 | */ |
||
341 | public function getRawAnnotations() |
||
346 | |||
347 | /** |
||
348 | * Get any annotations included in the docblock comment, |
||
349 | * also including default values such as @command. We add |
||
350 | * in the default @command annotation late, and only in a |
||
351 | * copy of the annotation data because we use the existance |
||
352 | * of a @command to indicate that this CommandInfo is |
||
353 | * a command, and not a hook or anything else. |
||
354 | * |
||
355 | * @return AnnotationData |
||
356 | */ |
||
357 | public function getAnnotations() |
||
366 | |||
367 | /** |
||
368 | * Return a specific named annotation for this command. |
||
369 | * |
||
370 | * @param string $annotation The name of the annotation. |
||
371 | * @return string |
||
372 | */ |
||
373 | public function getAnnotation($annotation) |
||
381 | |||
382 | /** |
||
383 | * Check to see if the specified annotation exists for this command. |
||
384 | * |
||
385 | * @param string $annotation The name of the annotation. |
||
386 | * @return boolean |
||
387 | */ |
||
388 | public function hasAnnotation($annotation) |
||
393 | |||
394 | /** |
||
395 | * Save any tag that we do not explicitly recognize in the |
||
396 | * 'otherAnnotations' map. |
||
397 | */ |
||
398 | public function addAnnotation($name, $content) |
||
402 | |||
403 | /** |
||
404 | * Remove an annotation that was previoudly set. |
||
405 | */ |
||
406 | public function removeAnnotation($name) |
||
410 | |||
411 | /** |
||
412 | * Get the synopsis of the command (~first line). |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | public function getDescription() |
||
421 | |||
422 | /** |
||
423 | * Set the command description. |
||
424 | * |
||
425 | * @param string $description The description to set. |
||
426 | */ |
||
427 | public function setDescription($description) |
||
432 | |||
433 | /** |
||
434 | * Get the help text of the command (the description) |
||
435 | */ |
||
436 | public function getHelp() |
||
441 | /** |
||
442 | * Set the help text for this command. |
||
443 | * |
||
444 | * @param string $help The help text. |
||
445 | */ |
||
446 | public function setHelp($help) |
||
451 | |||
452 | /** |
||
453 | * Return the list of aliases for this command. |
||
454 | * @return string[] |
||
455 | */ |
||
456 | public function getAliases() |
||
461 | |||
462 | /** |
||
463 | * Set aliases that can be used in place of the command's primary name. |
||
464 | * |
||
465 | * @param string|string[] $aliases |
||
466 | */ |
||
467 | public function setAliases($aliases) |
||
475 | |||
476 | /** |
||
477 | * Return the examples for this command. This is @usage instead of |
||
478 | * @example because the later is defined by the phpdoc standard to |
||
479 | * be example method calls. |
||
480 | * |
||
481 | * @return string[] |
||
482 | */ |
||
483 | public function getExampleUsages() |
||
488 | |||
489 | /** |
||
490 | * Add an example usage for this command. |
||
491 | * |
||
492 | * @param string $usage An example of the command, including the command |
||
493 | * name and all of its example arguments and options. |
||
494 | * @param string $description An explanation of what the example does. |
||
495 | */ |
||
496 | public function setExampleUsage($usage, $description) |
||
501 | |||
502 | /** |
||
503 | * Return the topics for this command. |
||
504 | * |
||
505 | * @return string[] |
||
506 | */ |
||
507 | public function getTopics() |
||
515 | |||
516 | /** |
||
517 | * Return the list of refleaction parameters. |
||
518 | * |
||
519 | * @return ReflectionParameter[] |
||
520 | */ |
||
521 | public function getParameters() |
||
525 | |||
526 | /** |
||
527 | * Descriptions of commandline arguements for this command. |
||
528 | * |
||
529 | * @return DefaultsWithDescriptions |
||
530 | */ |
||
531 | public function arguments() |
||
535 | |||
536 | /** |
||
537 | * Descriptions of commandline options for this command. |
||
538 | * |
||
539 | * @return DefaultsWithDescriptions |
||
540 | */ |
||
541 | public function options() |
||
545 | |||
546 | /** |
||
547 | * Get the inputOptions for the options associated with this CommandInfo |
||
548 | * object, e.g. via @option annotations, or from |
||
549 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
550 | * parameter list. |
||
551 | * |
||
552 | * @return InputOption[] |
||
553 | */ |
||
554 | public function inputOptions() |
||
561 | |||
562 | protected function createInputOptions() |
||
587 | |||
588 | /** |
||
589 | * An option might have a name such as 'silent|s'. In this |
||
590 | * instance, we will allow the @option or @default tag to |
||
591 | * reference the option only by name (e.g. 'silent' or 's' |
||
592 | * instead of 'silent|s'). |
||
593 | * |
||
594 | * @param string $optionName |
||
595 | * @return string |
||
596 | */ |
||
597 | public function findMatchingOption($optionName) |
||
609 | |||
610 | /** |
||
611 | * @param string $optionName |
||
612 | * @return string |
||
613 | */ |
||
614 | protected function findOptionAmongAlternatives($optionName) |
||
629 | |||
630 | /** |
||
631 | * @param string $optionName |
||
632 | * @return string|null |
||
633 | */ |
||
634 | protected function findExistingOption($optionName) |
||
645 | |||
646 | /** |
||
647 | * Examine the parameters of the method for this command, and |
||
648 | * build a list of commandline arguements for them. |
||
649 | * |
||
650 | * @return array |
||
651 | */ |
||
652 | protected function determineAgumentClassifications() |
||
665 | |||
666 | /** |
||
667 | * Examine the provided parameter, and determine whether it |
||
668 | * is a parameter that will be filled in with a positional |
||
669 | * commandline argument. |
||
670 | */ |
||
671 | protected function addParameterToResult($result, $param) |
||
688 | |||
689 | /** |
||
690 | * Examine the parameters of the method for this command, and determine |
||
691 | * the disposition of the options from them. |
||
692 | * |
||
693 | * @return array |
||
694 | */ |
||
695 | protected function determineOptionsFromParameters() |
||
710 | |||
711 | /** |
||
712 | * Helper; determine if an array is associative or not. An array |
||
713 | * is not associative if its keys are numeric, and numbered sequentially |
||
714 | * from zero. All other arrays are considered to be associative. |
||
715 | * |
||
716 | * @param arrau $arr The array |
||
717 | * @return boolean |
||
718 | */ |
||
719 | protected function isAssoc($arr) |
||
726 | |||
727 | /** |
||
728 | * Convert from a method name to the corresponding command name. A |
||
729 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
730 | * become 'foo:bar-baz-boz'. |
||
731 | * |
||
732 | * @param string $camel method name. |
||
733 | * @return string |
||
734 | */ |
||
735 | protected function convertName($camel) |
||
742 | |||
743 | /** |
||
744 | * Parse the docBlock comment for this command, and set the |
||
745 | * fields of this class with the data thereby obtained. |
||
746 | */ |
||
747 | protected function parseDocBlock() |
||
756 | |||
757 | /** |
||
758 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
759 | * convert the data into the last of these forms. |
||
760 | */ |
||
761 | protected static function convertListToCommaSeparated($text) |
||
765 | } |
||
766 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.