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:
1 | <?php |
||
12 | abstract class AbstractCommandDocBlockParser |
||
13 | { |
||
14 | /** |
||
15 | * @var CommandInfo |
||
16 | */ |
||
17 | protected $commandInfo; |
||
18 | |||
19 | /** |
||
20 | * @var \ReflectionMethod |
||
21 | */ |
||
22 | protected $reflection; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $optionParamName; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $tagProcessors = [ |
||
33 | 'command' => 'processCommandTag', |
||
34 | 'name' => 'processCommandTag', |
||
35 | 'arg' => 'processArgumentTag', |
||
36 | 'param' => 'processParamTag', |
||
37 | 'return' => 'processReturnTag', |
||
38 | 'option' => 'processOptionTag', |
||
39 | 'default' => 'processDefaultTag', |
||
40 | 'aliases' => 'processAliases', |
||
41 | 'usage' => 'processUsageTag', |
||
42 | 'description' => 'processAlternateDescriptionTag', |
||
43 | 'desc' => 'processAlternateDescriptionTag', |
||
44 | ]; |
||
45 | |||
46 | public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection) |
||
51 | |||
52 | protected function processAllTags($phpdoc) |
||
63 | |||
64 | abstract protected function getTagContents($tag); |
||
65 | |||
66 | /** |
||
67 | * Parse the docBlock comment for this command, and set the |
||
68 | * fields of this class with the data thereby obtained. |
||
69 | */ |
||
70 | abstract public function parse(); |
||
71 | |||
72 | /** |
||
73 | * Save any tag that we do not explicitly recognize in the |
||
74 | * 'otherAnnotations' map. |
||
75 | */ |
||
76 | protected function processGenericTag($tag) |
||
80 | |||
81 | /** |
||
82 | * Set the name of the command from a @command or @name annotation. |
||
83 | */ |
||
84 | protected function processCommandTag($tag) |
||
92 | |||
93 | /** |
||
94 | * The @description and @desc annotations may be used in |
||
95 | * place of the synopsis (which we call 'description'). |
||
96 | * This is discouraged. |
||
97 | * |
||
98 | * @deprecated |
||
99 | */ |
||
100 | protected function processAlternateDescriptionTag($tag) |
||
104 | |||
105 | /** |
||
106 | * Store the data from a @arg annotation in our argument descriptions. |
||
107 | */ |
||
108 | View Code Duplication | protected function processArgumentTag($tag) |
|
115 | |||
116 | /** |
||
117 | * Store the data from an @option annotation in our option descriptions. |
||
118 | */ |
||
119 | protected function processOptionTag($tag) |
||
126 | |||
127 | protected function addOptionOrArgumentTag($tag, DefaultsWithDescriptions $set, $nameAndDescription) |
||
134 | |||
135 | /** |
||
136 | * Store the data from a @default annotation in our argument or option store, |
||
137 | * as appropriate. |
||
138 | */ |
||
139 | protected function processDefaultTag($tag) |
||
155 | |||
156 | /** |
||
157 | * Store the data from a @usage annotation in our example usage list. |
||
158 | */ |
||
159 | protected function processUsageTag($tag) |
||
167 | |||
168 | /** |
||
169 | * Process the comma-separated list of aliases |
||
170 | */ |
||
171 | protected function processAliases($tag) |
||
175 | |||
176 | protected function lastParameterName() |
||
185 | |||
186 | /** |
||
187 | * Return the name of the last parameter if it holds the options. |
||
188 | */ |
||
189 | public function optionParamName() |
||
203 | |||
204 | /** |
||
205 | * Store the data from a @param annotation in our argument descriptions. |
||
206 | */ |
||
207 | protected function processParamTag($tag) |
||
217 | |||
218 | /** |
||
219 | * Store the data from a @return annotation in our argument descriptions. |
||
220 | */ |
||
221 | abstract protected function processReturnTag($tag); |
||
222 | |||
223 | protected function interpretDefaultValue($defaultValue) |
||
239 | |||
240 | /** |
||
241 | * Given a docblock description in the form "$variable description", |
||
242 | * return the variable name and description via the 'match' parameter. |
||
243 | */ |
||
244 | protected function pregMatchNameAndDescription($source, &$match) |
||
252 | |||
253 | /** |
||
254 | * Given a docblock description in the form "$variable description", |
||
255 | * return the variable name and description via the 'match' parameter. |
||
256 | */ |
||
257 | protected function pregMatchOptionNameAndDescription($source, &$match) |
||
267 | |||
268 | /** |
||
269 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
270 | * convert the data into the last of these forms. |
||
271 | */ |
||
272 | protected static function convertListToCommaSeparated($text) |
||
276 | |||
277 | /** |
||
278 | * Take a multiline description and convert it into a single |
||
279 | * long unbroken line. |
||
280 | */ |
||
281 | protected static function removeLineBreaks($text) |
||
285 | } |
||
286 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.