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:
1 | <?php |
||
9 | class CUrl |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Properties |
||
14 | * |
||
15 | */ |
||
16 | const URL_CLEAN = 'clean'; // controller/action/param1/param2 |
||
17 | const URL_APPEND = 'append'; // index.php/controller/action/param1/param2 |
||
18 | |||
19 | private $urlType = self::URL_APPEND; // What type of urls to generate |
||
20 | |||
21 | private $siteUrl = null; // Siteurl to prepend to all absolute urls created |
||
22 | private $baseUrl = null; // Baseurl to prepend to all relative urls created |
||
23 | private $scriptName = null; // Name of the frontcontroller script |
||
24 | |||
25 | |||
26 | private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets |
||
27 | private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * Create an url and prepending the baseUrl. |
||
33 | * |
||
34 | * @param string $uri part of uri to use when creating an url. "" or null means baseurl to |
||
35 | * current frontcontroller. |
||
36 | * |
||
37 | * @return string as resulting url. |
||
38 | */ |
||
39 | public function create($uri = null) |
||
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * Create an url and prepend the baseUrl to the directory of the frontcontroller. |
||
73 | * |
||
74 | * @param string $uri part of uri to use when creating an url. "" or null means baseurl to |
||
75 | * directory of the current frontcontroller. |
||
76 | * |
||
77 | * @return string as resulting url. |
||
78 | */ |
||
79 | public function createRelative($uri = null) |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * Create an url for a static asset. |
||
106 | * |
||
107 | * @param string $uri part of uri to use when creating an url. |
||
108 | * |
||
109 | * @return string as resulting url. |
||
110 | */ |
||
111 | public function asset($uri = null) |
||
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * Set the siteUrl to prepend all absolute urls created. |
||
140 | * |
||
141 | * @param string $url part of url to use when creating an url. |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function setSiteUrl($url) |
||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * Set the baseUrl to prepend all relative urls created. |
||
155 | * |
||
156 | * @param string $url part of url to use when creating an url. |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setBaseUrl($url) |
||
165 | |||
166 | |||
167 | |||
168 | /** |
||
169 | * Set the siteUrl to prepend absolute urls for assets. |
||
170 | * |
||
171 | * @param string $url part of url to use when creating an url. |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setStaticSiteUrl($url) |
||
180 | |||
181 | |||
182 | |||
183 | /** |
||
184 | * Set the baseUrl to prepend relative urls for assets. |
||
185 | * |
||
186 | * @param string $url part of url to use when creating an url. |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function setStaticBaseUrl($url) |
||
195 | |||
196 | |||
197 | |||
198 | /** |
||
199 | * Set the scriptname to use when creating URL_APPEND urls. |
||
200 | * |
||
201 | * @param string $name as the scriptname, for example index.php. |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function setScriptName($name) |
||
210 | |||
211 | |||
212 | |||
213 | /** |
||
214 | * Set the type of urls to be generated, URL_CLEAN, URL_APPEND. |
||
215 | * |
||
216 | * @param string $type what type of urls to create. |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function setUrlType($type) |
||
229 | } |
||
230 |
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.