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 | 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) |
||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | 2 | /** |
|
| 121 | * Create an url and prepend the baseUrl to the directory of the frontcontroller. |
||
| 122 | 2 | * |
|
| 123 | 2 | * @param string $uri part of uri to use when creating an url. "" or null means baseurl to |
|
| 124 | 2 | * directory of the current frontcontroller. |
|
| 125 | * |
||
| 126 | * @return string as resulting url. |
||
| 127 | */ |
||
| 128 | public function createRelative($uri = null) |
||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Create an url for a static asset. |
||
| 149 | * |
||
| 150 | * @param string $uri part of uri to use when creating an url. |
||
| 151 | 18 | * |
|
| 152 | * @return string as resulting url. |
||
| 153 | 18 | */ |
|
| 154 | 18 | public function asset($uri = null) |
|
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Set the siteUrl to prepend all absolute urls created. |
||
| 177 | * |
||
| 178 | * @param string $url part of url to use when creating an url. |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | 4 | */ |
|
| 182 | public function setSiteUrl($url) |
||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Set the baseUrl to prepend all relative urls created. |
||
| 192 | * |
||
| 193 | * @param string $url part of url to use when creating an url. |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | 7 | */ |
|
| 197 | public function setBaseUrl($url) |
||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Set the siteUrl to prepend absolute urls for assets. |
||
| 207 | * |
||
| 208 | * @param string $url part of url to use when creating an url. |
||
| 209 | * |
||
| 210 | * @return $this |
||
| 211 | 15 | */ |
|
| 212 | public function setStaticSiteUrl($url) |
||
| 217 | 14 | ||
| 218 | 14 | ||
| 219 | |||
| 220 | /** |
||
| 221 | * Set the baseUrl to prepend relative urls for assets. |
||
| 222 | * |
||
| 223 | * @param string $url part of url to use when creating an url. |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function setStaticBaseUrl($url) |
||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Set the scriptname to use when creating URL_APPEND urls. |
||
| 237 | * |
||
| 238 | * @param string $name as the scriptname, for example index.php. |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function setScriptName($name) |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Set the type of urls to be generated, URL_CLEAN, URL_APPEND. |
||
| 252 | * |
||
| 253 | * @param string $type what type of urls to create. |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function setUrlType($type) |
||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Create a slug of a string, to be used as url. |
||
| 271 | * |
||
| 272 | * @param string $str the string to format as slug. |
||
| 273 | * |
||
| 274 | * @return str the formatted slug. |
||
| 275 | */ |
||
| 276 | public function slugify($str) |
||
| 284 | } |
||
| 285 |
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.