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