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 |
||
32 | final class LeakyBucketThrottler implements RetriableThrottlerInterface |
||
33 | { |
||
34 | const TIME_CACHE_KEY = ':time'; |
||
35 | const TOKEN_CACHE_KEY = ':tokens'; |
||
36 | |||
37 | /** |
||
38 | * @var CacheAdapterInterface |
||
39 | */ |
||
40 | private $cache; |
||
41 | |||
42 | /** |
||
43 | * @var int|null |
||
44 | */ |
||
45 | private $cacheTtl; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $key; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | private $threshold; |
||
56 | |||
57 | /** |
||
58 | * @var TimeAdapterInterface |
||
59 | */ |
||
60 | private $timeProvider; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | private $timeLimit; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | private $tokenlimit; |
||
71 | |||
72 | /** |
||
73 | * @param CacheAdapterInterface $cache |
||
74 | * @param TimeAdapterInterface $timeAdapter |
||
75 | * @param string $key Cache key prefix |
||
76 | * @param int $tokenLimit Bucket capacity |
||
77 | * @param int $timeLimit Refill time in milliseconds |
||
78 | * @param int|null $threshold Capacity threshold on which to start throttling (default: 0) |
||
79 | * @param int|null $cacheTtl Cache ttl time (default: null => CacheAdapter ttl) |
||
80 | */ |
||
81 | 16 | public function __construct( |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 3 | public function access() |
|
106 | |||
107 | /** |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | 8 | public function hit() |
|
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | 7 | public function clear() |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 14 | public function count() |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 3 | public function check() |
|
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | public function getTime() |
||
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | public function getLimit() |
||
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 2 | View Code Duplication | public function getRetryTimeout() |
189 | |||
190 | /** |
||
191 | * @param int $tokenCount |
||
192 | * |
||
193 | * @return int |
||
194 | */ |
||
195 | 9 | View Code Duplication | private function getWaitTime($tokenCount) |
203 | |||
204 | /** |
||
205 | * @param int $tokens |
||
206 | */ |
||
207 | 10 | private function setUsedCapacity($tokens) |
|
212 | } |
||
213 |
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.