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 |
||
12 | class Url implements ConfigureInterface |
||
13 | { |
||
14 | use ConfigureTrait; |
||
15 | |||
16 | |||
17 | |||
18 | /** |
||
19 | * @const URL_CLEAN controller/action/param1/param2 |
||
20 | * @const URL_APPEND index.php/controller/action/param1/param2 |
||
21 | * @var $urlType What type of urls to generate, select from |
||
22 | * URL_CLEAN or URL_APPEND. |
||
23 | */ |
||
24 | const URL_CLEAN = 'clean'; |
||
25 | const URL_APPEND = 'append'; |
||
26 | private $urlType = self::URL_APPEND; |
||
27 | |||
28 | |||
29 | |||
30 | /** |
||
31 | * @var $siteUrl Siteurl to prepend to all absolute urls created. |
||
32 | * @var $baseUrl Baseurl to prepend to all relative urls created. |
||
33 | * @var $scriptName Name of the frontcontroller script. |
||
34 | */ |
||
35 | private $siteUrl = null; |
||
36 | private $baseUrl = null; |
||
37 | private $scriptName = null; |
||
38 | |||
39 | |||
40 | |||
41 | /** |
||
42 | * @var $staticSiteUrl Siteurl to prepend to all absolute urls for |
||
43 | * assets. |
||
44 | * @var $staticBaseUrl Baseurl to prepend to all relative urls for |
||
45 | * assets. |
||
46 | */ |
||
47 | private $staticSiteUrl = null; |
||
48 | private $staticBaseUrl = null; |
||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Set default values from configuration. |
||
54 | * |
||
55 | * @return this. |
||
|
|||
56 | */ |
||
57 | public function setDefaultsFromConfiguration() |
||
78 | |||
79 | |||
80 | |||
81 | /** |
||
82 | * Create an url and prepending the baseUrl. |
||
83 | * |
||
84 | * @param string $uri part of uri to use when creating an url. |
||
85 | * "" or null means baseurl to current |
||
86 | * frontcontroller. |
||
87 | * @param string $baseuri optional base to prepend uri. |
||
88 | * |
||
89 | * @return string as resulting url. |
||
90 | */ |
||
91 | 27 | public function create($uri = null, $baseuri = null) |
|
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * Create an url and prepend the baseUrl to the directory of |
||
147 | * the frontcontroller. |
||
148 | * |
||
149 | * @param string $uri part of uri to use when creating an url. |
||
150 | * "" or null means baseurl to directory of |
||
151 | * the current frontcontroller. |
||
152 | * |
||
153 | * @return string as resulting url. |
||
154 | */ |
||
155 | 4 | public function createRelative($uri = null) |
|
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * Create an url for a static asset. |
||
179 | * |
||
180 | * @param string $uri part of uri to use when creating an url. |
||
181 | * |
||
182 | * @return string as resulting url. |
||
183 | */ |
||
184 | 12 | public function asset($uri = null) |
|
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * Set the siteUrl to prepend all absolute urls created. |
||
212 | * |
||
213 | * @param string $url part of url to use when creating an url. |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | 31 | public function setSiteUrl($url) |
|
222 | |||
223 | |||
224 | |||
225 | /** |
||
226 | * Set the baseUrl to prepend all relative urls created. |
||
227 | * |
||
228 | * @param string $url part of url to use when creating an url. |
||
229 | * |
||
230 | * @return self |
||
231 | */ |
||
232 | 35 | public function setBaseUrl($url) |
|
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * Set the siteUrl to prepend absolute urls for assets. |
||
242 | * |
||
243 | * @param string $url part of url to use when creating an url. |
||
244 | * |
||
245 | * @return self |
||
246 | */ |
||
247 | 8 | public function setStaticSiteUrl($url) |
|
252 | |||
253 | |||
254 | |||
255 | /** |
||
256 | * Set the baseUrl to prepend relative urls for assets. |
||
257 | * |
||
258 | * @param string $url part of url to use when creating an url. |
||
259 | * |
||
260 | * @return self |
||
261 | */ |
||
262 | 8 | public function setStaticBaseUrl($url) |
|
267 | |||
268 | |||
269 | |||
270 | /** |
||
271 | * Set the scriptname to use when creating URL_APPEND urls. |
||
272 | * |
||
273 | * @param string $name as the scriptname, for example index.php. |
||
274 | * |
||
275 | * @return self |
||
276 | */ |
||
277 | 9 | public function setScriptName($name) |
|
282 | |||
283 | |||
284 | |||
285 | /** |
||
286 | * Set the type of urls to be generated, URL_CLEAN, URL_APPEND. |
||
287 | * |
||
288 | * @param string $type what type of urls to create. |
||
289 | * |
||
290 | * @return self |
||
291 | * |
||
292 | * @throws Anax\Url\Exception |
||
293 | */ |
||
294 | 28 | public function setUrlType($type) |
|
303 | |||
304 | |||
305 | |||
306 | /** |
||
307 | * Create a slug of a string, to be used as url. |
||
308 | * |
||
309 | * @param string $str the string to format as slug. |
||
310 | * |
||
311 | * @return str the formatted slug. |
||
312 | */ |
||
313 | public function slugify($str) |
||
321 | } |
||
322 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.