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 Link 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 Link, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Link |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * class constants. |
||
| 44 | */ |
||
| 45 | const LINK_AMPERSAND = '&'; |
||
| 46 | const LINK_CATEGORY = 'category/'; |
||
| 47 | const LINK_CONTENT = 'content/'; |
||
| 48 | const LINK_EQUAL = '='; |
||
| 49 | const LINK_FRAGMENT_SEPARATOR = '#'; |
||
| 50 | const LINK_HTML_MINUS = '-'; |
||
| 51 | const LINK_HTML_UNDERSCORE = '_'; |
||
| 52 | const LINK_HTML_SLASH = '/'; |
||
| 53 | const LINK_HTML_TARGET_BLANK = '_blank'; |
||
| 54 | const LINK_HTML_TARGET_PARENT = '_parent'; |
||
| 55 | const LINK_HTML_TARGET_SELF = '_self'; |
||
| 56 | const LINK_HTML_TARGET_TOP = '_top'; |
||
| 57 | const LINK_NEWS = 'news/'; |
||
| 58 | const LINK_SITEMAP = 'sitemap/'; |
||
| 59 | const LINK_SLASH = '/'; |
||
| 60 | const LINK_SEARCHPART_SEPARATOR = '?'; |
||
| 61 | const LINK_TAGS = 'tags/'; |
||
| 62 | |||
| 63 | const LINK_INDEX_ADMIN = '/admin/index.php'; |
||
| 64 | const LINK_INDEX_HOME = '/index.php'; |
||
| 65 | |||
| 66 | const LINK_GET_ACTION = 'action'; |
||
| 67 | const LINK_GET_ARTLANG = 'artlang'; |
||
| 68 | const LINK_GET_CATEGORY = 'cat'; |
||
| 69 | const LINK_GET_HIGHLIGHT = 'highlight'; |
||
| 70 | const LINK_GET_ID = 'id'; |
||
| 71 | const LINK_GET_LANG = 'lang'; |
||
| 72 | const LINK_GET_LETTER = 'letter'; |
||
| 73 | const LINK_GET_NEWS_ID = 'newsid'; |
||
| 74 | const LINK_GET_NEWS_LANG = 'newslang'; |
||
| 75 | const LINK_GET_PAGE = 'seite'; |
||
| 76 | const LINK_GET_SIDS = 'sid'; |
||
| 77 | const LINK_GET_TAGGING_ID = 'tagging_id'; |
||
| 78 | const LINK_GET_LANGS = 'langs'; |
||
| 79 | |||
| 80 | const LINK_GET_ACTION_ADD = 'add'; |
||
| 81 | const LINK_GET_ACTION_FAQ = 'faq'; |
||
| 82 | const LINK_GET_ACTION_ASK = 'ask'; |
||
| 83 | const LINK_GET_ACTION_CONTACT = 'contact'; |
||
| 84 | const LINK_GET_ACTION_GLOSSARY = 'glossary'; |
||
| 85 | const LINK_GET_ACTION_HELP = 'help'; |
||
| 86 | const LINK_GET_ACTION_LOGIN = 'login'; |
||
| 87 | const LINK_GET_ACTION_NEWS = 'news'; |
||
| 88 | const LINK_GET_ACTION_OPEN = 'open'; |
||
| 89 | const LINK_GET_ACTION_PASSWORD = 'password'; |
||
| 90 | const LINK_GET_ACTION_REGISTER = 'register'; |
||
| 91 | const LINK_GET_ACTION_SEARCH = 'search'; |
||
| 92 | const LINK_GET_ACTION_SITEMAP = 'sitemap'; |
||
| 93 | const LINK_GET_ACTION_SHOW = 'show'; |
||
| 94 | |||
| 95 | const LINK_HTML_CATEGORY = 'category'; |
||
| 96 | const LINK_HTML_EXTENSION = '.html'; |
||
| 97 | const LINK_HTML_SITEMAP = 'sitemap'; |
||
| 98 | |||
| 99 | const LINK_HTML_ADDCONTENT = 'addcontent.html'; |
||
| 100 | const LINK_HTML_ASK = 'ask.html'; |
||
| 101 | const LINK_HTML_CONTACT = 'contact.html'; |
||
| 102 | const LINK_HTML_GLOSSARY = 'glossary.html'; |
||
| 103 | const LINK_HTML_HELP = 'help.html'; |
||
| 104 | const LINK_HTML_LOGIN = 'login.html'; |
||
| 105 | const LINK_HTML_OPEN = 'open.html'; |
||
| 106 | const LINK_HTML_PASSWORD = 'password.html'; |
||
| 107 | const LINK_HTML_REGISTER = 'register.html'; |
||
| 108 | const LINK_HTML_SEARCH = 'search.html'; |
||
| 109 | const LINK_HTML_SHOWCAT = 'showcat.html'; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * URL. |
||
| 113 | * |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | public $url = ''; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * CSS class. |
||
| 120 | * |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | public $class = ''; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Linktext. |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | public $text = ''; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Tooltip. |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | public $tooltip = ''; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Target. |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | public $target = ''; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Name selector. |
||
| 148 | * |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | public $name = ''; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * property specific to the SEO/SEF URLs. |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | public $itemTitle = ''; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Item property for HTML5 microdata. |
||
| 162 | * |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | protected $itemprop = ''; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * rel property. |
||
| 169 | * |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | protected $rel = ''; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * id selector. |
||
| 176 | * |
||
| 177 | * @var string |
||
| 178 | */ |
||
| 179 | public $id = ''; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var Configuration |
||
| 183 | */ |
||
| 184 | private $config = null; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Constructor. |
||
| 188 | * |
||
| 189 | * @param string $url URL |
||
| 190 | * @param Configuration $config |
||
| 191 | */ |
||
| 192 | public function __construct($url, Configuration $config) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Checks if webserver is an IIS Server. |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public static function isIISServer() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Checks if the the current URL is the main index.php file. |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | protected function isHomeIndex() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Checks if URL is an internal reference. |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | protected function isInternalReference() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Checks if URL is a relative system link. |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | protected function isRelativeSystemLink() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Checks if URL is a system link. |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | protected function isSystemLink() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $itemprop Item property |
||
| 272 | */ |
||
| 273 | public function setItemProperty($itemprop) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $rel rel property |
||
| 280 | */ |
||
| 281 | public function setRelation($rel) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Checks if URL contains a scheme. |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | protected function hasScheme() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns a search engine optimized title. |
||
| 300 | * |
||
| 301 | * @param string $title |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getSEOItemTitle($title = '') |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the HTTP GET parameters. |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | protected function getHttpGetParameters(): array |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns the query of an URL. |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | protected function getQuery(): array |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns the default scheme. |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | protected function getDefaultScheme(): string |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Returns the system scheme, http or https. |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | public function getSystemScheme(): string |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Returns the relative URI. |
||
| 441 | * @param string $path |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public static function getSystemRelativeUri(string $path = null): string |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns the system URI. |
||
| 455 | * |
||
| 456 | * $_SERVER['HTTP_HOST'] is the name of the website or virtual host name (HTTP/1.1) |
||
| 457 | * Precisely, it contains what the user has written in the Host request-header, see below. |
||
| 458 | * RFC 2616: The Host request-header field specifies the Internet host and port number of the resource |
||
| 459 | * being requested, as obtained from the original URI given by the user or referring resource |
||
| 460 | * |
||
| 461 | * @param string $path |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public function getSystemUri($path = null): string |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Builds a HTML anchor. |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | public function toHtmlAnchor():string |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Appends the session id. |
||
| 526 | * @param string $url URL |
||
| 527 | * @param int $sids Session Id |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | protected function appendSids(string $url, int $sids): string |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Rewrites a URL string. Checks mod_rewrite support and 'rewrite' |
||
| 543 | * the passed (system) uri according to the rewrite rules written |
||
| 544 | * in .htaccess |
||
| 545 | * @param bool $removeSessionFromUrl Remove session from URL |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | public function toString(bool $removeSessionFromUrl = false): string |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Transforms a URI. |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | public function toUri(): string |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Returns the current URL. |
||
| 727 | * @return string |
||
| 728 | */ |
||
| 729 | public function getCurrentUrl(): string |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Static method to generate simple HTML anchors. |
||
| 736 | * @static |
||
| 737 | * @param string $url URL |
||
| 738 | * @param string $text Text |
||
| 739 | * @param bool $active Add CSS class named "active"? |
||
| 740 | * @return string |
||
| 741 | */ |
||
| 742 | public static function renderNavigationLink(string $url, string $text, bool $active = false): string |
||
| 751 | } |
||
| 752 |