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) |
|
| 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 | 405 | public static function filterTag(Tag $tag, TagStack $tagStack, array $sites) |
|
| 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) |
|
| 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 | 410 | public static function scrape(Tag $tag, array $scrapeConfig, $cacheDir = null) |
|
| 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 | 403 | 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 | 5 | 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 | 401 | protected static function addTagFromMediaUrl(Tag $tag, TagStack $tagStack, array $sites) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Match a given host to a site ID |
||
| 184 | * |
||
| 185 | * @param string $host Host |
||
| 186 | * @param array $sites Map of [host => siteId] |
||
| 187 | * @return string|bool Site ID or FALSE |
||
| 188 | */ |
||
| 189 | 401 | protected static function findSiteIdByHost($host, array $sites) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Return a cached instance of the HTTP client |
||
| 214 | * |
||
| 215 | * @param string|null $cacheDir |
||
| 216 | * @return \s9e\TextFormatter\Utils\Http\Client |
||
| 217 | 2 | */ |
|
| 218 | protected static function getHttpClient($cacheDir) |
||
| 219 | 2 | { |
|
| 220 | if (!isset(self::$client)) |
||
| 221 | 1 | { |
|
| 222 | self::$client = (isset($cacheDir)) ? Http::getCachingClient($cacheDir) : Http::getClient(); |
||
| 223 | 2 | } |
|
| 224 | |||
| 225 | 2 | return self::$client; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Replace {@var} tokens in given URL |
||
| 230 | * |
||
| 231 | * @param string $url Original URL |
||
| 232 | * @param string[] $vars Replacements |
||
| 233 | * @return string Modified URL |
||
| 234 | */ |
||
| 235 | 18 | protected static function replaceTokens($url, array $vars) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Scrape the content of an URL to extract some data |
||
| 249 | * |
||
| 250 | * @param string $url Original URL |
||
| 251 | * @param Tag $tag Source tag |
||
| 252 | * @param array $scrape Array of scrape directives |
||
| 253 | * @param string $cacheDir Path to the cache directory |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | 141 | protected static function scrapeEntry($url, Tag $tag, array $scrape, $cacheDir) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Scrape a URL to help fill a tag's attributes |
||
| 289 | * |
||
| 290 | * @param string $url URL to scrape |
||
| 291 | * @param Tag $tag Tag to fill |
||
| 292 | * @param string[] $regexps Regexps used to extract content from the page |
||
| 293 | * @param string|null $cacheDir Path to the cache directory |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | 61 | protected static function scrapeUrl($url, Tag $tag, array $regexps, $cacheDir) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Test whether a tag is missing any of given attributes |
||
| 318 | * |
||
| 319 | * @param Tag $tag |
||
| 320 | * @param string[] $attrNames |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 141 | protected static function tagIsMissingAnyAttribute(Tag $tag, array $attrNames) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Retrieve external content (possibly from the cache) |
||
| 338 | * |
||
| 339 | * If the cache directory exists, the external content will be saved into it. Cached content is |
||
| 340 | * never pruned |
||
| 341 | * |
||
| 342 | * @param string $url URL |
||
| 343 | * @param string $cacheDir Path to the cache directory |
||
| 344 | * @return string External content |
||
| 345 | */ |
||
| 346 | 61 | protected static function wget($url, $cacheDir = null) |
|
| 352 | } |