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  | 
            ||
| 8 | class Link extends Parser implements ParserInterface  | 
            ||
| 9 | { | 
            ||
| 10 | const LINK_TAG_REGEXP = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";  | 
            ||
| 11 | const LINK_INTERNAL_REGEXP = '\[\[(.*?)(\|(.*?))*\]\]';  | 
            ||
| 12 | |||
| 13 | const REGEXP_EMAIL = '#(^|[\n \[\]\:<>&;]|\()([a-z0-9&\-_.]+?@[\w\-]+\.(?:[\w\-\.]+\.)?[\w]+)#i';  | 
            ||
| 14 | |||
| 15 | /**  | 
            ||
| 16 | * @var PageRepository  | 
            ||
| 17 | */  | 
            ||
| 18 | private $page;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * @var string  | 
            ||
| 22 | */  | 
            ||
| 23 | private $host;  | 
            ||
| 24 | |||
| 25 | /**  | 
            ||
| 26 | * @var HtmlBuilder|null  | 
            ||
| 27 | */  | 
            ||
| 28 | private $html;  | 
            ||
| 29 | |||
| 30 | /** @var UrlFormatter */  | 
            ||
| 31 | private $urlParser;  | 
            ||
| 32 | |||
| 33 | /**  | 
            ||
| 34 | * Link constructor.  | 
            ||
| 35 | *  | 
            ||
| 36 | * @param PageRepository $page  | 
            ||
| 37 | * @param string $host  | 
            ||
| 38 | * @param HtmlBuilder|null $html  | 
            ||
| 39 | */  | 
            ||
| 40 | public function __construct(PageRepository $page, string $host, HtmlBuilder $html = null)  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * @param string $text  | 
            ||
| 50 | * @return string  | 
            ||
| 51 | */  | 
            ||
| 52 | public function parse($text)  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @param string $text  | 
            ||
| 84 | * @return string  | 
            ||
| 85 | */  | 
            ||
| 86 | protected function parseLinks($text)  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * @param string $text  | 
            ||
| 106 | * @param string $match  | 
            ||
| 107 | * @param string $url  | 
            ||
| 108 | * @param string $title  | 
            ||
| 109 | * @return string  | 
            ||
| 110 | */  | 
            ||
| 111 | protected function parseInternalLink($text, $match, $url, $title)  | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * @param string $text  | 
            ||
| 126 | * @param string $match  | 
            ||
| 127 | * @param string $url  | 
            ||
| 128 | * @param string $title  | 
            ||
| 129 | * @return string  | 
            ||
| 130 | */  | 
            ||
| 131 | protected function parseYoutubeLinks($text, $match, $url, $title)  | 
            ||
| 171 | |||
| 172 | /**  | 
            ||
| 173 | * Parse "old" coyote links like [[Foo/Bar]] to http://4programmers.net/Foo/Bar  | 
            ||
| 174 | *  | 
            ||
| 175 | * @param string $text  | 
            ||
| 176 | * @return string  | 
            ||
| 177 | */  | 
            ||
| 178 | protected function parseInternalAccessors($text)  | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * @param string $text  | 
            ||
| 215 | * @return string  | 
            ||
| 216 | */  | 
            ||
| 217 | protected function parseUrl(string $text): string  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * @param string $text  | 
            ||
| 224 | * @return string  | 
            ||
| 225 | */  | 
            ||
| 226 | protected function parseEmail(string $text): string  | 
            ||
| 230 | |||
| 231 | /**  | 
            ||
| 232 | * @param string|null $time  | 
            ||
| 233 | * @return null|string  | 
            ||
| 234 | */  | 
            ||
| 235 | private function timeToSeconds($time)  | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * @param string $videoId  | 
            ||
| 250 | * @param string $start  | 
            ||
| 251 | * @return string  | 
            ||
| 252 | */  | 
            ||
| 253 | private function makeIframe(string $videoId, string $start = null): string  | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * Get path from url only if it's internal link (false if it's NOT internal link)  | 
            ||
| 266 | *  | 
            ||
| 267 | * @example http://4programmers.net/Foo/Bar => /Foo/Bar  | 
            ||
| 268 | * @param string $url  | 
            ||
| 269 | * @return string|false  | 
            ||
| 270 | */  | 
            ||
| 271 | private function getPathFromInternalUrl($url)  | 
            ||
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * @param array|false $components  | 
            ||
| 288 | * @return bool  | 
            ||
| 289 | */  | 
            ||
| 290 | private function isUrl($components)  | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * @param string $path  | 
            ||
| 301 | * @return string  | 
            ||
| 302 | */  | 
            ||
| 303 | private function getHashFromPath(&$path)  | 
            ||
| 314 | |||
| 315 | /**  | 
            ||
| 316 | * Get host without "www" at the beginning.  | 
            ||
| 317 | *  | 
            ||
| 318 | * @param string $host  | 
            ||
| 319 | * @return string  | 
            ||
| 320 | */  | 
            ||
| 321 | private function getHost(string $host): string  | 
            ||
| 331 | }  | 
            ||
| 332 | 
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.