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 | class ElasticWindowThrottler implements RetriableThrottlerInterface, \Countable |
||
33 | { |
||
34 | /* @var ThrottlerCacheInterface */ |
||
35 | protected $cache; |
||
36 | /* @var string */ |
||
37 | protected $key; |
||
38 | /* @var int */ |
||
39 | protected $limit; |
||
40 | /* @var int */ |
||
41 | protected $ttl; |
||
42 | /* @var int */ |
||
43 | protected $counter; |
||
44 | |||
45 | /** |
||
46 | * @param ThrottlerCacheInterface $cache |
||
47 | * @param string $key |
||
48 | * @param int $limit |
||
49 | * @param int $ttl |
||
50 | */ |
||
51 | 7 | public function __construct(ThrottlerCacheInterface $cache, $key, $limit, $ttl) |
|
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | 2 | public function access() |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 5 | View Code Duplication | public function hit() |
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | 1 | View Code Duplication | public function clear() |
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | 5 | public function count() |
|
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | 4 | public function check() |
|
124 | |||
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | public function getTime() |
||
132 | |||
133 | /** |
||
134 | * @inheritdoc |
||
135 | */ |
||
136 | public function getLimit() |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public function getRetryTimeout() |
||
152 | } |
||
153 |
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.