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 Url 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 Url, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Url extends NativeGenerator |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Build link via controller/action and other params |
||
16 | * @param string $controller_action |
||
17 | * @param string|null $id |
||
18 | * @param string|null $add |
||
19 | * @param array $params |
||
20 | * @param bool $encode |
||
21 | * @return null|string |
||
22 | */ |
||
23 | public static function to($controller_action, $id = null, $add = null, array $params = null, $encode = true) |
||
28 | |||
29 | /** |
||
30 | * Build pathway from array $to. Example: ['controller/action', 'id', 'add', ['get' => 'value'], '#anchor'] |
||
31 | * @param array $to |
||
32 | * @param bool $encode |
||
33 | * @return string|null |
||
34 | */ |
||
35 | public static function buildPathway(array $to = null, $encode = true) |
||
113 | |||
114 | /** |
||
115 | * Build current pathway with get data to compare in some methods |
||
116 | * @return null|string |
||
117 | */ |
||
118 | public static function buildPathwayFromRequest() |
||
127 | |||
128 | /** |
||
129 | * Create <a></a> block link |
||
130 | * @param string|array $to |
||
131 | * @param string $name |
||
132 | * @param array|null $property |
||
133 | * @return string |
||
134 | */ |
||
135 | public static function link($to, $name, array $property = null) |
||
147 | |||
148 | /** |
||
149 | * Build full URL link from URI string, if REQUEST data is not available (console, cron, etc) |
||
150 | * @param string $uri |
||
151 | * @param string|null $lang |
||
152 | * @return string |
||
153 | */ |
||
154 | public static function standaloneUrl($uri, $lang = null) |
||
175 | |||
176 | /** |
||
177 | * Build a.href html tag with full URL link from uri & text |
||
178 | * @param string $text |
||
179 | * @param string $uri |
||
180 | * @param string|null $lang |
||
181 | * @param array $property |
||
182 | * @return string |
||
183 | */ |
||
184 | public static function standaloneLink($text, $uri, $lang = null, array $property = []) |
||
189 | |||
190 | /** |
||
191 | * Download remote content in binary string |
||
192 | * @param string $url |
||
193 | * @return null|string |
||
194 | */ |
||
195 | public static function download($url) |
||
222 | |||
223 | } |
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.