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) |
||
197 | { |
||
198 | $this->url = $url; |
||
199 | $this->_config = $config; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Checks if webserver is an IIS Server |
||
204 | * |
||
205 | * @return boolean |
||
206 | */ |
||
207 | public static function isIISServer() |
||
208 | { |
||
209 | return (isset($_SERVER['ALL_HTTP']) || isset($_SERVER['COMPUTERNAME']) || isset($_SERVER['APP_POOL_ID'])); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Checks if the the current URL is the main index.php file |
||
214 | * |
||
215 | * @return boolean |
||
216 | */ |
||
217 | protected function isHomeIndex() |
||
218 | { |
||
219 | if (!$this->isSystemLink()) { |
||
220 | return false; |
||
221 | } |
||
222 | |||
223 | return !(false === strpos($this->url, self::PMF_LINK_INDEX_HOME)); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Checks if URL is an internal reference |
||
228 | * |
||
229 | * @return boolean |
||
230 | */ |
||
231 | protected function isInternalReference() |
||
232 | { |
||
233 | if ($this->isRelativeSystemLink()) { |
||
234 | return true; |
||
235 | } |
||
236 | if (false === strpos($this->url, '#')) { |
||
237 | return false; |
||
238 | } |
||
239 | |||
240 | return (strpos($this->url, '#') == 0); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Checks if URL is a relative system link |
||
245 | * |
||
246 | * @return boolean |
||
247 | */ |
||
248 | protected function isRelativeSystemLink() |
||
249 | { |
||
250 | $slashIdx = strpos($this->url, self::PMF_LINK_SLASH); |
||
251 | if (false === $slashIdx) { |
||
252 | return false; |
||
253 | } |
||
254 | |||
255 | return ($slashIdx == 0); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Checks if URL is a system link |
||
260 | * |
||
261 | * @return boolean |
||
262 | */ |
||
263 | protected function isSystemLink() |
||
264 | { |
||
265 | // a. Is the url relative, starting with '/'? |
||
266 | // b. Is the url related to the current running PMF system? |
||
267 | if ($this->isRelativeSystemLink()) { |
||
268 | return true; |
||
269 | } |
||
270 | // $_SERVER['HTTP_HOST'] is the name of the website or virtual host name |
||
271 | return !(false === strpos($this->url, $_SERVER['HTTP_HOST'])); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param string $itemprop Item property |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | public function setItemProperty($itemprop) |
||
280 | { |
||
281 | $this->itemprop = $itemprop; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param string $rel rel property |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | public function setRelation($rel) |
||
290 | { |
||
291 | $this->rel = $rel; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Checks if URL contains a scheme |
||
296 | * |
||
297 | * @return boolean |
||
298 | */ |
||
299 | protected function hasScheme() |
||
300 | { |
||
301 | $parsed = parse_url($this->url); |
||
302 | |||
303 | return (!empty($parsed['scheme'])); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Returns a search engine optimized title |
||
308 | * |
||
309 | * @param string $title |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | public function getSEOItemTitle($title = '') |
||
314 | { |
||
315 | if ('' === $title) { |
||
316 | $title = $this->itemTitle; |
||
317 | } |
||
318 | |||
319 | $itemTitle = trim($title); |
||
320 | // Lower the case (aesthetic) |
||
321 | $itemTitle = PMF_String::strtolower($itemTitle); |
||
322 | // Use '_' for some other characters for: |
||
323 | // 1. avoiding regexp match break; |
||
324 | // 2. improving the reading. |
||
325 | $itemTitle = str_replace(array('-', "'", '/', '''), '_', $itemTitle); |
||
326 | // 1. Remove any CR LF sequence |
||
327 | // 2. Use a '-' for the words separation |
||
328 | $itemTitle = PMF_String::preg_replace('/\s/m', '-', $itemTitle); |
||
329 | // Hack: remove some chars for having a better readable title |
||
330 | $itemTitle = str_replace( |
||
331 | array('+', ',', ';', ':', '.', '?', '!', '"', '(', ')', '[', ']', '{', '}', '<', '>'), |
||
332 | '', |
||
333 | $itemTitle |
||
334 | ); |
||
335 | // Hack: move some chars to "similar" but plain ASCII chars |
||
336 | $itemTitle = str_replace( |
||
337 | array( |
||
338 | 'à', 'è', 'é', 'ì', 'ò', 'ù', 'ä', 'ö', 'ü', 'ß', 'Ä', 'Ö', 'Ü', |
||
339 | 'č', 'ę', 'ė', 'į', 'š', 'ų', 'ū', 'ž' |
||
340 | ), |
||
341 | array( |
||
342 | 'a', 'e', 'e', 'i', 'o', 'u', 'ae', 'oe', 'ue', 'ss', 'Ae', 'Oe', 'Ue', |
||
343 | 'c', 'e', 'e', 'i', 's', 'u', 'u', 'z' |
||
344 | ), |
||
345 | $itemTitle |
||
346 | ); |
||
347 | // Clean up |
||
348 | $itemTitle = PMF_String::preg_replace('/-[\-]+/m', '-', $itemTitle); |
||
349 | |||
350 | return $itemTitle; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Returns the HTTP GET parameters |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | protected function getHttpGetParameters() |
||
384 | |||
385 | /** |
||
386 | * Returns the Query of an URL |
||
387 | * |
||
388 | * @return string |
||
389 | */ |
||
390 | protected function getQuery() |
||
391 | { |
||
392 | $query = ''; |
||
393 | |||
394 | if (!empty($this->url)) { |
||
395 | $parsed = parse_url($this->url); |
||
396 | |||
397 | View Code Duplication | if (isset($parsed['query'])) { |
|
398 | $query['main'] = filter_var($parsed['query'], FILTER_SANITIZE_STRIPPED); |
||
399 | } |
||
400 | View Code Duplication | if (isset($parsed['fragment'])) { |
|
401 | $query['fragment'] = filter_var($parsed['fragment'], FILTER_SANITIZE_STRIPPED); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | return $query; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Returns the default scheme |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | protected function getDefaultScheme() |
||
414 | { |
||
415 | $scheme = 'http://'; |
||
416 | if ($this->isSystemLink()) { |
||
417 | $scheme = $this->getSystemScheme(); |
||
418 | } |
||
419 | |||
420 | return $scheme; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Returns the system scheme, http or https |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | public function getSystemScheme() |
||
450 | |||
451 | /** |
||
452 | * Returns the relative URI |
||
453 | * |
||
454 | * @param string $path |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | public static function getSystemRelativeUri($path = null) |
||
459 | { |
||
460 | if (isset($path)) { |
||
461 | return str_replace($path, '', $_SERVER['SCRIPT_NAME']); |
||
462 | } |
||
466 | |||
467 | /** |
||
468 | * Returns the system URI |
||
469 | * |
||
470 | * @param string $path |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | public function getSystemUri($path = null) |
||
488 | |||
489 | /** |
||
490 | * Builds a HTML anchor |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | public function toHtmlAnchor() |
||
539 | |||
540 | /** |
||
541 | * Appends the session id |
||
542 | * |
||
543 | * @param string $url URL |
||
544 | * @param integer $sids Session Id |
||
545 | * |
||
546 | * @return string |
||
547 | */ |
||
548 | protected function appendSids($url, $sids) |
||
557 | |||
558 | /** |
||
559 | * Rewrites a URL string |
||
560 | * |
||
561 | * @param boolean $forceNoModrewriteSupport Force no rewrite support |
||
562 | * |
||
563 | * @return string |
||
564 | */ |
||
565 | public function toString($forceNoModrewriteSupport = false) |
||
710 | |||
711 | /** |
||
712 | * Transforms a URI |
||
713 | * |
||
714 | * @return string |
||
715 | */ |
||
716 | public function toUri() |
||
727 | |||
728 | /** |
||
729 | * Returns the current URL |
||
730 | * |
||
731 | * @return string |
||
732 | */ |
||
733 | public function getCurrentUrl() |
||
741 | |||
742 | /** |
||
743 | * Static method to generate simple HTML anchors |
||
744 | * |
||
745 | * @static |
||
746 | * @param string $url URL |
||
747 | * @param string $text Text |
||
748 | * @param bool $active Add CSS class named "active"? |
||
749 | * |
||
750 | * @return string |
||
751 | */ |
||
752 | public static function renderNavigationLink($url, $text, $active = false) |
||
761 | } |