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 |
||
| 14 | class DocDescriptionHelper |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The php code sniffer file. |
||
| 18 | * |
||
| 19 | * @var PHP_CodeSniffer_File |
||
| 20 | */ |
||
| 21 | private $file; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The doc comment helper. |
||
| 25 | * |
||
| 26 | * @var DocHelper |
||
| 27 | */ |
||
| 28 | private $docHelper; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The token stack of the cs file. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $tokens; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The doc comment summary helper. |
||
| 39 | * |
||
| 40 | * @var DocSummaryHelper |
||
| 41 | */ |
||
| 42 | private $summaryHelper; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Indicator if a description is required. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $descriptionRequired; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * DocSummaryHelper constructor. |
||
| 53 | * |
||
| 54 | * @param PHP_CodeSniffer_File $file The php cs file |
||
| 55 | * @param DocHelper $docHelper The doc comment helper |
||
| 56 | * @param DocSummaryHelper $summaryHelper The doc comment summary helper |
||
| 57 | */ |
||
| 58 | 109 | public function __construct(PHP_CodeSniffer_File $file, DocHelper $docHelper, DocSummaryHelper $summaryHelper) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Checks for comment description. |
||
| 68 | * |
||
| 69 | * @param bool $descriptionRequired Indicator if the description is required. |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | 101 | public function checkCommentDescription($descriptionRequired) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Checks if the description starts with a capital letter. |
||
| 147 | * |
||
| 148 | * @param int $descriptionStartPtr Pointer to the start of the description. |
||
| 149 | * |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | 86 | View Code Duplication | private function checkCommentDescriptionUcFirst($descriptionStartPtr) |
| 172 | |||
| 173 | /** |
||
| 174 | * Checks the line length of each line of the comment description. |
||
| 175 | * |
||
| 176 | * @param int $descriptionStartPtr Pointer to the start of the description. |
||
| 177 | * @param int $descriptionEndPtr Pointer to the end of the description. |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | 86 | private function checkCommentDescriptionLineLength($descriptionStartPtr, $descriptionEndPtr) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Returns pointer to the end of the description. |
||
| 211 | * |
||
| 212 | * @return int|bool Pointer to the end of the description or false |
||
| 213 | */ |
||
| 214 | 86 | View Code Duplication | private function getCommentDescriptionEndPointer() |
| 239 | |||
| 240 | /** |
||
| 241 | * Returns pointer to the start of the long description or false if not found. |
||
| 242 | * |
||
| 243 | * @return bool|int Pointer to the start of the description or false |
||
| 244 | */ |
||
| 245 | 97 | View Code Duplication | private function getCommentDescriptionStartPointer() |
| 270 | |||
| 271 | /** |
||
| 272 | * Adds error when description is not found. |
||
| 273 | * |
||
| 274 | * @param int $summaryPtr Pointer to summary token. |
||
| 275 | * |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | 11 | private function addDescriptionNotFoundError($summaryPtr) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Fixes no line after description. |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | 8 | View Code Duplication | private function fixNoLineAfterDescription() |
| 308 | |||
| 309 | /** |
||
| 310 | * Fixes much lines after description. |
||
| 311 | * |
||
| 312 | * @param int $startLine Line to start removing |
||
| 313 | * @param int $endLine Line to end removing |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | 8 | private function fixMuchLinesAfterDescription($startLine, $endLine) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Fixes the description uc first. |
||
| 328 | * |
||
| 329 | * @param int $descriptionStartPtr Pointer to the description start |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | 4 | View Code Duplication | private function fixDescriptionUcFirst($descriptionStartPtr) |
| 346 | } |
||
| 347 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.