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 BespokeDocBlockParser 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 BespokeDocBlockParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class BespokeDocBlockParser |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $tagProcessors = [ |
||
18 | 'command' => 'processCommandTag', |
||
19 | 'name' => 'processCommandTag', |
||
20 | 'arg' => 'processArgumentTag', |
||
21 | 'param' => 'processArgumentTag', |
||
22 | 'return' => 'processReturnTag', |
||
23 | 'option' => 'processOptionTag', |
||
24 | 'default' => 'processDefaultTag', |
||
25 | 'aliases' => 'processAliases', |
||
26 | 'usage' => 'processUsageTag', |
||
27 | 'description' => 'processAlternateDescriptionTag', |
||
28 | 'desc' => 'processAlternateDescriptionTag', |
||
29 | ]; |
||
30 | |||
31 | public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection) |
||
36 | |||
37 | /** |
||
38 | * Parse the docBlock comment for this command, and set the |
||
39 | * fields of this class with the data thereby obtained. |
||
40 | */ |
||
41 | public function parse() |
||
46 | |||
47 | /** |
||
48 | * Save any tag that we do not explicitly recognize in the |
||
49 | * 'otherAnnotations' map. |
||
50 | */ |
||
51 | protected function processGenericTag($tag) |
||
55 | |||
56 | /** |
||
57 | * Set the name of the command from a @command or @name annotation. |
||
58 | */ |
||
59 | protected function processCommandTag($tag) |
||
70 | |||
71 | /** |
||
72 | * The @description and @desc annotations may be used in |
||
73 | * place of the synopsis (which we call 'description'). |
||
74 | * This is discouraged. |
||
75 | * |
||
76 | * @deprecated |
||
77 | */ |
||
78 | protected function processAlternateDescriptionTag($tag) |
||
82 | |||
83 | /** |
||
84 | * Store the data from a @arg annotation in our argument descriptions. |
||
85 | */ |
||
86 | View Code Duplication | protected function processArgumentTag($tag) |
|
96 | |||
97 | /** |
||
98 | * Store the data from an @option annotation in our option descriptions. |
||
99 | */ |
||
100 | View Code Duplication | protected function processOptionTag($tag) |
|
107 | |||
108 | protected function addOptionOrArgumentTag($tag, DefaultsWithDescriptions $set, $name, $description) |
||
114 | |||
115 | /** |
||
116 | * Store the data from a @default annotation in our argument or option store, |
||
117 | * as appropriate. |
||
118 | */ |
||
119 | View Code Duplication | protected function processDefaultTag($tag) |
|
135 | |||
136 | /** |
||
137 | * Store the data from a @usage annotation in our example usage list. |
||
138 | */ |
||
139 | protected function processUsageTag($tag) |
||
149 | |||
150 | /** |
||
151 | * Process the comma-separated list of aliases |
||
152 | */ |
||
153 | protected function processAliases($tag) |
||
157 | |||
158 | /** |
||
159 | * Store the data from a @return annotation in our argument descriptions. |
||
160 | */ |
||
161 | protected function processReturnTag($tag) |
||
170 | |||
171 | private function parseDocBlock($doc) |
||
198 | |||
199 | protected function processDescriptionAndHelp($lines) |
||
222 | |||
223 | protected function nextLineIsNotEmpty($lines) |
||
232 | |||
233 | View Code Duplication | protected function processAllTags($tags) |
|
244 | |||
245 | View Code Duplication | protected function lastParameterName() |
|
254 | |||
255 | /** |
||
256 | * Return the name of the last parameter if it holds the options. |
||
257 | */ |
||
258 | View Code Duplication | public function optionParamName() |
|
272 | |||
273 | View Code Duplication | protected function interpretDefaultValue($defaultValue) |
|
289 | |||
290 | /** |
||
291 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
292 | * convert the data into the last of these forms. |
||
293 | */ |
||
294 | protected static function convertListToCommaSeparated($text) |
||
298 | |||
299 | /** |
||
300 | * Take a multiline description and convert it into a single |
||
301 | * long unbroken line. |
||
302 | */ |
||
303 | protected static function removeLineBreaks($text) |
||
307 | } |
||
308 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: