Complex classes like AbstractBrowscap 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 AbstractBrowscap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class AbstractBrowscap extends AbstractProvider |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Homepage of the provider |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $homepage = 'https://github.com/browscap/browscap-php'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Composer package name |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $packageName = 'browscap/browscap-php'; |
||
| 26 | |||
| 27 | protected $defaultValues = [ |
||
| 28 | |||
| 29 | 'general' => [ |
||
| 30 | '/^unknown$/i', |
||
| 31 | ], |
||
| 32 | |||
| 33 | 'browser' => [ |
||
| 34 | 'name' => [ |
||
| 35 | '/^Default Browser$/i', |
||
| 36 | ], |
||
| 37 | ], |
||
| 38 | |||
| 39 | 'device' => [ |
||
| 40 | 'model' => [ |
||
| 41 | '/^general/i', |
||
| 42 | '/desktop$/i', |
||
| 43 | ], |
||
| 44 | ], |
||
| 45 | |||
| 46 | 'bot' => [ |
||
| 47 | 'name' => [ |
||
| 48 | '/^General Crawlers/i', |
||
| 49 | ], |
||
| 50 | ], |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * @var Browscap |
||
| 56 | */ |
||
| 57 | private $parser; |
||
| 58 | |||
| 59 | 27 | public function __construct(Browscap $parser, $expectedType = '') |
|
| 71 | |||
| 72 | 1 | public function getVersion() |
|
| 78 | |||
| 79 | 1 | public function getUpdateDate() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @return Browscap |
||
| 91 | */ |
||
| 92 | 15 | public function getParser() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * |
||
| 99 | * @param stdClass $resultRaw |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | 12 | private function hasResult(stdClass $resultRaw) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * |
||
| 114 | * @param stdClass $resultRaw |
||
| 115 | * @return boolean |
||
| 116 | */ |
||
| 117 | 8 | private function isBot(stdClass $resultRaw) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * |
||
| 128 | * @param Model\Bot $bot |
||
| 129 | * @param stdClass $resultRaw |
||
| 130 | */ |
||
| 131 | 4 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * |
||
| 148 | * @param Model\Browser $browser |
||
| 149 | * @param stdClass $resultRaw |
||
| 150 | */ |
||
| 151 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * |
||
| 164 | * @param Model\RenderingEngine $engine |
||
| 165 | * @param stdClass $resultRaw |
||
| 166 | */ |
||
| 167 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * |
||
| 180 | * @param Model\OperatingSystem $os |
||
| 181 | * @param stdClass $resultRaw |
||
| 182 | */ |
||
| 183 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * |
||
| 196 | * @param Model\UserAgent $device |
||
| 197 | * @param stdClass $resultRaw |
||
| 198 | */ |
||
| 199 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
| 222 | |||
| 223 | 12 | public function parse($userAgent, array $headers = []) |
|
| 262 | } |
||
| 263 |