Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Parser extends ParserBase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var \s9e\TextFormatter\Utils\Http\Client Client used to perform HTTP request |
||
| 19 | */ |
||
| 20 | protected static $client; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | */ |
||
| 25 | 407 | public function parse($text, array $matches) |
|
| 26 | { |
||
| 27 | 407 | foreach ($matches as $m) |
|
| 28 | { |
||
| 29 | 407 | $url = $m[0][0]; |
|
| 30 | 407 | $pos = $m[0][1]; |
|
| 31 | 407 | $len = strlen($url); |
|
| 32 | |||
| 33 | // Give that tag priority over other tags such as Autolink's |
||
| 34 | 407 | $tag = $this->parser->addSelfClosingTag($this->config['tagName'], $pos, $len, -10); |
|
| 35 | 407 | $tag->setAttribute('url', $url); |
|
| 36 | } |
||
| 37 | 407 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Filter a MEDIA tag |
||
| 41 | * |
||
| 42 | * This will always invalidate the original tag, and possibly replace it with the tag that |
||
| 43 | * corresponds to the media site |
||
| 44 | * |
||
| 45 | * @param Tag $tag The original tag |
||
| 46 | * @param TagStack $tagStack Parser instance, so that we can add the new tag to the stack |
||
| 47 | * @param array $sites Map of [host => siteId] |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | 407 | public static function filterTag(Tag $tag, TagStack $tagStack, array $sites) |
|
| 51 | { |
||
| 52 | 407 | $tag->invalidate(); |
|
| 53 | 407 | if ($tag->hasAttribute('site')) |
|
| 54 | { |
||
| 55 | 5 | self::addTagFromMediaId($tag, $tagStack, $sites); |
|
| 56 | } |
||
| 57 | 403 | elseif ($tag->hasAttribute('url')) |
|
| 58 | { |
||
| 59 | 403 | self::addTagFromMediaUrl($tag, $tagStack, $sites); |
|
| 60 | } |
||
| 61 | 407 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Invalidate given tag if it doesn't have at least one non-default attribute |
||
| 65 | * |
||
| 66 | * @param Tag $tag The original tag |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | 60 | public static function hasNonDefaultAttribute(Tag $tag) |
|
| 70 | { |
||
| 71 | 60 | foreach ($tag->getAttributes() as $attrName => $void) |
|
| 72 | { |
||
| 73 | 60 | if ($attrName !== 'url') |
|
| 74 | { |
||
| 75 | 60 | return; |
|
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | 6 | $tag->invalidate(); |
|
| 80 | 6 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Scrape the content of an URL to extract some data |
||
| 84 | * |
||
| 85 | * @param Tag $tag Source tag |
||
| 86 | * @param array $scrapeConfig Array of scrape directives |
||
| 87 | * @param string $cacheDir Path to the cache directory |
||
| 88 | * @return bool Unconditionally TRUE |
||
| 89 | */ |
||
| 90 | 412 | public static function scrape(Tag $tag, array $scrapeConfig, $cacheDir = null) |
|
| 91 | { |
||
| 92 | 412 | if ($tag->hasAttribute('url')) |
|
| 93 | { |
||
| 94 | // Ensure that the URL actually looks like a URL if we want to use it to scrape |
||
| 95 | 411 | $url = $tag->getAttribute('url'); |
|
| 96 | 411 | if (preg_match('#^https?://[^<>"\'\\s]+$#Di', $url)) |
|
| 97 | { |
||
| 98 | 406 | $url = strtolower(substr($url, 0, 5)) . substr($url, 5); |
|
| 99 | 406 | foreach ($scrapeConfig as $scrape) |
|
| 100 | { |
||
| 101 | 143 | self::scrapeEntry($url, $tag, $scrape, $cacheDir); |
|
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | 412 | return true; |
|
| 107 | } |
||
| 108 | |||
| 109 | //============================================================================================== |
||
| 110 | // Internals |
||
| 111 | //============================================================================================== |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add a site tag |
||
| 115 | * |
||
| 116 | * @param Tag $tag The original tag |
||
| 117 | * @param TagStack $tagStack Parser instance, so that we can add the new tag to the stack |
||
| 118 | * @param string $siteId Site ID |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | 405 | protected static function addSiteTag(Tag $tag, TagStack $tagStack, $siteId) |
|
| 122 | { |
||
| 123 | 405 | $endTag = $tag->getEndTag(); |
|
| 124 | 405 | if ($endTag) |
|
| 125 | { |
||
| 126 | 12 | $startPos = $tag->getPos(); |
|
| 127 | 12 | $startLen = $tag->getLen(); |
|
| 128 | 12 | $endPos = $endTag->getPos(); |
|
| 129 | 12 | $endLen = $endTag->getLen(); |
|
| 130 | } |
||
| 131 | else |
||
| 132 | { |
||
| 133 | 401 | $startPos = $tag->getPos(); |
|
| 134 | 401 | $startLen = 0; |
|
| 135 | 401 | $endPos = $tag->getPos() + $tag->getLen(); |
|
| 136 | 401 | $endLen = 0; |
|
| 137 | } |
||
| 138 | |||
| 139 | // Create a new tag and copy this tag's attributes and priority |
||
| 140 | 405 | $tagStack->addTagPair(strtoupper($siteId), $startPos, $startLen, $endPos, $endLen, $tag->getSortPriority())->setAttributes($tag->getAttributes()); |
|
| 141 | 405 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Add a media site tag based on the attributes of a MEDIA tag |
||
| 145 | * |
||
| 146 | * @param Tag $tag The original tag |
||
| 147 | * @param TagStack $tagStack Parser instance |
||
| 148 | * @param array $sites Map of [host => siteId] |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | 5 | protected static function addTagFromMediaId(Tag $tag, TagStack $tagStack, array $sites) |
|
| 152 | { |
||
| 153 | 5 | $siteId = strtolower($tag->getAttribute('site')); |
|
| 154 | 5 | if (in_array($siteId, $sites, true)) |
|
| 155 | { |
||
| 156 | 4 | self::addSiteTag($tag, $tagStack, $siteId); |
|
| 157 | } |
||
| 158 | 5 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Add a media site tag based on the url attribute of a MEDIA tag |
||
| 162 | * |
||
| 163 | * @param Tag $tag The original tag |
||
| 164 | * @param TagStack $tagStack Parser instance |
||
| 165 | * @param array $sites Map of [host => siteId] |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | 403 | protected static function addTagFromMediaUrl(Tag $tag, TagStack $tagStack, array $sites) |
|
| 169 | { |
||
| 170 | // Capture the scheme and (if applicable) host of the URL |
||
| 171 | 403 | $p = parse_url($tag->getAttribute('url')); |
|
| 172 | 403 | if (isset($p['scheme']) && isset($sites[$p['scheme'] . ':'])) |
|
| 173 | { |
||
| 174 | $siteId = $sites[$p['scheme'] . ':']; |
||
| 175 | } |
||
| 176 | 403 | elseif (isset($p['host'])) |
|
| 177 | { |
||
| 178 | 403 | $siteId = self::findSiteIdByHost($p['host'], $sites); |
|
| 179 | } |
||
| 180 | |||
| 181 | 403 | if (!empty($siteId)) |
|
| 182 | { |
||
| 183 | 401 | self::addSiteTag($tag, $tagStack, $siteId); |
|
| 184 | } |
||
| 185 | 403 | } |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Match a given host to a site ID |
||
| 189 | * |
||
| 190 | * @param string $host Host |
||
| 191 | * @param array $sites Map of [host => siteId] |
||
| 192 | * @return string|bool Site ID or FALSE |
||
| 193 | */ |
||
| 194 | 403 | protected static function findSiteIdByHost($host, array $sites) |
|
| 195 | { |
||
| 196 | // Start with the full host then pop domain labels off the start until we get a match |
||
| 197 | do |
||
| 198 | { |
||
| 199 | 403 | if (isset($sites[$host])) |
|
| 200 | { |
||
| 201 | 401 | return $sites[$host]; |
|
| 202 | } |
||
| 203 | |||
| 204 | 264 | $pos = strpos($host, '.'); |
|
| 205 | 264 | if ($pos === false) |
|
| 206 | { |
||
| 207 | 4 | break; |
|
| 208 | } |
||
| 209 | |||
| 210 | 264 | $host = substr($host, 1 + $pos); |
|
| 211 | } |
||
| 212 | 264 | while ($host > ''); |
|
| 213 | |||
| 214 | 4 | return false; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Return a cached instance of the HTTP client |
||
| 219 | * |
||
| 220 | * @return \s9e\TextFormatter\Utils\Http\Client |
||
| 221 | */ |
||
| 222 | 2 | protected static function getHttpClient() |
|
| 223 | { |
||
| 224 | 2 | if (!isset(self::$client)) |
|
| 225 | { |
||
| 226 | 1 | self::$client = Http::getClient(); |
|
| 227 | } |
||
| 228 | 2 | self::$client->timeout = 10; |
|
| 229 | |||
| 230 | 2 | return self::$client; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Replace {@var} tokens in given URL |
||
| 235 | * |
||
| 236 | * @param string $url Original URL |
||
| 237 | * @param string[] $vars Replacements |
||
| 238 | * @return string Modified URL |
||
| 239 | */ |
||
| 240 | 20 | protected static function replaceTokens($url, array $vars) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Scrape the content of an URL to extract some data |
||
| 254 | * |
||
| 255 | * @param string $url Original URL |
||
| 256 | * @param Tag $tag Source tag |
||
| 257 | * @param array $scrape Array of scrape directives |
||
| 258 | * @param string $cacheDir Path to the cache directory |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | 143 | protected static function scrapeEntry($url, Tag $tag, array $scrape, $cacheDir) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Scrape a URL to help fill a tag's attributes |
||
| 294 | * |
||
| 295 | * @param string $url URL to scrape |
||
| 296 | * @param Tag $tag Tag to fill |
||
| 297 | * @param string[] $regexps Regexps used to extract content from the page |
||
| 298 | * @param string|null $cacheDir Path to the cache directory |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | 63 | protected static function scrapeUrl($url, Tag $tag, array $regexps, $cacheDir) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Test whether a tag is missing any of given attributes |
||
| 323 | * |
||
| 324 | * @param Tag $tag |
||
| 325 | * @param string[] $attrNames |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | 143 | protected static function tagIsMissingAnyAttribute(Tag $tag, array $attrNames) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Retrieve external content (possibly from the cache) |
||
| 343 | * |
||
| 344 | * If the cache directory exists, the external content will be saved into it. Cached content is |
||
| 345 | * never pruned |
||
| 346 | * |
||
| 347 | * @param string $url URL |
||
| 348 | * @param string $cacheDir Path to the cache directory |
||
| 349 | * @return string External content |
||
| 350 | */ |
||
| 351 | 63 | protected static function wget($url, $cacheDir = null) |
|
| 382 | } |