Complex classes like SiteChecker 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 SiteChecker, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 17 | class SiteChecker | ||
| 18 | { | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @var array | ||
| 22 | */ | ||
| 23 | protected $messages = []; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * @var Asset[] | ||
| 27 | */ | ||
| 28 | protected $checkedAssets = []; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var Asset | ||
| 32 | */ | ||
| 33 | protected $basePage; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @var SiteCheckObserver | ||
| 37 | */ | ||
| 38 | protected $observer; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @var Client | ||
| 42 | */ | ||
| 43 | protected $client; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * @var Config | ||
| 47 | */ | ||
| 48 | protected $config; | ||
| 49 | |||
| 50 | |||
| 51 | /** | ||
| 52 | * @return Config | ||
| 53 | */ | ||
| 54 | public function getConfig() | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @param Config $config | ||
| 61 | */ | ||
| 62 | public function setConfig($config) | ||
| 66 | |||
| 67 | /** | ||
| 68 | * SiteChecker constructor. | ||
| 69 | * @param \GuzzleHttp\Client $client | ||
| 70 | * @param \SiteChecker\SiteCheckObserver|null $observer | ||
| 71 | */ | ||
| 72 | public function __construct( | ||
| 73 | Client $client, | ||
| 74 | SiteCheckObserver $observer = null | ||
| 75 |     ) { | ||
| 76 | $this->client = $client; | ||
| 77 | $this->observer = $observer ?: new DummyObserver(); | ||
| 78 | $this->config = new Config(); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | /** | ||
| 83 | * @param \SiteChecker\SiteCheckObserver $observer | ||
| 84 | * @return static | ||
| 85 | */ | ||
| 86 | public static function create(SiteCheckObserver $observer) | ||
| 87 |     { | ||
| 88 | $client = new Client([ | ||
| 89 | RequestOptions::ALLOW_REDIRECTS => true, | ||
| 90 | RequestOptions::COOKIES => true, | ||
| 91 | RequestOptions::VERIFY => false, | ||
| 92 | ]); | ||
| 93 | |||
| 94 | return new static($client, $observer); | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Check the site for broken assets. | ||
| 99 | * | ||
| 100 | * @param string $baseUrl | ||
| 101 | */ | ||
| 102 | public function check($baseUrl) | ||
| 103 |     { | ||
| 104 |         if (!$baseUrl instanceof Asset) { | ||
| 105 | $baseUrl = new Asset($baseUrl); | ||
| 106 | } | ||
| 107 | $this->messages = []; | ||
| 108 | $this->basePage = $baseUrl; | ||
| 109 | |||
| 110 | $this->checkAsset($baseUrl); | ||
| 111 |         foreach ($this->config->includedUrls as $includedUrl) { | ||
| 112 | $asset = new Asset($includedUrl); | ||
| 113 | $this->normalizeUrl($asset); | ||
| 114 |             if (!in_array($asset->getUrl(), $this->checkedAssets)) { | ||
| 115 | $this->checkAsset($asset); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | $this->observer->receiveResults($this->checkedAssets); | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | /** | ||
| 123 | * @param Asset $asset | ||
| 124 | */ | ||
| 125 | protected function checkAsset(Asset $asset) | ||
| 126 |     { | ||
| 127 |         if (!$this->shouldBeChecked($asset)) { | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 |         if (!$this->observer->pageToCheck($asset)) { | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | $cookies = $this->config->getCookies(); | ||
| 135 | |||
| 136 |         foreach ($cookies as $key => $cookie) { | ||
| 137 | $cookie['Domain'] = $this->basePage->host; | ||
| 138 | $cookies[$key] = new SetCookie($cookie); | ||
| 139 | } | ||
| 140 | |||
| 141 | $jar = new CookieJar(false, $cookies); | ||
| 142 | |||
| 143 |         try { | ||
| 144 |             $response = $this->client->request('GET', $asset->getURL(), | ||
| 145 | [ | ||
| 146 | 'cookies' => $jar | ||
| 147 | ]); | ||
| 148 |         } catch (RequestException $exception) { | ||
| 149 | $response = $exception->getResponse(); | ||
| 150 |             $asset->setResponseCode('500'); | ||
| 151 | } | ||
| 152 | |||
| 153 |         if ($response) { | ||
| 154 | $asset->setResponseCode($response->getStatusCode()); | ||
| 155 | } | ||
| 156 | |||
| 157 | $this->observer->pageChecked($asset, $response); | ||
| 158 | |||
| 159 | $this->checkedAssets[] = $asset; | ||
| 160 | |||
| 161 |         if (!$response) { | ||
| 162 | return; | ||
| 163 | } | ||
| 164 | |||
| 165 |         if (!$this->isExternal($asset) && $this->isHtmlPage($response)) { | ||
| 166 | $this->checkAllAssets($response->getBody()->getContents(), $asset); | ||
| 167 | } | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | /** | ||
| 172 | * @param Asset $asset | ||
| 173 | * @return bool | ||
| 174 | */ | ||
| 175 | protected function isExternal(Asset $asset) | ||
| 179 | |||
| 180 | /** | ||
| 181 | * Crawl all assets in the given html. | ||
| 182 | * | ||
| 183 | * @param string $html | ||
| 184 | * @param Asset $parentAsset | ||
| 185 | */ | ||
| 186 | protected function checkAllAssets($html, $parentAsset) | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Crawl all assets in the given html. | ||
| 204 | * | ||
| 205 | * @param $html | ||
| 206 | * @param $parentPage | ||
| 207 | * @return array | ||
| 208 | */ | ||
| 209 | protected function getAllAssets($html, $parentPage) | ||
| 252 | |||
| 253 | /** | ||
| 254 | * @param $html | ||
| 255 | * @param $selector | ||
| 256 | * @param $urlAttribute | ||
| 257 | * @param $type | ||
| 258 | * @param $parentPage | ||
| 259 | * @return array | ||
| 260 | */ | ||
| 261 | protected function createAssetsFromDOMElements( | ||
| 292 | |||
| 293 | /** | ||
| 294 | * @param \Psr\Http\Message\ResponseInterface $response | ||
| 295 | * @return bool | ||
| 296 | */ | ||
| 297 | protected function isHtmlPage(ResponseInterface $response) | ||
| 306 | |||
| 307 | /** | ||
| 308 | * @param Asset $asset | ||
| 309 | * @return bool | ||
| 310 | */ | ||
| 311 | protected function shouldBeChecked(Asset $asset) | ||
| 326 | |||
| 327 | /** | ||
| 328 | * @param \SiteChecker\Asset $asset | ||
| 329 | * @return bool | ||
| 330 | */ | ||
| 331 | protected function isAlreadyChecked(Asset $asset) | ||
| 335 | |||
| 336 | |||
| 337 | /** | ||
| 338 | * Normalize the given url. | ||
| 339 | * @param \SiteChecker\Asset $asset | ||
| 340 | * @return $this | ||
| 341 | */ | ||
| 342 | protected function normalizeUrl(Asset $asset) | ||
| 357 | |||
| 358 | /** | ||
| 359 | * @return \SiteChecker\Asset[] | ||
| 360 | */ | ||
| 361 | public function getResults() | ||
| 365 | |||
| 366 | } | ||
| 367 |