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 |
||
| 8 | class Parser |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Parses a part of the content MIME type string. |
||
| 12 | * |
||
| 13 | * Splits string and comment until a delimiter is found. |
||
| 14 | * |
||
| 15 | * @param string $string Input string. |
||
| 16 | * @param int $offset Offset to start parsing from. |
||
| 17 | * @param string $delimiter Stop parsing when delimiter found. |
||
| 18 | * |
||
| 19 | * @return array An array with the following keys: |
||
| 20 | * 'string' - the uncommented part of $string |
||
| 21 | * 'comment' - the comment part of $string |
||
| 22 | * 'delimiter_matched' - true if a $delimiter stopped the parsing, false |
||
| 23 | * otherwise |
||
| 24 | * 'end_offset' - the last position parsed in $string. |
||
| 25 | */ |
||
| 26 | public static function parseStringPart($string, $offset, $delimiter) |
||
| 102 | } |
||
| 103 |
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.