Complex classes like Browscap 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 Browscap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 63 | class Browscap |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * Parser to use |
||
| 67 | * |
||
| 68 | * @var \BrowscapPHP\Parser\ParserInterface |
||
| 69 | */ |
||
| 70 | private $parser = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Formatter to use |
||
| 74 | * |
||
| 75 | * @var \BrowscapPHP\Formatter\FormatterInterface |
||
| 76 | */ |
||
| 77 | private $formatter = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The cache instance |
||
| 81 | * |
||
| 82 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface |
||
| 83 | */ |
||
| 84 | private $cache = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var @var \Psr\Log\LoggerInterface |
||
| 88 | */ |
||
| 89 | private $logger = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Options for the updater. The array should be overwritten, |
||
| 93 | * containing all options as keys, set to the default value. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private $options = array(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \BrowscapPHP\Helper\IniLoader |
||
| 101 | */ |
||
| 102 | private $loader = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Set theformatter instance to use for the getBrowser() result |
||
| 106 | * |
||
| 107 | * @param \BrowscapPHP\Formatter\FormatterInterface $formatter |
||
| 108 | * |
||
| 109 | * @return \BrowscapPHP\Browscap |
||
| 110 | */ |
||
| 111 | 6 | public function setFormatter(Formatter\FormatterInterface $formatter) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return \BrowscapPHP\Formatter\FormatterInterface |
||
| 120 | */ |
||
| 121 | 4 | public function getFormatter() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Gets a cache instance |
||
| 132 | * |
||
| 133 | * @return \BrowscapPHP\Cache\BrowscapCacheInterface |
||
| 134 | */ |
||
| 135 | 16 | public function getCache() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Sets a cache instance |
||
| 152 | * |
||
| 153 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache |
||
| 154 | * |
||
| 155 | * @throws \BrowscapPHP\Exception |
||
| 156 | * @return \BrowscapPHP\Browscap |
||
| 157 | */ |
||
| 158 | 14 | public function setCache($cache) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Sets the parser instance to use |
||
| 177 | * |
||
| 178 | * @param \BrowscapPHP\Parser\ParserInterface $parser |
||
| 179 | * |
||
| 180 | * @return \BrowscapPHP\Browscap |
||
| 181 | */ |
||
| 182 | 4 | public function setParser(ParserInterface $parser) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * returns an instance of the used parser class |
||
| 191 | * |
||
| 192 | * @return \BrowscapPHP\Parser\ParserInterface |
||
| 193 | */ |
||
| 194 | 6 | public function getParser() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Sets a logger instance |
||
| 212 | * |
||
| 213 | * @param \Psr\Log\LoggerInterface $logger |
||
| 214 | * |
||
| 215 | * @return \BrowscapPHP\Browscap |
||
| 216 | */ |
||
| 217 | 10 | public function setLogger(LoggerInterface $logger) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * returns a logger instance |
||
| 226 | * |
||
| 227 | * @return \Psr\Log\LoggerInterface |
||
| 228 | */ |
||
| 229 | 15 | public function getLogger() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Sets multiple loader options at once |
||
| 240 | * |
||
| 241 | * @param array $options |
||
| 242 | * |
||
| 243 | * @return \BrowscapPHP\Browscap |
||
| 244 | */ |
||
| 245 | 1 | public function setOptions(array $options) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @return \BrowscapPHP\Helper\IniLoader |
||
| 254 | */ |
||
| 255 | 6 | public function getLoader() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param \BrowscapPHP\Helper\IniLoader $loader |
||
| 266 | */ |
||
| 267 | 7 | public function setLoader(IniLoader $loader) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * parses the given user agent to get the information about the browser |
||
| 274 | * |
||
| 275 | * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it |
||
| 276 | * |
||
| 277 | * @param string $userAgent the user agent string |
||
| 278 | * |
||
| 279 | * @throws \BrowscapPHP\Exception |
||
| 280 | * @return \stdClass the object containing the browsers details. Array if |
||
| 281 | * $return_array is set to true. |
||
| 282 | */ |
||
| 283 | 4 | public function getBrowser($userAgent = null) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * reads and parses an ini file and writes the results into the cache |
||
| 305 | * |
||
| 306 | * @param string $iniFile |
||
| 307 | * |
||
| 308 | * @throws \BrowscapPHP\Exception |
||
| 309 | */ |
||
| 310 | 4 | public function convertFile($iniFile) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * reads and parses an ini string and writes the results into the cache |
||
| 331 | * |
||
| 332 | * @param string $iniString |
||
| 333 | */ |
||
| 334 | 1 | public function convertString($iniString) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * fetches a remote file and stores it into a local folder |
||
| 342 | * |
||
| 343 | * @param string $file The name of the file where to store the remote content |
||
| 344 | * @param string $remoteFile The code for the remote file to load |
||
| 345 | * |
||
| 346 | * @throws \BrowscapPHP\Exception\FetcherException |
||
| 347 | * @throws \BrowscapPHP\Helper\Exception |
||
| 348 | */ |
||
| 349 | 3 | public function fetch($file, $remoteFile = IniLoader::PHP_INI) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * fetches a remote file, parses it and writes the result into the cache |
||
| 393 | * |
||
| 394 | * if the local stored information are in the same version as the remote data no actions are |
||
| 395 | * taken |
||
| 396 | * |
||
| 397 | * @param string $remoteFile The code for the remote file to load |
||
| 398 | * @param string|null $buildFolder |
||
| 399 | * @param int|null $buildNumber |
||
| 400 | * |
||
| 401 | * @throws \BrowscapPHP\Exception\FileNotFoundException |
||
| 402 | * @throws \BrowscapPHP\Helper\Exception |
||
| 403 | * @throws \BrowscapPHP\Exception\FetcherException |
||
| 404 | */ |
||
| 405 | 1 | public function update($remoteFile = IniLoader::PHP_INI, $buildFolder = null, $buildNumber = null) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * checks if an update on a remote location for the local file or the cache |
||
| 488 | * |
||
| 489 | * @param string $remoteFile |
||
| 490 | * |
||
| 491 | * @return int|null The actual cached version if a newer version is available, null otherwise |
||
| 492 | * @throws \BrowscapPHP\Helper\Exception |
||
| 493 | * @throws \BrowscapPHP\Exception\FetcherException |
||
| 494 | */ |
||
| 495 | 7 | public function checkUpdate($remoteFile = IniLoader::PHP_INI) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * @param string $content |
||
| 542 | * |
||
| 543 | * @return mixed |
||
| 544 | */ |
||
| 545 | 2 | private function sanitizeContent($content) |
|
| 553 | } |
||
| 554 |