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 |
||
| 27 | class Clickable extends FilterAbstract |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var array default configuration values |
||
| 31 | */ |
||
| 32 | protected static $defaultConfiguration = [ |
||
| 33 | 'enabled' => true, |
||
| 34 | 'truncate_length' => 60, |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Make and URL's in the text clickable links |
||
| 39 | * |
||
| 40 | * @param string $text text string to filter |
||
| 41 | * |
||
| 42 | * @return mixed |
||
| 43 | */ |
||
| 44 | 4 | public function applyFilter($text) |
|
| 45 | { |
||
| 46 | 4 | if (!$this->config['enabled']) { |
|
| 47 | return $text; |
||
| 48 | } |
||
| 49 | |||
| 50 | 4 | $valid_chars = "a-z0-9\/\-_+=.~!%@?#&;:$\|"; |
|
| 51 | 4 | $end_chars = "a-z0-9\/\-_+=~!%@?#&;:$\|"; |
|
| 52 | |||
| 53 | 4 | $pattern = "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$valid_chars}]+[{$end_chars}])/i"; |
|
| 54 | 4 | $text = preg_replace_callback( |
|
| 55 | 4 | $pattern, |
|
| 56 | 4 | View Code Duplication | function ($match) { |
| 57 | 1 | return $match[1] . "<a href=\"$match[2]://$match[3]\" title=\"$match[2]://$match[3]\"" |
|
| 58 | 1 | . "rel=\"external\">$match[2]://".$this->truncate($match[3]).'</a>'; |
|
| 59 | 4 | }, |
|
| 60 | 4 | $text |
|
| 61 | ); |
||
| 62 | |||
| 63 | 4 | $pattern = "/(^|[^]_a-z0-9-=\"'\/:\.])www\.((([a-zA-Z0-9\-]*\.){1,}){1}([a-zA-Z]{2,6}){1})((\/([a-zA-Z0-9\-\._\?\,\'\/\\+&%\$#\=~])*)*)/i"; |
|
| 64 | 4 | $text = preg_replace_callback( |
|
| 65 | 4 | $pattern, |
|
| 66 | 4 | View Code Duplication | function ($match) { |
| 67 | return $match[1] ."<a href=\"http://www.$match[2]$match[6]\" " |
||
| 68 | . "title=\"www.$match[2]$match[6]\" rel=\"external\">" . |
||
| 69 | $this->truncate('www.'.$match[2].$match[6]) .'</a>'; |
||
| 70 | 4 | }, |
|
| 71 | 4 | $text |
|
| 72 | ); |
||
| 73 | |||
| 74 | 4 | $pattern = "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$valid_chars}]+[{$end_chars}])/i"; |
|
| 75 | 4 | $text = preg_replace_callback( |
|
| 76 | 4 | $pattern, |
|
| 77 | 4 | View Code Duplication | function ($match) { |
| 78 | return $match[1]."<a href=\"ftp://ftp.$match[2].$match[3]\" " |
||
| 79 | . "title=\"ftp.$match[2].$match[3]\" rel=\"external\">" |
||
| 80 | . $this->truncate('ftp.'.$match[2].$match[3]) .'</a>'; |
||
| 81 | 4 | }, |
|
| 82 | 4 | $text |
|
| 83 | ); |
||
| 84 | |||
| 85 | 4 | $pattern = "/(^|[^]_a-z0-9-=\"'\/:\.])([-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+)@((?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?)/i"; |
|
| 86 | 4 | $text = preg_replace_callback( |
|
| 87 | 4 | $pattern, |
|
| 88 | 4 | View Code Duplication | function ($match) { |
| 89 | 1 | return $match[1]. "<a href=\"mailto:$match[2]@$match[3]\" title=\"$match[2]@$match[3]\">" |
|
| 90 | 1 | . $this->truncate($match[2] . "@" . $match[3]) . '</a>'; |
|
| 91 | 4 | }, |
|
| 92 | 4 | $text |
|
| 93 | ); |
||
| 94 | |||
| 95 | |||
| 96 | 4 | return $text; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * truncate string in context of |
||
| 101 | * |
||
| 102 | * @param string $text string to be truncated |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | View Code Duplication | protected function truncate($text) |
|
| 120 | } |
||
| 121 |