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 PMF_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 PMF_Link, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 41 | class PMF_Link | ||
| 42 | { | ||
| 43 | /** | ||
| 44 | * class constants | ||
| 45 | * | ||
| 46 | */ | ||
| 47 | const PMF_LINK_AMPERSAND = '&'; | ||
| 48 | const PMF_LINK_CATEGORY = 'category/'; | ||
| 49 | const PMF_LINK_CONTENT = 'content/'; | ||
| 50 | const PMF_LINK_EQUAL = '='; | ||
| 51 | const PMF_LINK_FRAGMENT_SEPARATOR = '#'; | ||
| 52 | const PMF_LINK_HTML_MINUS = '-'; | ||
| 53 | const PMF_LINK_HTML_UNDERSCORE = '_'; | ||
| 54 | const PMF_LINK_HTML_SLASH = '/'; | ||
| 55 | const PMF_LINK_HTML_TARGET_BLANK = '_blank'; | ||
| 56 | const PMF_LINK_HTML_TARGET_PARENT = '_parent'; | ||
| 57 | const PMF_LINK_HTML_TARGET_SELF = '_self'; | ||
| 58 | const PMF_LINK_HTML_TARGET_TOP = '_top'; | ||
| 59 | const PMF_LINK_NEWS = 'news/'; | ||
| 60 | const PMF_LINK_SITEMAP = 'sitemap/'; | ||
| 61 | const PMF_LINK_SLASH = '/'; | ||
| 62 | const PMF_LINK_SEARCHPART_SEPARATOR = '?'; | ||
| 63 | const PMF_LINK_TAGS = 'tags/'; | ||
| 64 | |||
| 65 | const PMF_LINK_INDEX_ADMIN = '/admin/index.php'; | ||
| 66 | const PMF_LINK_INDEX_HOME = '/index.php'; | ||
| 67 | |||
| 68 | const PMF_LINK_GET_ACTION = 'action'; | ||
| 69 | const PMF_LINK_GET_ARTLANG = 'artlang'; | ||
| 70 | const PMF_LINK_GET_CATEGORY = 'cat'; | ||
| 71 | const PMF_LINK_GET_HIGHLIGHT = 'highlight'; | ||
| 72 | const PMF_LINK_GET_ID = 'id'; | ||
| 73 | const PMF_LINK_GET_LANG = 'lang'; | ||
| 74 | const PMF_LINK_GET_LETTER = 'letter'; | ||
| 75 | const PMF_LINK_GET_NEWS_ID = 'newsid'; | ||
| 76 | const PMF_LINK_GET_NEWS_LANG = 'newslang'; | ||
| 77 | const PMF_LINK_GET_PAGE = 'seite'; | ||
| 78 | const PMF_LINK_GET_SIDS = 'SIDS'; | ||
| 79 | const PMF_LINK_GET_TAGGING_ID = 'tagging_id'; | ||
| 80 | const PMF_LINK_GET_LANGS = 'langs'; | ||
| 81 | |||
| 82 | const PMF_LINK_GET_ACTION_ADD = 'add'; | ||
| 83 | const PMF_LINK_GET_ACTION_ARTIKEL = 'artikel'; | ||
| 84 | const PMF_LINK_GET_ACTION_ASK = 'ask'; | ||
| 85 | const PMF_LINK_GET_ACTION_CONTACT = 'contact'; | ||
| 86 | const PMF_LINK_GET_ACTION_GLOSSARY = 'glossary'; | ||
| 87 | const PMF_LINK_GET_ACTION_HELP = 'help'; | ||
| 88 | const PMF_LINK_GET_ACTION_LOGIN = 'login'; | ||
| 89 | const PMF_LINK_GET_ACTION_NEWS = 'news'; | ||
| 90 | const PMF_LINK_GET_ACTION_OPEN = 'open'; | ||
| 91 | const PMF_LINK_GET_ACTION_PASSWORD = 'password'; | ||
| 92 | const PMF_LINK_GET_ACTION_REGISTER = 'register'; | ||
| 93 | const PMF_LINK_GET_ACTION_SEARCH = 'search'; | ||
| 94 | const PMF_LINK_GET_ACTION_SITEMAP = 'sitemap'; | ||
| 95 | const PMF_LINK_GET_ACTION_SHOW = 'show'; | ||
| 96 | |||
| 97 | const PMF_LINK_HTML_CATEGORY = 'category'; | ||
| 98 | const PMF_LINK_HTML_EXTENSION = '.html'; | ||
| 99 | const PMF_LINK_HTML_SITEMAP = 'sitemap'; | ||
| 100 | |||
| 101 | const PMF_LINK_HTML_ADDCONTENT = 'addcontent.html'; | ||
| 102 | const PMF_LINK_HTML_ASK = 'ask.html'; | ||
| 103 | const PMF_LINK_HTML_CONTACT = 'contact.html'; | ||
| 104 | const PMF_LINK_HTML_GLOSSARY = 'glossary.html'; | ||
| 105 | const PMF_LINK_HTML_HELP = 'help.html'; | ||
| 106 | const PMF_LINK_HTML_LOGIN = 'login.html'; | ||
| 107 | const PMF_LINK_HTML_OPEN = 'open.html'; | ||
| 108 | const PMF_LINK_HTML_PASSWORD = 'password.html'; | ||
| 109 | const PMF_LINK_HTML_REGISTER = 'register.html'; | ||
| 110 | const PMF_LINK_HTML_SEARCH = 'search.html'; | ||
| 111 | const PMF_LINK_HTML_SHOWCAT = 'showcat.html'; | ||
| 112 | |||
| 113 | /** | ||
| 114 | * URL | ||
| 115 | * | ||
| 116 | * @var string | ||
| 117 | */ | ||
| 118 | public $url = ''; | ||
| 119 | |||
| 120 | /** | ||
| 121 | * CSS class | ||
| 122 | * | ||
| 123 | * @var string | ||
| 124 | */ | ||
| 125 | public $class = ''; | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Linktext | ||
| 129 | * | ||
| 130 | * @var string | ||
| 131 | */ | ||
| 132 | public $text = ''; | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Tooltip | ||
| 136 | * | ||
| 137 | * @var string | ||
| 138 | */ | ||
| 139 | public $tooltip = ''; | ||
| 140 | |||
| 141 | /** | ||
| 142 | * Target | ||
| 143 | * | ||
| 144 | * @var string | ||
| 145 | */ | ||
| 146 | public $target = ''; | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Name selector | ||
| 150 | * | ||
| 151 | * @var string | ||
| 152 | */ | ||
| 153 | public $name = ''; | ||
| 154 | |||
| 155 | /** | ||
| 156 | * property specific to the SEO/SEF URLs | ||
| 157 | * | ||
| 158 | * @var string | ||
| 159 | */ | ||
| 160 | public $itemTitle = ''; | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Item property for HTML5 microdata | ||
| 164 | * | ||
| 165 | * @var string | ||
| 166 | */ | ||
| 167 | protected $itemprop = ''; | ||
| 168 | |||
| 169 | /** | ||
| 170 | * rel property | ||
| 171 | * | ||
| 172 | * @var string | ||
| 173 | */ | ||
| 174 | protected $rel = ''; | ||
| 175 | |||
| 176 | /** | ||
| 177 | * id selector | ||
| 178 | * | ||
| 179 | * @var string | ||
| 180 | */ | ||
| 181 | public $id = ''; | ||
| 182 | |||
| 183 | /** | ||
| 184 | * @var PMF_Configuration | ||
| 185 | */ | ||
| 186 | private $_config = null; | ||
| 187 | |||
| 188 | /** | ||
| 189 | * Constructor | ||
| 190 | * | ||
| 191 | * @param string $url URL | ||
| 192 | * @param PMF_Configuration $config | ||
| 193 | * | ||
| 194 | * @return PMF_Link | ||
| 195 | */ | ||
| 196 | public function __construct($url, PMF_Configuration $config) | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Checks if webserver is an IIS Server | ||
| 204 | * | ||
| 205 | * @return boolean | ||
| 206 | */ | ||
| 207 | public static function isIISServer() | ||
| 211 | |||
| 212 | /** | ||
| 213 | * Checks if the the current URL is the main index.php file | ||
| 214 | * | ||
| 215 | * @return boolean | ||
| 216 | */ | ||
| 217 | protected function isHomeIndex() | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Checks if URL is an internal reference | ||
| 228 | * | ||
| 229 | * @return boolean | ||
| 230 | */ | ||
| 231 | protected function isInternalReference() | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Checks if URL is a relative system link | ||
| 245 | * | ||
| 246 | * @return boolean | ||
| 247 | */ | ||
| 248 | protected function isRelativeSystemLink() | ||
| 257 | |||
| 258 | /** | ||
| 259 | * Checks if URL is a system link | ||
| 260 | * | ||
| 261 | * @return boolean | ||
| 262 | */ | ||
| 263 | protected function isSystemLink() | ||
| 273 | |||
| 274 | /** | ||
| 275 | * @param string $itemprop Item property | ||
| 276 | * | ||
| 277 | * @return void | ||
| 278 | */ | ||
| 279 | public function setItemProperty($itemprop) | ||
| 283 | |||
| 284 | /** | ||
| 285 | * @param string $rel rel property | ||
| 286 | * | ||
| 287 | * @return void | ||
| 288 | */ | ||
| 289 | public function setRelation($rel) | ||
| 293 | |||
| 294 | /** | ||
| 295 | * Checks if URL contains a scheme | ||
| 296 | * | ||
| 297 | * @return boolean | ||
| 298 | */ | ||
| 299 | protected function hasScheme() | ||
| 305 | |||
| 306 | /** | ||
| 307 | * Returns a search engine optimized title | ||
| 308 | * | ||
| 309 | * @param string $title | ||
| 310 | * | ||
| 311 | * @return string | ||
| 312 | */ | ||
| 313 | public function getSEOItemTitle($title = '') | ||
| 352 | |||
| 353 | /** | ||
| 354 | * Returns the HTTP GET parameters | ||
| 355 | * | ||
| 356 | * @return array | ||
| 357 | */ | ||
| 358 | protected function getHttpGetParameters() | ||
| 379 | |||
| 380 | /** | ||
| 381 | * Returns the Query of an URL | ||
| 382 | * | ||
| 383 | * @return string | ||
| 384 | */ | ||
| 385 | protected function getQuery() | ||
| 402 | |||
| 403 | /** | ||
| 404 | * Returns the default scheme | ||
| 405 | * | ||
| 406 | * @return string | ||
| 407 | */ | ||
| 408 | protected function getDefaultScheme() | ||
| 417 | |||
| 418 | /** | ||
| 419 | * Returns the system scheme, http or https | ||
| 420 | * | ||
| 421 | * @return string | ||
| 422 | */ | ||
| 423 | public function getSystemScheme() | ||
| 434 | |||
| 435 | /** | ||
| 436 | * Returns the relative URI | ||
| 437 | * | ||
| 438 | * @param string $path | ||
| 439 | * | ||
| 440 | * @return string | ||
| 441 | */ | ||
| 442 | public static function getSystemRelativeUri($path = null) | ||
| 450 | |||
| 451 | /** | ||
| 452 | * Returns the system URI | ||
| 453 | * | ||
| 454 | * @param string $path | ||
| 455 | * | ||
| 456 | * @return string | ||
| 457 | */ | ||
| 458 | public function getSystemUri($path = null) | ||
| 472 | |||
| 473 | /** | ||
| 474 | * Builds a HTML anchor | ||
| 475 | * | ||
| 476 | * @return string | ||
| 477 | */ | ||
| 478 | public function toHtmlAnchor() | ||
| 523 | |||
| 524 | /** | ||
| 525 | * Appends the session id | ||
| 526 | * | ||
| 527 | * @param string $url URL | ||
| 528 | * @param integer $sids Session Id | ||
| 529 | * | ||
| 530 | * @return string | ||
| 531 | */ | ||
| 532 | protected function appendSids($url, $sids) | ||
| 541 | |||
| 542 | /** | ||
| 543 | * Rewrites a URL string | ||
| 544 | * | ||
| 545 | * @param boolean $forceNoModrewriteSupport Force no rewrite support | ||
| 546 | * | ||
| 547 | * @return string | ||
| 548 | */ | ||
| 549 | public function toString($forceNoModrewriteSupport = false) | ||
| 694 | |||
| 695 | /** | ||
| 696 | * Transforms a URI | ||
| 697 | * | ||
| 698 | * @return string | ||
| 699 | */ | ||
| 700 | public function toUri() | ||
| 711 | |||
| 712 | /** | ||
| 713 | * Returns the current URL | ||
| 714 | * | ||
| 715 | * @return string | ||
| 716 | */ | ||
| 717 | public function getCurrentUrl() | ||
| 725 | |||
| 726 | /** | ||
| 727 | * Static method to generate simple HTML anchors | ||
| 728 | * | ||
| 729 | * @static | ||
| 730 | * @param string $url URL | ||
| 731 | * @param string $text Text | ||
| 732 | * @param bool $active Add CSS class named "active"? | ||
| 733 | * | ||
| 734 | * @return string | ||
| 735 | */ | ||
| 736 | public static function renderNavigationLink($url, $text, $active = false) | ||
| 745 | } |