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 |
||
20 | class RequestRateParser implements ParserInterface, RobotsTxtInterface |
||
21 | { |
||
22 | use DirectiveParserTrait; |
||
23 | |||
24 | /** |
||
25 | * Base uri |
||
26 | * @var string |
||
27 | */ |
||
28 | private $base; |
||
29 | |||
30 | /** |
||
31 | * RequestRate array |
||
32 | * @var array |
||
33 | */ |
||
34 | private $requestRates = []; |
||
35 | |||
36 | /** |
||
37 | * Sorted |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $sorted = false; |
||
41 | |||
42 | /** |
||
43 | * Time units |
||
44 | * @var int[] |
||
45 | */ |
||
46 | private $units = [ |
||
47 | 'w' => 604800, |
||
48 | 'd' => 86400, |
||
49 | 'h' => 3600, |
||
50 | 'm' => 60, |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * RequestRate constructor. |
||
55 | * |
||
56 | * @param string $base |
||
57 | */ |
||
58 | public function __construct($base) |
||
62 | |||
63 | /** |
||
64 | * Add |
||
65 | * |
||
66 | * @param string $line |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function add($line) |
||
96 | |||
97 | /** |
||
98 | * Get ratio string |
||
99 | * |
||
100 | * @param int $rate |
||
101 | * @param int $time |
||
102 | * @return string |
||
103 | */ |
||
104 | private function getRatio($rate, $time) |
||
119 | |||
120 | /** |
||
121 | * Returns the greatest common divisor of two integers using the Euclidean algorithm. |
||
122 | * |
||
123 | * @param int $a |
||
124 | * @param int $b |
||
125 | * @return int |
||
126 | */ |
||
127 | private function getGCD($a, $b) |
||
137 | |||
138 | /** |
||
139 | * Client |
||
140 | * |
||
141 | * @param string $userAgent |
||
142 | * @param float|int $fallbackValue |
||
143 | * @return RequestRateClient |
||
144 | */ |
||
145 | public function client($userAgent = self::USER_AGENT, $fallbackValue = 0) |
||
150 | |||
151 | /** |
||
152 | * Sort |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | private function sort() |
||
167 | |||
168 | /** |
||
169 | * Render |
||
170 | * |
||
171 | * @param RenderHandler $handler |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function render(RenderHandler $handler) |
||
188 | } |
||
189 |
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.