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 | 331 | public function parse($text, array $matches) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Filter a MEDIA tag |
||
| 43 | * |
||
| 44 | * This will always invalidate the original tag, and possibly replace it with the tag that |
||
| 45 | * corresponds to the media site |
||
| 46 | * |
||
| 47 | * @param Tag $tag The original tag |
||
| 48 | * @param TagStack $tagStack Parser instance, so that we can add the new tag to the stack |
||
| 49 | * @param array $sites Map of [host => siteId] |
||
| 50 | * @return bool Unconditionally FALSE |
||
| 51 | */ |
||
| 52 | 325 | public static function filterTag(Tag $tag, TagStack $tagStack, array $sites) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Test whether a given tag has at least one non-default attribute |
||
| 68 | * |
||
| 69 | * @param Tag $tag The original tag |
||
| 70 | * @return bool Whether the tag contains an attribute not named "url" |
||
| 71 | */ |
||
| 72 | 52 | public static function hasNonDefaultAttribute(Tag $tag) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Scrape the content of an URL to extract some data |
||
| 87 | * |
||
| 88 | * @param Tag $tag Source tag |
||
| 89 | * @param array $scrapeConfig Array of scrape directives |
||
| 90 | * @param string $cacheDir Path to the cache directory |
||
| 91 | * @return bool Unconditionally TRUE |
||
| 92 | */ |
||
| 93 | 122 | public static function scrape(Tag $tag, array $scrapeConfig, $cacheDir = null) |
|
| 116 | |||
| 117 | //============================================================================================== |
||
| 118 | // Internals |
||
| 119 | //============================================================================================== |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Add a site tag |
||
| 123 | * |
||
| 124 | * @param Tag $tag The original tag |
||
| 125 | * @param TagStack $tagStack Parser instance, so that we can add the new tag to the stack |
||
| 126 | * @param string $siteId Site ID |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | 323 | protected static function addSiteTag(Tag $tag, TagStack $tagStack, $siteId) |
|
| 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 | 6 | protected static function addTagFromMediaId(Tag $tag, TagStack $tagStack, array $sites) |
|
| 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 | 319 | protected static function addTagFromMediaUrl(Tag $tag, TagStack $tagStack, array $sites) |
|
| 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 | 317 | protected static function findSiteIdByHost($host, array $sites) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Return a cached instance of the HTTP client |
||
| 219 | * |
||
| 220 | * @return \s9e\TextFormatter\Utils\Http\Client |
||
| 221 | */ |
||
| 222 | 1 | protected static function getHttpClient() |
|
| 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 | 19 | 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 | 118 | protected static function scrapeEntry($url, Tag $tag, array $scrape, $cacheDir) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Scrape a URL to help fill a tag's attributes |
||
| 295 | * |
||
| 296 | * @param string $url URL to scrape |
||
| 297 | * @param Tag $tag Tag to fill |
||
| 298 | * @param string[] $regexps Regexps used to extract content from the page |
||
| 299 | * @param string|null $cacheDir Path to the cache directory |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | 74 | protected static function scrapeUrl($url, Tag $tag, array $regexps, $cacheDir) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Test whether a tag is missing any of given attributes |
||
| 324 | * |
||
| 325 | * @param Tag $tag |
||
| 326 | * @param string[] $attrNames |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | 118 | protected static function tagIsMissingAnyAttribute(Tag $tag, array $attrNames) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Retrieve external content (possibly from the cache) |
||
| 344 | * |
||
| 345 | * If the cache directory exists, the external content will be saved into it. Cached content is |
||
| 346 | * never pruned |
||
| 347 | * |
||
| 348 | * @param string $url URL |
||
| 349 | * @param string $cacheDir Path to the cache directory |
||
| 350 | * @return string External content |
||
| 351 | */ |
||
| 352 | 74 | protected static function wget($url, $cacheDir = null) |
|
| 382 | } |