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 = 3; |
||
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 = []) |
||
118 | |||
119 | public static function create($classNameOrInstance, $methodName) |
||
123 | |||
124 | public static function deserialize($cache) |
||
129 | |||
130 | public function cachedFileIsModified($cache) |
||
135 | |||
136 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
145 | |||
146 | /** |
||
147 | * Recover the method name provided to the constructor. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getMethodName() |
||
155 | |||
156 | /** |
||
157 | * Return the primary name for this command. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getName() |
||
166 | |||
167 | /** |
||
168 | * Set the primary name for this command. |
||
169 | * |
||
170 | * @param string $name |
||
171 | */ |
||
172 | public function setName($name) |
||
177 | |||
178 | /** |
||
179 | * Return whether or not this method represents a valid command |
||
180 | * or hook. |
||
181 | */ |
||
182 | public function valid() |
||
186 | |||
187 | /** |
||
188 | * If higher-level code decides that this CommandInfo is not interesting |
||
189 | * or useful (if it is not a command method or a hook method), then |
||
190 | * we will mark it as invalid to prevent it from being created as a command. |
||
191 | * We still cache a placeholder record for invalid methods, so that we |
||
192 | * do not need to re-parse the method again later simply to determine that |
||
193 | * it is invalid. |
||
194 | */ |
||
195 | public function invalidate() |
||
199 | |||
200 | public function getReturnType() |
||
205 | |||
206 | public function setReturnType($returnType) |
||
211 | |||
212 | /** |
||
213 | * Get any annotations included in the docblock comment for the |
||
214 | * implementation method of this command that are not already |
||
215 | * handled by the primary methods of this class. |
||
216 | * |
||
217 | * @return AnnotationData |
||
218 | */ |
||
219 | public function getRawAnnotations() |
||
224 | |||
225 | /** |
||
226 | * Replace the annotation data. |
||
227 | */ |
||
228 | public function replaceRawAnnotations($annotationData) |
||
233 | |||
234 | /** |
||
235 | * Get any annotations included in the docblock comment, |
||
236 | * also including default values such as @command. We add |
||
237 | * in the default @command annotation late, and only in a |
||
238 | * copy of the annotation data because we use the existance |
||
239 | * of a @command to indicate that this CommandInfo is |
||
240 | * a command, and not a hook or anything else. |
||
241 | * |
||
242 | * @return AnnotationData |
||
243 | */ |
||
244 | public function getAnnotations() |
||
259 | |||
260 | /** |
||
261 | * Return a specific named annotation for this command. |
||
262 | * |
||
263 | * @param string $annotation The name of the annotation. |
||
264 | * @return string |
||
265 | */ |
||
266 | View Code Duplication | public function getAnnotation($annotation) |
|
274 | |||
275 | /** |
||
276 | * Convert a string to a csv. |
||
277 | */ |
||
278 | protected function csvEscape($data, $delimiter = ',') |
||
287 | |||
288 | /** |
||
289 | * Return a specific named annotation for this command. |
||
290 | * |
||
291 | * @param string $annotation The name of the annotation. |
||
292 | * @return string |
||
293 | */ |
||
294 | View Code Duplication | public function getAnnotationList($annotation) |
|
302 | |||
303 | /** |
||
304 | * Return a specific named annotation for this command. |
||
305 | * |
||
306 | * @param string $annotation The name of the annotation. |
||
307 | * @return string |
||
308 | */ |
||
309 | protected function getAnnotationData($annotation) |
||
317 | |||
318 | /** |
||
319 | * Check to see if the specified annotation exists for this command. |
||
320 | * |
||
321 | * @param string $annotation The name of the annotation. |
||
322 | * @return boolean |
||
323 | */ |
||
324 | public function hasAnnotation($annotation) |
||
329 | |||
330 | /** |
||
331 | * Save any tag that we do not explicitly recognize in the |
||
332 | * 'otherAnnotations' map. |
||
333 | */ |
||
334 | public function addAnnotation($name, $content) |
||
343 | |||
344 | /** |
||
345 | * Remove an annotation that was previoudly set. |
||
346 | */ |
||
347 | public function removeAnnotation($name) |
||
351 | |||
352 | /** |
||
353 | * Get the synopsis of the command (~first line). |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getDescription() |
||
362 | |||
363 | /** |
||
364 | * Set the command description. |
||
365 | * |
||
366 | * @param string $description The description to set. |
||
367 | */ |
||
368 | public function setDescription($description) |
||
373 | |||
374 | /** |
||
375 | * Get the help text of the command (the description) |
||
376 | */ |
||
377 | public function getHelp() |
||
382 | /** |
||
383 | * Set the help text for this command. |
||
384 | * |
||
385 | * @param string $help The help text. |
||
386 | */ |
||
387 | public function setHelp($help) |
||
392 | |||
393 | /** |
||
394 | * Return the list of aliases for this command. |
||
395 | * @return string[] |
||
396 | */ |
||
397 | public function getAliases() |
||
402 | |||
403 | /** |
||
404 | * Set aliases that can be used in place of the command's primary name. |
||
405 | * |
||
406 | * @param string|string[] $aliases |
||
407 | */ |
||
408 | public function setAliases($aliases) |
||
416 | |||
417 | /** |
||
418 | * Return the examples for this command. This is @usage instead of |
||
419 | * @example because the later is defined by the phpdoc standard to |
||
420 | * be example method calls. |
||
421 | * |
||
422 | * @return string[] |
||
423 | */ |
||
424 | public function getExampleUsages() |
||
429 | |||
430 | /** |
||
431 | * Add an example usage for this command. |
||
432 | * |
||
433 | * @param string $usage An example of the command, including the command |
||
434 | * name and all of its example arguments and options. |
||
435 | * @param string $description An explanation of what the example does. |
||
436 | */ |
||
437 | public function setExampleUsage($usage, $description) |
||
442 | |||
443 | /** |
||
444 | * Overwrite all example usages |
||
445 | */ |
||
446 | public function replaceExampleUsages($usages) |
||
451 | |||
452 | /** |
||
453 | * Return the topics for this command. |
||
454 | * |
||
455 | * @return string[] |
||
456 | */ |
||
457 | public function getTopics() |
||
465 | |||
466 | /** |
||
467 | * Return the list of refleaction parameters. |
||
468 | * |
||
469 | * @return ReflectionParameter[] |
||
470 | */ |
||
471 | public function getParameters() |
||
475 | |||
476 | /** |
||
477 | * Descriptions of commandline arguements for this command. |
||
478 | * |
||
479 | * @return DefaultsWithDescriptions |
||
480 | */ |
||
481 | public function arguments() |
||
485 | |||
486 | /** |
||
487 | * Descriptions of commandline options for this command. |
||
488 | * |
||
489 | * @return DefaultsWithDescriptions |
||
490 | */ |
||
491 | public function options() |
||
495 | |||
496 | /** |
||
497 | * Get the inputOptions for the options associated with this CommandInfo |
||
498 | * object, e.g. via @option annotations, or from |
||
499 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
500 | * parameter list. |
||
501 | * |
||
502 | * @return InputOption[] |
||
503 | */ |
||
504 | public function inputOptions() |
||
511 | |||
512 | protected function createInputOptions() |
||
546 | |||
547 | /** |
||
548 | * An option might have a name such as 'silent|s'. In this |
||
549 | * instance, we will allow the @option or @default tag to |
||
550 | * reference the option only by name (e.g. 'silent' or 's' |
||
551 | * instead of 'silent|s'). |
||
552 | * |
||
553 | * @param string $optionName |
||
554 | * @return string |
||
555 | */ |
||
556 | public function findMatchingOption($optionName) |
||
568 | |||
569 | /** |
||
570 | * @param string $optionName |
||
571 | * @return string |
||
572 | */ |
||
573 | protected function findOptionAmongAlternatives($optionName) |
||
588 | |||
589 | /** |
||
590 | * @param string $optionName |
||
591 | * @return string|null |
||
592 | */ |
||
593 | protected function findExistingOption($optionName) |
||
604 | |||
605 | /** |
||
606 | * Examine the parameters of the method for this command, and |
||
607 | * build a list of commandline arguements for them. |
||
608 | * |
||
609 | * @return array |
||
610 | */ |
||
611 | protected function determineAgumentClassifications() |
||
624 | |||
625 | /** |
||
626 | * Examine the provided parameter, and determine whether it |
||
627 | * is a parameter that will be filled in with a positional |
||
628 | * commandline argument. |
||
629 | */ |
||
630 | protected function addParameterToResult($result, $param) |
||
647 | |||
648 | /** |
||
649 | * Examine the parameters of the method for this command, and determine |
||
650 | * the disposition of the options from them. |
||
651 | * |
||
652 | * @return array |
||
653 | */ |
||
654 | protected function determineOptionsFromParameters() |
||
669 | |||
670 | /** |
||
671 | * Helper; determine if an array is associative or not. An array |
||
672 | * is not associative if its keys are numeric, and numbered sequentially |
||
673 | * from zero. All other arrays are considered to be associative. |
||
674 | * |
||
675 | * @param array $arr The array |
||
676 | * @return boolean |
||
677 | */ |
||
678 | protected function isAssoc($arr) |
||
685 | |||
686 | /** |
||
687 | * Convert from a method name to the corresponding command name. A |
||
688 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
689 | * become 'foo:bar-baz-boz'. |
||
690 | * |
||
691 | * @param string $camel method name. |
||
692 | * @return string |
||
693 | */ |
||
694 | protected function convertName($camel) |
||
701 | |||
702 | /** |
||
703 | * Parse the docBlock comment for this command, and set the |
||
704 | * fields of this class with the data thereby obtained. |
||
705 | */ |
||
706 | protected function parseDocBlock() |
||
715 | |||
716 | /** |
||
717 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
718 | * convert the data into the last of these forms. |
||
719 | */ |
||
720 | protected static function convertListToCommaSeparated($text) |
||
724 | } |
||
725 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.