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 DocSummaryHelper |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The php cs 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 | * Token stack of the current file. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $tokens; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * DocSummaryHelper constructor. |
||
| 39 | * |
||
| 40 | * @param PHP_CodeSniffer_File $file The php cs file |
||
| 41 | * @param DocHelper $docHelper The doc comment helper |
||
| 42 | */ |
||
| 43 | 109 | public function __construct(PHP_CodeSniffer_File $file, DocHelper $docHelper) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Returns pointer to the comment summary. |
||
| 52 | * |
||
| 53 | * @return bool|int Pointer to the token or false |
||
| 54 | */ |
||
| 55 | 101 | public function getCommentSummaryPointer() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Returns comment summary token. |
||
| 77 | * |
||
| 78 | * @return array Summary token array |
||
| 79 | */ |
||
| 80 | 97 | public function getCommentSummaryToken() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Check class comment summary. |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | 101 | public function checkCommentSummary() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Checks that the first character of the summary is upper case. |
||
| 118 | * |
||
| 119 | * @param array $summaryToken Token of the summary |
||
| 120 | * @param int $summaryPtr Pointer to the summary |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 97 | private function checkSummaryCapitalLetter(array $summaryToken, $summaryPtr) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Checks if the line length of the summary is maximum 120 chars. |
||
| 145 | * |
||
| 146 | * @param array $summaryToken Token array of the summary |
||
| 147 | * @param int $summaryPtr Pointer of the summary |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | 97 | private function checkSummaryLineLength($summaryToken, $summaryPtr) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Checks if the summary is the first line of the comment. |
||
| 166 | * |
||
| 167 | * @param array $commentStartToken Token array of the comment start |
||
| 168 | * @param array $summaryToken Token of the summary |
||
| 169 | * @param int $summaryPtr Pointer to the summary |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | 97 | private function checkSummaryIsFirstLine($commentStartToken, $summaryToken, $summaryPtr) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Checks the line after the summary. |
||
| 190 | * |
||
| 191 | * @param int $summaryPtr Pointer to the summary |
||
| 192 | * @param int $commentEndPtr Pointer to the end of the doc comment |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | 97 | private function checkLineAfterSummary($summaryPtr, $commentEndPtr) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Fixes no line after summary. |
||
| 229 | * |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | 4 | View Code Duplication | private function fixNoLineAfterSummary() |
| 246 | |||
| 247 | /** |
||
| 248 | * Fixes summary not first statement. |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | 4 | private function fixSummaryNotFirst() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Fixes the first letter of the summary to be uppercase. |
||
| 275 | * |
||
| 276 | * @param array $summaryToken Token array of the summary |
||
| 277 | * @param int $summaryPtr Pointer to the summary |
||
| 278 | * |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | 4 | View Code Duplication | private function fixSummaryUcFirst(array $summaryToken, $summaryPtr) |
| 289 | } |
||
| 290 |
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.