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 | 386 | public function parse($text, array $matches) |
|
| 26 | { |
||
| 27 | 386 | foreach ($matches as $m) |
|
| 28 | { |
||
| 29 | 386 | $url = $m[0][0]; |
|
| 30 | 386 | $pos = $m[0][1]; |
|
| 31 | 386 | $len = strlen($url); |
|
| 32 | |||
| 33 | // Give that tag priority over other tags such as Autolink's |
||
| 34 | 386 | $tag = $this->parser->addSelfClosingTag($this->config['tagName'], $pos, $len, -10); |
|
| 35 | 386 | $tag->setAttribute('url', $url); |
|
| 36 | 386 | } |
|
| 37 | 386 | } |
|
| 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 | 387 | public static function filterTag(Tag $tag, TagStack $tagStack, array $sites) |
|
| 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 | 64 | public static function hasNonDefaultAttribute(Tag $tag) |
|
| 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 | 150 | public static function scrape(Tag $tag, array $scrapeConfig, $cacheDir = null) |
|
| 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 | 385 | protected static function addSiteTag(Tag $tag, TagStack $tagStack, $siteId) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Add a media site tag based on the attributes of a MEDIA tag |
||
| 136 | * |
||
| 137 | * @param Tag $tag The original tag |
||
| 138 | * @param TagStack $tagStack Parser instance |
||
| 139 | * @param array $sites Map of [host => siteId] |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | 5 | protected static function addTagFromMediaId(Tag $tag, TagStack $tagStack, array $sites) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Add a media site tag based on the url attribute of a MEDIA tag |
||
| 153 | * |
||
| 154 | * @param Tag $tag The original tag |
||
| 155 | * @param TagStack $tagStack Parser instance |
||
| 156 | * @param array $sites Map of [host => siteId] |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 383 | protected static function addTagFromMediaUrl(Tag $tag, TagStack $tagStack, array $sites) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Match a given host to a site ID |
||
| 180 | * |
||
| 181 | * @param string $host Host |
||
| 182 | * @param array $sites Map of [host => siteId] |
||
| 183 | * @return string|bool Site ID or FALSE |
||
| 184 | */ |
||
| 185 | 379 | protected static function findSiteIdByHost($host, array $sites) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Return a cached instance of the HTTP client |
||
| 210 | * |
||
| 211 | * @return \s9e\TextFormatter\Utils\Http\Client |
||
| 212 | */ |
||
| 213 | protected static function getHttpClient() |
||
| 214 | { |
||
| 215 | if (!isset(self::$client)) |
||
| 216 | { |
||
| 217 | self::$client = Http::getClient(); |
||
| 218 | } |
||
| 219 | self::$client->timeout = 10; |
||
| 220 | |||
| 221 | return self::$client; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Replace {@var} tokens in given URL |
||
| 226 | * |
||
| 227 | * @param string $url Original URL |
||
| 228 | * @param string[] $vars Replacements |
||
| 229 | * @return string Modified URL |
||
| 230 | */ |
||
| 231 | 19 | protected static function replaceTokens($url, array $vars) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Scrape the content of an URL to extract some data |
||
| 245 | * |
||
| 246 | * @param string $url Original URL |
||
| 247 | * @param Tag $tag Source tag |
||
| 248 | * @param array $scrape Array of scrape directives |
||
| 249 | * @param string $cacheDir Path to the cache directory |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | 143 | protected static function scrapeEntry($url, Tag $tag, array $scrape, $cacheDir) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Scrape a URL to help fill a tag's attributes |
||
| 286 | * |
||
| 287 | * @param string $url URL to scrape |
||
| 288 | * @param Tag $tag Tag to fill |
||
| 289 | * @param string[] $regexps Regexps used to extract content from the page |
||
| 290 | * @param string|null $cacheDir Path to the cache directory |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | 68 | protected static function scrapeUrl($url, Tag $tag, array $regexps, $cacheDir) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Test whether a tag is missing any of given attributes |
||
| 315 | * |
||
| 316 | * @param Tag $tag |
||
| 317 | * @param string[] $attrNames |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 143 | protected static function tagIsMissingAnyAttribute(Tag $tag, array $attrNames) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Retrieve external content (possibly from the cache) |
||
| 335 | * |
||
| 336 | * If the cache directory exists, the external content will be saved into it. Cached content is |
||
| 337 | * never pruned |
||
| 338 | * |
||
| 339 | * @param string $url URL |
||
| 340 | * @param string $cacheDir Path to the cache directory |
||
| 341 | * @return string External content |
||
| 342 | */ |
||
| 343 | 68 | protected static function wget($url, $cacheDir = null) |
|
| 374 | } |