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 |
||
18 | class Import extends TxtClient |
||
19 | { |
||
20 | use PhpAddOnTrait; |
||
21 | |||
22 | /** |
||
23 | * Root level export template |
||
24 | */ |
||
25 | const TEMPLATE_ROOT = [ |
||
26 | self::DIRECTIVE_HOST => null, |
||
27 | self::DIRECTIVE_CLEAN_PARAM => [], |
||
28 | self::DIRECTIVE_SITEMAP => [], |
||
29 | self::DIRECTIVE_USER_AGENT => [] |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * User-agent level export template |
||
34 | */ |
||
35 | const TEMPLATE_SUB = [ |
||
36 | self::DIRECTIVE_ROBOT_VERSION => null, |
||
37 | self::DIRECTIVE_VISIT_TIME => [], |
||
38 | self::DIRECTIVE_NO_INDEX => [ |
||
39 | self::DIRECTIVE_HOST => [], |
||
40 | 'path' => [], |
||
41 | self::DIRECTIVE_CLEAN_PARAM => [], |
||
42 | ], |
||
43 | self::DIRECTIVE_DISALLOW => |
||
44 | [ |
||
45 | self::DIRECTIVE_HOST => [], |
||
46 | 'path' => [], |
||
47 | self::DIRECTIVE_CLEAN_PARAM => [], |
||
48 | ], |
||
49 | self::DIRECTIVE_ALLOW => |
||
50 | [ |
||
51 | self::DIRECTIVE_HOST => [], |
||
52 | 'path' => [], |
||
53 | self::DIRECTIVE_CLEAN_PARAM => [], |
||
54 | ], |
||
55 | self::DIRECTIVE_CRAWL_DELAY => null, |
||
56 | self::DIRECTIVE_CACHE_DELAY => null, |
||
57 | self::DIRECTIVE_REQUEST_RATE => [], |
||
58 | self::DIRECTIVE_COMMENT => [], |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * Array |
||
63 | * @var array |
||
64 | */ |
||
65 | private $array; |
||
66 | |||
67 | /** |
||
68 | * Import constructor. |
||
69 | * |
||
70 | * @param array $export |
||
71 | * @param string $baseUri |
||
72 | */ |
||
73 | public function __construct(array $export, $baseUri = 'https://example.com') |
||
86 | |||
87 | /** |
||
88 | * Host |
||
89 | * |
||
90 | * @param string[]|string|null $array |
||
91 | * @return string[] |
||
92 | */ |
||
93 | private function buildHost($array) |
||
100 | |||
101 | /** |
||
102 | * Clean-param |
||
103 | * |
||
104 | * @param string[][] $array |
||
105 | * @return string[] |
||
106 | */ |
||
107 | private function buildCleanParam($array) |
||
117 | |||
118 | /** |
||
119 | * Comment | Sitemap |
||
120 | * |
||
121 | * @param string[] $array |
||
122 | * @param string $directive |
||
123 | * @return string[] |
||
124 | */ |
||
125 | private function buildGenericArray($array, $directive) |
||
129 | |||
130 | /** |
||
131 | * User-agent |
||
132 | * |
||
133 | * @param array $array |
||
134 | * @return string[] |
||
135 | */ |
||
136 | private function buildUserAgent($array) |
||
156 | |||
157 | /** |
||
158 | * Cache-delay | Comment | Crawl-delay | Robot-version |
||
159 | * |
||
160 | * @param float|int|string|null $value |
||
161 | * @param string $directive |
||
162 | * @return string[] |
||
163 | */ |
||
164 | private function buildGenericString($value, $directive) |
||
168 | |||
169 | /** |
||
170 | * Visit-time |
||
171 | * |
||
172 | * @param int[]|string[] $array |
||
173 | * @return string[] |
||
174 | */ |
||
175 | private function buildVisitTime($array) |
||
183 | |||
184 | /** |
||
185 | * Allow / Disallow / NoIndex |
||
186 | * |
||
187 | * @param array $array |
||
188 | * @param string $directive |
||
189 | * @return string[] |
||
190 | */ |
||
191 | private function buildAllow($array, $directive) |
||
200 | |||
201 | /** |
||
202 | * Request-rate |
||
203 | * |
||
204 | * @param array $array |
||
205 | * @return string[] |
||
206 | */ |
||
207 | private function buildRequestRate($array) |
||
221 | |||
222 | /** |
||
223 | * Get difference |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function getIgnoredImportData() |
||
236 | } |
||
237 |
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.