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 |
||
| 10 | class Link extends Parser implements ParserInterface |
||
| 11 | { |
||
| 12 | const LINK_TAG_REGEXP = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; |
||
| 13 | const LINK_INTERNAL_REGEXP = '\[\[(.*?)(\|(.*?))*\]\]'; |
||
| 14 | |||
| 15 | const REGEXP_EMAIL = '#(^|[\n \[\]\:<>&;]|\()([a-z0-9&\-_.]+?@[\w\-]+\.(?:[\w\-\.]+\.)?[\w]+)#i'; |
||
| 16 | |||
| 17 | private const PARENTHESES_LEVEL = 3; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var PageRepository |
||
| 21 | */ |
||
| 22 | private $page; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $host; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var HtmlBuilder|null |
||
| 31 | */ |
||
| 32 | private $html; |
||
| 33 | |||
| 34 | /** @var UrlFormatter */ |
||
| 35 | private $urlParser; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Link constructor. |
||
| 39 | * |
||
| 40 | * @param PageRepository $page |
||
| 41 | * @param string $host |
||
| 42 | * @param HtmlBuilder|null $html |
||
| 43 | */ |
||
| 44 | public function __construct(PageRepository $page, string $host, HtmlBuilder $html = null) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $text |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function parse($text) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $text |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | protected function parseLinks($text) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $text |
||
| 110 | * @param string $match |
||
| 111 | * @param string $url |
||
| 112 | * @param string $title |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | protected function parseInternalLink($text, $match, $url, $title) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $text |
||
| 130 | * @param string $match |
||
| 131 | * @param string $url |
||
| 132 | * @param string $title |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | protected function parseYoutubeLinks($text, $match, $url, $title) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Parse "old" coyote links like [[Foo/Bar]] to http://4programmers.net/Foo/Bar |
||
| 178 | * |
||
| 179 | * @param string $text |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | protected function parseInternalAccessors($text) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $text |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | protected function parseUrl(string $text): string |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $text |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | protected function parseEmail(string $text): string |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string|null $time |
||
| 237 | * @return null|string |
||
| 238 | */ |
||
| 239 | private function timeToSeconds($time) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $videoId |
||
| 254 | * @param string $start |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | private function makeIframe(string $videoId, string $start = null): string |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get path from url only if it's internal link (false if it's NOT internal link) |
||
| 270 | * |
||
| 271 | * @example http://4programmers.net/Foo/Bar => /Foo/Bar |
||
| 272 | * @param string $url |
||
| 273 | * @return string|false |
||
| 274 | */ |
||
| 275 | private function getPathFromInternalUrl($url) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array|false $components |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | private function isUrl($components) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $path |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | private function getHashFromPath(&$path) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get host without "www" at the beginning. |
||
| 321 | * |
||
| 322 | * @param string $host |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | private function getHost(string $host): string |
||
| 335 | } |
||
| 336 |