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 |
||
5 | class Level { |
||
6 | const NONE = 0; // TODO: Get rid of this *level* <[email protected]> |
||
7 | const PATCH = 1; |
||
8 | const MINOR = 2; |
||
9 | const MAJOR = 3; |
||
10 | |||
11 | /** |
||
12 | * @param string $order |
||
13 | * @return array |
||
14 | */ |
||
15 | 61 | public static function asList($order = 'asc') |
|
29 | |||
30 | /** |
||
31 | * @param int $level |
||
32 | * @return string |
||
33 | */ |
||
34 | 2 | View Code Duplication | public static function toString($level) |
45 | |||
46 | /** |
||
47 | * @param string $level |
||
48 | * @return int |
||
49 | */ |
||
50 | 4 | View Code Duplication | public static function fromString($level) |
61 | } |
||
62 |
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.