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 CUrl 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 CUrl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class CUrl |
||
10 | { |
||
11 | use \Anax\TConfigure; |
||
12 | |||
13 | |||
14 | |||
15 | /** |
||
16 | * Properties |
||
17 | * |
||
18 | */ |
||
19 | const URL_CLEAN = 'clean'; // controller/action/param1/param2 |
||
20 | const URL_APPEND = 'append'; // index.php/controller/action/param1/param2 |
||
21 | |||
22 | private $urlType = self::URL_APPEND; // What type of urls to generate |
||
23 | |||
24 | private $siteUrl = null; // Siteurl to prepend to all absolute urls created |
||
25 | private $baseUrl = null; // Baseurl to prepend to all relative urls created |
||
26 | private $scriptName = null; // Name of the frontcontroller script |
||
27 | |||
28 | |||
29 | private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets |
||
30 | private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets |
||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * Set default values from configuration. |
||
36 | * |
||
37 | * @return this. |
||
|
|||
38 | */ |
||
39 | 17 | public function setDefaultsFromConfiguration() |
|
60 | 4 | ||
61 | |||
62 | 4 | ||
63 | /** |
||
64 | * Create an url and prepending the baseUrl. |
||
65 | * |
||
66 | * @param string $uri part of uri to use when creating an url. |
||
67 | * "" or null means baseurl to current |
||
68 | * frontcontroller. |
||
69 | * @param string $baseuri optional base to prepend uri. |
||
70 | * |
||
71 | * @return string as resulting url. |
||
72 | */ |
||
73 | public function create($uri = null, $baseuri = null) |
||
124 | 2 | ||
125 | |||
126 | |||
127 | /** |
||
128 | * Create an url and prepend the baseUrl to the directory of |
||
129 | * the frontcontroller. |
||
130 | * |
||
131 | * @param string $uri part of uri to use when creating an url. |
||
132 | * "" or null means baseurl to directory of |
||
133 | * the current frontcontroller. |
||
134 | * |
||
135 | * @return string as resulting url. |
||
136 | 17 | */ |
|
137 | public function createRelative($uri = null) |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * Create an url for a static asset. |
||
161 | * |
||
162 | * @param string $uri part of uri to use when creating an url. |
||
163 | * |
||
164 | * @return string as resulting url. |
||
165 | */ |
||
166 | 4 | public function asset($uri = null) |
|
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * Set the siteUrl to prepend all absolute urls created. |
||
194 | * |
||
195 | * @param string $url part of url to use when creating an url. |
||
196 | 7 | * |
|
197 | * @return $this |
||
198 | 7 | */ |
|
199 | 7 | public function setSiteUrl($url) |
|
204 | |||
205 | |||
206 | |||
207 | /** |
||
208 | * Set the baseUrl to prepend all relative urls created. |
||
209 | * |
||
210 | * @param string $url part of url to use when creating an url. |
||
211 | 15 | * |
|
212 | * @return $this |
||
213 | 15 | */ |
|
214 | 1 | public function setBaseUrl($url) |
|
219 | |||
220 | |||
221 | |||
222 | /** |
||
223 | * Set the siteUrl to prepend absolute urls for assets. |
||
224 | * |
||
225 | * @param string $url part of url to use when creating an url. |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setStaticSiteUrl($url) |
||
234 | |||
235 | |||
236 | |||
237 | /** |
||
238 | * Set the baseUrl to prepend relative urls for assets. |
||
239 | * |
||
240 | * @param string $url part of url to use when creating an url. |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function setStaticBaseUrl($url) |
||
249 | |||
250 | |||
251 | |||
252 | /** |
||
253 | * Set the scriptname to use when creating URL_APPEND urls. |
||
254 | * |
||
255 | * @param string $name as the scriptname, for example index.php. |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function setScriptName($name) |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * Set the type of urls to be generated, URL_CLEAN, URL_APPEND. |
||
269 | * |
||
270 | * @param string $type what type of urls to create. |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function setUrlType($type) |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * Create a slug of a string, to be used as url. |
||
288 | * |
||
289 | * @param string $str the string to format as slug. |
||
290 | * |
||
291 | * @return str the formatted slug. |
||
292 | */ |
||
293 | public function slugify($str) |
||
301 | } |
||
302 |
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.