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 |
||
13 | View Code Duplication | class Download implements RobotsTxtInterface |
|
|
|||
14 | { |
||
15 | /** |
||
16 | * Base uri |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $baseUri; |
||
20 | |||
21 | /** |
||
22 | * HTTP Status code |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $statusCode; |
||
26 | |||
27 | /** |
||
28 | * Robots.txt contents |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $contents; |
||
32 | |||
33 | /** |
||
34 | * Robots.txt character encoding |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $encoding; |
||
38 | |||
39 | /** |
||
40 | * Download constructor. |
||
41 | * |
||
42 | * @param string $baseUri |
||
43 | * @param array $guzzleConfig |
||
44 | */ |
||
45 | public function __construct($baseUri, $guzzleConfig = []) |
||
80 | |||
81 | /** |
||
82 | * HTTP header encoding |
||
83 | * |
||
84 | * @param $header |
||
85 | * @return string |
||
86 | */ |
||
87 | protected function headerEncoding($header) |
||
100 | |||
101 | /** |
||
102 | * Manually detect encoding |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | protected function detectEncoding() |
||
113 | |||
114 | /** |
||
115 | * URL content |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getContents() |
||
123 | |||
124 | /** |
||
125 | * Connection issue |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | private function connectionIssue() |
||
135 | |||
136 | /** |
||
137 | * Parser client |
||
138 | * |
||
139 | * @param int|null $byteLimit |
||
140 | * @return Client |
||
141 | */ |
||
142 | public function parserClient($byteLimit = self::BYTE_LIMIT) |
||
146 | |||
147 | /** |
||
148 | * Status code |
||
149 | * |
||
150 | * @return int |
||
151 | */ |
||
152 | public function getStatusCode() |
||
156 | |||
157 | /** |
||
158 | * Encoding |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getEncoding() |
||
166 | } |
||
167 |
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.