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 | * RequestRate constructor. |
||
44 | * |
||
45 | * @param string $base |
||
46 | */ |
||
47 | public function __construct($base) |
||
51 | |||
52 | /** |
||
53 | * Add |
||
54 | * |
||
55 | * @param string $line |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function add($line) |
||
76 | |||
77 | /** |
||
78 | * Client rate as specified in the `Robot exclusion standard` version 2.0 draft |
||
79 | * rate = numDocuments / timeUnit |
||
80 | * @link http://www.conman.org/people/spc/robots2.html#format.directives.request-rate |
||
81 | * |
||
82 | * @param string $string |
||
83 | * @return float|int|false |
||
84 | */ |
||
85 | private function draftParseRate($string) |
||
106 | |||
107 | /** |
||
108 | * Client |
||
109 | * |
||
110 | * @param string $userAgent |
||
111 | * @param float|int $fallbackValue |
||
112 | * @return RequestRateClient |
||
113 | */ |
||
114 | public function client($userAgent = self::USER_AGENT, $fallbackValue = 0) |
||
119 | |||
120 | /** |
||
121 | * Sort |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | private function sort() |
||
136 | |||
137 | /** |
||
138 | * Render |
||
139 | * |
||
140 | * @param RenderHandler $handler |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function render(RenderHandler $handler) |
||
157 | } |
||
158 |
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.