Complex classes like BrowscapUpdater 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 BrowscapUpdater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class BrowscapUpdater |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The cache instance |
||
| 27 | * |
||
| 28 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface|null |
||
| 29 | */ |
||
| 30 | private $cache; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var @var \Psr\Log\LoggerInterface|null |
||
| 34 | */ |
||
| 35 | private $logger; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \GuzzleHttp\ClientInterface|null |
||
| 39 | */ |
||
| 40 | private $client; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Curl connect timeout in seconds |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $connectTimeout = 5; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Gets a cache instance |
||
| 51 | * |
||
| 52 | * @return \BrowscapPHP\Cache\BrowscapCacheInterface |
||
| 53 | */ |
||
| 54 | 14 | public function getCache() : BrowscapCacheInterface |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Sets a cache instance |
||
| 71 | * |
||
| 72 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache |
||
| 73 | * @throws \BrowscapPHP\Exception |
||
| 74 | * @return self |
||
| 75 | */ |
||
| 76 | 14 | public function setCache($cache) : self |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Sets a logger instance |
||
| 95 | * |
||
| 96 | * @param \Psr\Log\LoggerInterface $logger |
||
| 97 | * |
||
| 98 | * @return self |
||
| 99 | */ |
||
| 100 | 10 | public function setLogger(LoggerInterface $logger) : self |
|
| 106 | |||
| 107 | /** |
||
| 108 | * returns a logger instance |
||
| 109 | * |
||
| 110 | * @return \Psr\Log\LoggerInterface |
||
| 111 | */ |
||
| 112 | 10 | public function getLogger() : LoggerInterface |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Sets the Connection Timeout |
||
| 123 | * |
||
| 124 | * @param int $connectTimeout |
||
| 125 | */ |
||
| 126 | 1 | public function setConnectTimeout(int $connectTimeout) : void |
|
| 130 | |||
| 131 | 10 | public function getClient() : ClientInterface |
|
| 139 | |||
| 140 | 10 | public function setClient(ClientInterface $client) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * reads and parses an ini file and writes the results into the cache |
||
| 147 | * |
||
| 148 | * @param string $iniFile |
||
| 149 | * @throws \BrowscapPHP\Exception |
||
| 150 | */ |
||
| 151 | 3 | public function convertFile(string $iniFile) : void |
|
| 169 | |||
| 170 | /** |
||
| 171 | * reads and parses an ini string and writes the results into the cache |
||
| 172 | * |
||
| 173 | * @param string $iniString |
||
| 174 | */ |
||
| 175 | 2 | public function convertString(string $iniString) : void |
|
| 182 | |||
| 183 | /** |
||
| 184 | * fetches a remote file and stores it into a local folder |
||
| 185 | * |
||
| 186 | * @param string $file The name of the file where to store the remote content |
||
| 187 | * @param string $remoteFile The code for the remote file to load |
||
| 188 | * |
||
| 189 | * @throws \BrowscapPHP\Exception\FetcherException |
||
| 190 | * @throws \BrowscapPHP\Helper\Exception |
||
| 191 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 192 | */ |
||
| 193 | 3 | public function fetch(string $file, string $remoteFile = IniLoader::PHP_INI) : void |
|
| 244 | |||
| 245 | /** |
||
| 246 | * fetches a remote file, parses it and writes the result into the cache |
||
| 247 | * |
||
| 248 | * if the local stored information are in the same version as the remote data no actions are |
||
| 249 | * taken |
||
| 250 | * |
||
| 251 | * @param string $remoteFile The code for the remote file to load |
||
| 252 | * |
||
| 253 | * @throws \BrowscapPHP\Exception\FileNotFoundException |
||
| 254 | * @throws \BrowscapPHP\Helper\Exception |
||
| 255 | * @throws \BrowscapPHP\Exception\FetcherException |
||
| 256 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 257 | */ |
||
| 258 | 2 | public function update(string $remoteFile = IniLoader::PHP_INI) : void |
|
| 301 | |||
| 302 | /** |
||
| 303 | * checks if an update on a remote location for the local file or the cache |
||
| 304 | * |
||
| 305 | * @throws \BrowscapPHP\Helper\Exception |
||
| 306 | * @throws \BrowscapPHP\Exception\FetcherException |
||
| 307 | * @return int|null The actual cached version if a newer version is available, null otherwise |
||
| 308 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 309 | * @throws \BrowscapPHP\Exception\NoCachedVersionException |
||
| 310 | */ |
||
| 311 | 9 | public function checkUpdate() : ?int |
|
| 364 | |||
| 365 | 5 | private function sanitizeContent(string $content) : string |
|
| 373 | |||
| 374 | /** |
||
| 375 | * reads and parses an ini string and writes the results into the cache |
||
| 376 | * |
||
| 377 | * @param \BrowscapPHP\Helper\Converter $converter |
||
| 378 | * @param string $content |
||
| 379 | * @param int|null $cachedVersion |
||
| 380 | */ |
||
| 381 | 3 | private function storeContent(Converter $converter, string $content, ?int $cachedVersion) |
|
| 392 | } |
||
| 393 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: