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 |
||
17 | class Assert |
||
18 | { |
||
19 | /** |
||
20 | * @param float $value |
||
21 | * @param string $message |
||
22 | */ |
||
23 | 16 | View Code Duplication | public static function latitude($value, string $message = '') |
30 | |||
31 | 16 | /** |
|
32 | * @param float $value |
||
33 | * @param string $message |
||
34 | */ |
||
35 | View Code Duplication | public static function longitude($value, string $message = '') |
|
42 | 16 | ||
43 | /** |
||
44 | 16 | * @param mixed $value |
|
45 | * @param string $message |
||
46 | */ |
||
47 | public static function notNull($value, string $message = '') |
||
53 | |||
54 | private static function typeToString($value): string |
||
58 | |||
59 | /** |
||
60 | * @param $value |
||
61 | 16 | * @param $message |
|
62 | */ |
||
63 | 16 | private static function float($value, string $message) |
|
71 | } |
||
72 |
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.