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 |
||
| 10 | abstract class AbstractBrowscap extends AbstractProvider |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Homepage of the provider |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $homepage = 'https://github.com/browscap/browscap-php'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Composer package name |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $packageName = 'browscap/browscap-php'; |
||
| 25 | |||
| 26 | protected $defaultValues = [ |
||
| 27 | |||
| 28 | 'general' => [ |
||
| 29 | '/^unknown$/i', |
||
| 30 | ], |
||
| 31 | |||
| 32 | 'browser' => [ |
||
| 33 | 'name' => [ |
||
| 34 | '/^Default Browser$/i', |
||
| 35 | ], |
||
| 36 | ], |
||
| 37 | |||
| 38 | 'device' => [ |
||
| 39 | 'model' => [ |
||
| 40 | '/^general/i', |
||
| 41 | '/desktop$/i', |
||
| 42 | ], |
||
| 43 | ], |
||
| 44 | |||
| 45 | 'bot' => [ |
||
| 46 | 'name' => [ |
||
| 47 | '/^General Crawlers/i', |
||
| 48 | ], |
||
| 49 | ], |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | * @var Browscap |
||
| 55 | */ |
||
| 56 | private $parser; |
||
| 57 | |||
| 58 | 26 | public function __construct(Browscap $parser, $expectedType = '') |
|
| 66 | |||
| 67 | 1 | public function getVersion() |
|
| 73 | |||
| 74 | 1 | public function getUpdateDate() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * |
||
| 85 | * @return Browscap |
||
| 86 | */ |
||
| 87 | 15 | public function getParser() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | * @param stdClass $resultRaw |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | 12 | private function hasResult(stdClass $resultRaw) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * |
||
| 109 | * @param stdClass $resultRaw |
||
| 110 | * @return boolean |
||
| 111 | */ |
||
| 112 | 8 | private function isBot(stdClass $resultRaw) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * |
||
| 123 | * @param Model\Bot $bot |
||
| 124 | * @param stdClass $resultRaw |
||
| 125 | */ |
||
| 126 | 4 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | * @param Model\Browser $browser |
||
| 144 | * @param stdClass $resultRaw |
||
| 145 | */ |
||
| 146 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * |
||
| 159 | * @param Model\RenderingEngine $engine |
||
| 160 | * @param stdClass $resultRaw |
||
| 161 | */ |
||
| 162 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * |
||
| 175 | * @param Model\OperatingSystem $os |
||
| 176 | * @param stdClass $resultRaw |
||
| 177 | */ |
||
| 178 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * |
||
| 191 | * @param Model\UserAgent $device |
||
| 192 | * @param stdClass $resultRaw |
||
| 193 | */ |
||
| 194 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
| 217 | |||
| 218 | 12 | public function parse($userAgent, array $headers = []) |
|
| 257 | } |
||
| 258 |