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 |
||
9 | class Url implements \Anax\Common\ConfigureInterface |
||
10 | { |
||
11 | use \Anax\Common\ConfigureTrait; |
||
12 | |||
13 | |||
14 | |||
15 | /** |
||
16 | * @const URL_CLEAN controller/action/param1/param2 |
||
17 | * @const URL_APPEND index.php/controller/action/param1/param2 |
||
18 | * @var $urlType What type of urls to generate, select from |
||
19 | * URL_CLEAN or URL_APPEND. |
||
20 | */ |
||
21 | const URL_CLEAN = 'clean'; |
||
22 | const URL_APPEND = 'append'; |
||
23 | private $urlType = self::URL_APPEND; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * @var $siteUrl Siteurl to prepend to all absolute urls created. |
||
29 | * @var $baseUrl Baseurl to prepend to all relative urls created. |
||
30 | * @var $scriptName Name of the frontcontroller script. |
||
31 | */ |
||
32 | private $siteUrl = null; |
||
33 | private $baseUrl = null; |
||
34 | private $scriptName = null; |
||
35 | |||
36 | |||
37 | |||
38 | /** |
||
39 | * @var $staticSiteUrl Siteurl to prepend to all absolute urls for |
||
40 | * assets. |
||
41 | * @var $staticBaseUrl Baseurl to prepend to all relative urls for |
||
42 | * assets. |
||
43 | */ |
||
44 | private $staticSiteUrl = null; |
||
45 | private $staticBaseUrl = null; |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * Set default values from configuration. |
||
51 | * |
||
52 | * @return this. |
||
|
|||
53 | */ |
||
54 | public function setDefaultsFromConfiguration() |
||
75 | |||
76 | |||
77 | |||
78 | /** |
||
79 | * Create an url and prepending the baseUrl. |
||
80 | * |
||
81 | * @param string $uri part of uri to use when creating an url. |
||
82 | * "" or null means baseurl to current |
||
83 | * frontcontroller. |
||
84 | * @param string $baseuri optional base to prepend uri. |
||
85 | * |
||
86 | * @return string as resulting url. |
||
87 | */ |
||
88 | 27 | public function create($uri = null, $baseuri = null) |
|
138 | |||
139 | |||
140 | |||
141 | /** |
||
142 | * Create an url and prepend the baseUrl to the directory of |
||
143 | * the frontcontroller. |
||
144 | * |
||
145 | * @param string $uri part of uri to use when creating an url. |
||
146 | * "" or null means baseurl to directory of |
||
147 | * the current frontcontroller. |
||
148 | * |
||
149 | * @return string as resulting url. |
||
150 | */ |
||
151 | 4 | public function createRelative($uri = null) |
|
170 | |||
171 | |||
172 | |||
173 | /** |
||
174 | * Create an url for a static asset. |
||
175 | * |
||
176 | * @param string $uri part of uri to use when creating an url. |
||
177 | * |
||
178 | * @return string as resulting url. |
||
179 | */ |
||
180 | 12 | public function asset($uri = null) |
|
203 | |||
204 | |||
205 | |||
206 | /** |
||
207 | * Set the siteUrl to prepend all absolute urls created. |
||
208 | * |
||
209 | * @param string $url part of url to use when creating an url. |
||
210 | * |
||
211 | * @return self |
||
212 | */ |
||
213 | 31 | public function setSiteUrl($url) |
|
218 | |||
219 | |||
220 | |||
221 | /** |
||
222 | * Set the baseUrl to prepend all relative urls created. |
||
223 | * |
||
224 | * @param string $url part of url to use when creating an url. |
||
225 | * |
||
226 | * @return self |
||
227 | */ |
||
228 | 35 | public function setBaseUrl($url) |
|
233 | |||
234 | |||
235 | |||
236 | /** |
||
237 | * Set the siteUrl to prepend absolute urls for assets. |
||
238 | * |
||
239 | * @param string $url part of url to use when creating an url. |
||
240 | * |
||
241 | * @return self |
||
242 | */ |
||
243 | 8 | public function setStaticSiteUrl($url) |
|
248 | |||
249 | |||
250 | |||
251 | /** |
||
252 | * Set the baseUrl to prepend relative urls for assets. |
||
253 | * |
||
254 | * @param string $url part of url to use when creating an url. |
||
255 | * |
||
256 | * @return self |
||
257 | */ |
||
258 | 8 | public function setStaticBaseUrl($url) |
|
263 | |||
264 | |||
265 | |||
266 | /** |
||
267 | * Set the scriptname to use when creating URL_APPEND urls. |
||
268 | * |
||
269 | * @param string $name as the scriptname, for example index.php. |
||
270 | * |
||
271 | * @return self |
||
272 | */ |
||
273 | 9 | public function setScriptName($name) |
|
278 | |||
279 | |||
280 | |||
281 | /** |
||
282 | * Set the type of urls to be generated, URL_CLEAN, URL_APPEND. |
||
283 | * |
||
284 | * @param string $type what type of urls to create. |
||
285 | * |
||
286 | * @return self |
||
287 | * |
||
288 | * @throws Anax\Url\Exception |
||
289 | */ |
||
290 | 28 | public function setUrlType($type) |
|
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * Create a slug of a string, to be used as url. |
||
304 | * |
||
305 | * @param string $str the string to format as slug. |
||
306 | * |
||
307 | * @return str the formatted slug. |
||
308 | */ |
||
309 | public function slugify($str) |
||
317 | } |
||
318 |
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.