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 |
||
12 | class Ranges |
||
13 | { |
||
14 | /** |
||
15 | * @param int $minimumLength |
||
16 | * @param int $maximumLength |
||
17 | * |
||
18 | * @return \Closure |
||
19 | */ |
||
20 | 4 | public static function stringWithLengthBetween(int $minimumLength, int $maximumLength): \Closure |
|
30 | |||
31 | /** |
||
32 | * @param int $minimum |
||
33 | * @param int $maximum |
||
34 | * |
||
35 | * @return \Closure |
||
36 | */ |
||
37 | 4 | View Code Duplication | public static function intBetween(int $minimum, int $maximum): \Closure |
44 | |||
45 | /** |
||
46 | * @param float $minimum |
||
47 | * @param float $maximum |
||
48 | * |
||
49 | * @return \Closure |
||
50 | */ |
||
51 | 1 | View Code Duplication | public static function floatBetween(float $minimum, float $maximum): \Closure |
58 | |||
59 | /** |
||
60 | * @param array ...$allowedValues |
||
61 | * |
||
62 | * @return \Closure |
||
63 | */ |
||
64 | 1 | public static function enum(...$allowedValues): \Closure |
|
71 | |||
72 | /** |
||
73 | * @param array ...$allowedTypes |
||
74 | * |
||
75 | * @return \Closure |
||
76 | */ |
||
77 | 1 | public static function typeEnum(...$allowedTypes): \Closure |
|
84 | |||
85 | /** |
||
86 | * @param array ...$allowedValues |
||
87 | * |
||
88 | * @return \Closure |
||
89 | */ |
||
90 | 3 | public static function stringOneOf(...$allowedValues): \Closure |
|
100 | } |
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.