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 CleanParamFilter 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 CleanParamFilter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class CleanParamFilter |
||
21 | { |
||
22 | // Clean-Param set |
||
23 | private $cleanParam = []; |
||
24 | |||
25 | // URL set |
||
26 | private $urls = []; |
||
27 | |||
28 | // Status |
||
29 | private $filtered = false; |
||
30 | |||
31 | // Approved and duplicate URLs |
||
32 | private $approved = []; |
||
33 | private $duplicate = []; |
||
34 | |||
35 | // Invalid URLs |
||
36 | private $invalid = []; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * |
||
41 | * @param array $urls |
||
42 | */ |
||
43 | public function __construct($urls) |
||
57 | |||
58 | /** |
||
59 | * Lists all approved URLs |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function listApproved() |
||
68 | |||
69 | /** |
||
70 | * Filter URLs |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | private function filter() |
||
75 | { |
||
76 | // skip the filtering process if it's already done |
||
77 | if ($this->filtered) { |
||
78 | return; |
||
79 | } |
||
80 | $urlsByHost = []; |
||
81 | $parsed = []; |
||
82 | // Loop |
||
83 | foreach ($this->urls as $host => $urlArray) { |
||
84 | // prepare each individual URL |
||
85 | foreach ($urlArray as $url) { |
||
86 | $path = parse_url($url, PHP_URL_PATH); |
||
87 | View Code Duplication | if ($path !== false && mb_substr($path, -1) == '/') { |
|
|
|||
88 | $path = substr_replace($path, '', -1); |
||
89 | } |
||
90 | $urlsByHost[$host][$path][$url] = $this->prepareURL($url); |
||
91 | } |
||
92 | // Filter |
||
93 | foreach ($urlsByHost[$host] as $array) { |
||
94 | $parsed[] = $this->filterDuplicates($array, $host); |
||
95 | } |
||
96 | } |
||
97 | // generate lists of URLs for 3rd party usage |
||
98 | $allURLs = call_user_func_array('array_merge', $this->urls); |
||
99 | $this->approved = call_user_func_array('array_merge', $parsed); |
||
100 | $this->duplicate = array_diff($allURLs, $this->approved); |
||
101 | // Sort the result arrays |
||
102 | sort($this->approved); |
||
103 | sort($this->duplicate); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Prepare URL |
||
108 | * |
||
109 | * @param string $url |
||
110 | * @return string |
||
111 | */ |
||
112 | private function prepareURL($url) |
||
131 | |||
132 | /** |
||
133 | * Build URL from array |
||
134 | * |
||
135 | * @param array $parsedURL |
||
136 | * @return string |
||
137 | */ |
||
138 | private function unParseURL($parsedURL) |
||
147 | |||
148 | /** |
||
149 | * Filter duplicate URLs |
||
150 | * |
||
151 | * @param array $array - URLs to filter |
||
152 | * @param string $host - Hostname |
||
153 | * @return array |
||
154 | */ |
||
155 | private function filterDuplicates($array, $host) |
||
181 | |||
182 | /** |
||
183 | * Find CleanParam parameters in provided URL |
||
184 | * |
||
185 | * @param string $url |
||
186 | * @param string $host |
||
187 | * @return array |
||
188 | */ |
||
189 | private function findCleanParam($url, $host) |
||
213 | |||
214 | /** |
||
215 | * Check if path matches |
||
216 | * |
||
217 | * @param string $path - Path compare |
||
218 | * @param string $prefix - Path prefix |
||
219 | * @return bool |
||
220 | */ |
||
221 | private function checkPath($path, $prefix) |
||
233 | |||
234 | /** |
||
235 | * Strip provided parameters from URL |
||
236 | * |
||
237 | * @param string $url - URL to check |
||
238 | * @param array $paramArray - parameters to remove |
||
239 | * @return string |
||
240 | */ |
||
241 | private function stripParam($url, $paramArray) |
||
262 | |||
263 | /** |
||
264 | * Fix damaged URL query string |
||
265 | * |
||
266 | * @param string $url |
||
267 | * @return string |
||
268 | */ |
||
269 | private static function fixURL($url) |
||
284 | |||
285 | /** |
||
286 | * Lists all duplicate URLs |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | public function listDuplicate() |
||
295 | |||
296 | /** |
||
297 | * Lists all invalid URLs |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function listInvalid() |
||
305 | |||
306 | /** |
||
307 | * Add CleanParam |
||
308 | * |
||
309 | * @param string $param - parameter(s) |
||
310 | * @param string $path - path the param is valid for |
||
311 | * @param string $host - limit to a single hostname |
||
312 | * @return void |
||
313 | */ |
||
314 | public function addCleanParam($param, $path = '/', $host = null) |
||
331 | } |
||
332 |
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.