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 | protected $fqcnCache; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $tagProcessors = [ |
||
| 20 | 'command' => 'processCommandTag', |
||
| 21 | 'name' => 'processCommandTag', |
||
| 22 | 'arg' => 'processArgumentTag', |
||
| 23 | 'param' => 'processParamTag', |
||
| 24 | 'return' => 'processReturnTag', |
||
| 25 | 'option' => 'processOptionTag', |
||
| 26 | 'default' => 'processDefaultTag', |
||
| 27 | 'aliases' => 'processAliases', |
||
| 28 | 'usage' => 'processUsageTag', |
||
| 29 | 'description' => 'processAlternateDescriptionTag', |
||
| 30 | 'desc' => 'processAlternateDescriptionTag', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection, $fqcnCache = null) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Parse the docBlock comment for this command, and set the |
||
| 42 | * fields of this class with the data thereby obtained. |
||
| 43 | */ |
||
| 44 | public function parse() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Save any tag that we do not explicitly recognize in the |
||
| 52 | * 'otherAnnotations' map. |
||
| 53 | */ |
||
| 54 | protected function processGenericTag($tag) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set the name of the command from a @command or @name annotation. |
||
| 61 | */ |
||
| 62 | protected function processCommandTag($tag) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The @description and @desc annotations may be used in |
||
| 76 | * place of the synopsis (which we call 'description'). |
||
| 77 | * This is discouraged. |
||
| 78 | * |
||
| 79 | * @deprecated |
||
| 80 | */ |
||
| 81 | protected function processAlternateDescriptionTag($tag) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Store the data from a @param annotation in our argument descriptions. |
||
| 88 | */ |
||
| 89 | protected function processParamTag($tag) |
||
| 98 | |||
| 99 | protected function ignoredParamType($paramType) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Store the data from a @arg annotation in our argument descriptions. |
||
| 113 | */ |
||
| 114 | View Code Duplication | protected function processArgumentTag($tag) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Store the data from an @option annotation in our option descriptions. |
||
| 127 | */ |
||
| 128 | View Code Duplication | protected function processOptionTag($tag) |
|
| 135 | |||
| 136 | protected function addOptionOrArgumentTag($tag, DefaultsWithDescriptions $set, $name, $description) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Store the data from a @default annotation in our argument or option store, |
||
| 145 | * as appropriate. |
||
| 146 | */ |
||
| 147 | protected function processDefaultTag($tag) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Store the data from a @usage annotation in our example usage list. |
||
| 166 | */ |
||
| 167 | protected function processUsageTag($tag) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Process the comma-separated list of aliases |
||
| 180 | */ |
||
| 181 | protected function processAliases($tag) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Store the data from a @return annotation in our argument descriptions. |
||
| 188 | */ |
||
| 189 | protected function processReturnTag($tag) |
||
| 201 | |||
| 202 | protected function findFullyQualifiedClass($className) |
||
| 210 | |||
| 211 | private function parseDocBlock($doc) |
||
| 238 | |||
| 239 | protected function processDescriptionAndHelp($lines) |
||
| 262 | |||
| 263 | protected function nextLineIsNotEmpty($lines) |
||
| 272 | |||
| 273 | protected function processAllTags($tags) |
||
| 284 | |||
| 285 | protected function lastParameterName() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return the name of the last parameter if it holds the options. |
||
| 297 | */ |
||
| 298 | public function optionParamName() |
||
| 312 | |||
| 313 | protected function interpretDefaultValue($defaultValue) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 332 | * convert the data into the last of these forms. |
||
| 333 | */ |
||
| 334 | protected static function convertListToCommaSeparated($text) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Take a multiline description and convert it into a single |
||
| 341 | * long unbroken line. |
||
| 342 | */ |
||
| 343 | protected static function removeLineBreaks($text) |
||
| 347 | } |
||
| 348 |
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: