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:
Complex classes like Import often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Import, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Import extends TxtClient |
||
17 | { |
||
18 | /** |
||
19 | * Root level export template |
||
20 | */ |
||
21 | const TEMPLATE_ROOT = [ |
||
22 | self::DIRECTIVE_CLEAN_PARAM => [], |
||
23 | self::DIRECTIVE_HOST => null, |
||
24 | self::DIRECTIVE_SITEMAP => [], |
||
25 | self::DIRECTIVE_USER_AGENT => [] |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * User-agent level export template |
||
30 | */ |
||
31 | const TEMPLATE_SUB = [ |
||
32 | self::DIRECTIVE_ALLOW => [], |
||
33 | self::DIRECTIVE_CACHE_DELAY => null, |
||
34 | self::DIRECTIVE_COMMENT => [], |
||
35 | self::DIRECTIVE_CRAWL_DELAY => null, |
||
36 | self::DIRECTIVE_DISALLOW => [], |
||
37 | self::DIRECTIVE_NO_INDEX => [], |
||
38 | self::DIRECTIVE_REQUEST_RATE => [], |
||
39 | self::DIRECTIVE_ROBOT_VERSION => null, |
||
40 | self::DIRECTIVE_VISIT_TIME => [], |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Array |
||
45 | * @var array |
||
46 | */ |
||
47 | private $array; |
||
48 | |||
49 | /** |
||
50 | * Import constructor. |
||
51 | * |
||
52 | * @param array $export |
||
53 | * @param string $baseUri |
||
54 | */ |
||
55 | public function __construct(array $export, $baseUri = 'https://example.com') |
||
65 | |||
66 | /** |
||
67 | * array_merge_recursive_ex |
||
68 | * @link http://stackoverflow.com/a/25712428/4396537 |
||
69 | * |
||
70 | * @param array $array1 |
||
71 | * @param array $array2 |
||
72 | * @return array |
||
73 | */ |
||
74 | private function arrayMergeRecursiveEx(array $array1, array &$array2) |
||
90 | |||
91 | /** |
||
92 | * Host |
||
93 | * |
||
94 | * @param string[]|string|null $array |
||
95 | * @return string[] |
||
96 | */ |
||
97 | private function buildHost($array) |
||
104 | |||
105 | /** |
||
106 | * Clean-param |
||
107 | * |
||
108 | * @param string[][] $array |
||
109 | * @return string[] |
||
110 | */ |
||
111 | private function buildCleanParam($array) |
||
121 | |||
122 | /** |
||
123 | * Comment | Sitemap |
||
124 | * |
||
125 | * @param string[] $array |
||
126 | * @param string $directive |
||
127 | * @return string[] |
||
128 | */ |
||
129 | private function buildArray($array, $directive) |
||
133 | |||
134 | /** |
||
135 | * User-agent |
||
136 | * |
||
137 | * @param array $array |
||
138 | * @return string[] |
||
139 | */ |
||
140 | private function buildUserAgent($array) |
||
161 | |||
162 | /** |
||
163 | * Cache-delay | Comment | Crawl-delay | Robot-version |
||
164 | * |
||
165 | * @param float|int|string|null $value |
||
166 | * @param string $directive |
||
167 | * @return string[] |
||
168 | */ |
||
169 | private function buildString($value, $directive) |
||
173 | |||
174 | /** |
||
175 | * Visit-time |
||
176 | * |
||
177 | * @param int[]|string[] $array |
||
178 | * @return string[] |
||
179 | */ |
||
180 | private function buildVisitTime($array) |
||
188 | |||
189 | /** |
||
190 | * Request-rate |
||
191 | * |
||
192 | * @param array $array |
||
193 | * @return string[] |
||
194 | */ |
||
195 | private function buildRequestRate($array) |
||
209 | |||
210 | /** |
||
211 | * Get difference |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | public function getIgnoredImportData() |
||
228 | |||
229 | /** |
||
230 | * array_filter_recursive |
||
231 | * @link http://php.net/manual/en/function.array-filter.php#87581 |
||
232 | * |
||
233 | * @param array $array |
||
234 | * @return array |
||
235 | */ |
||
236 | private function arrayFilterRecursive(array &$array) |
||
248 | |||
249 | /** |
||
250 | * ksort_recursive |
||
251 | * @link http://stackoverflow.com/a/2543447/4396537 |
||
252 | * |
||
253 | * @param array $array |
||
254 | * @return bool |
||
255 | */ |
||
256 | private function kSortRecursive(array &$array) |
||
267 | |||
268 | /** |
||
269 | * array_diff_assoc_recursive |
||
270 | * @link http://php.net/manual/en/function.array-diff-assoc.php#111675 |
||
271 | * |
||
272 | * @param array $array1 |
||
273 | * @param array $array2 |
||
274 | * @return array |
||
275 | */ |
||
276 | private function arrayDiffAssocRecursive(array &$array1, array &$array2) |
||
292 | } |
||
293 |
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.