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 |
||
28 | final class LeakyBucketSettings implements ThrottleSettingsInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var int|null |
||
32 | */ |
||
33 | private $tokenLimit; |
||
34 | |||
35 | /** |
||
36 | * @var int|null |
||
37 | */ |
||
38 | private $timeLimit; |
||
39 | |||
40 | /** |
||
41 | * @var int|null |
||
42 | */ |
||
43 | private $threshold; |
||
44 | |||
45 | /** |
||
46 | * @var int|null |
||
47 | */ |
||
48 | private $cacheTtl; |
||
49 | |||
50 | /** |
||
51 | * @param int|null $tokenLimit |
||
52 | * @param int|null $timeLimit In milliseconds |
||
53 | * @param int|null $threshold |
||
54 | * @param int|null $cacheTtl In seconds |
||
55 | */ |
||
56 | 15 | public function __construct($tokenLimit = null, $timeLimit = null, $threshold = null, $cacheTtl = null) |
|
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | 3 | public function merge(ThrottleSettingsInterface $settings) |
|
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | 12 | public function isValid() |
|
93 | |||
94 | /** |
||
95 | * @return int|null |
||
96 | */ |
||
97 | 8 | public function getTokenLimit() |
|
101 | |||
102 | /** |
||
103 | * @return int|null |
||
104 | */ |
||
105 | 8 | public function getTimeLimit() |
|
109 | |||
110 | /** |
||
111 | * @return int|null |
||
112 | */ |
||
113 | 8 | public function getThreshold() |
|
117 | |||
118 | /** |
||
119 | * @return int|null |
||
120 | */ |
||
121 | 8 | public function getCacheTtl() |
|
125 | } |
||
126 |
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.