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 |
||
18 | abstract class AbstractBrowscap extends AbstractProvider |
||
19 | { |
||
20 | /** |
||
21 | * Homepage of the provider |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $homepage = 'https://github.com/browscap/browscap-php'; |
||
26 | |||
27 | /** |
||
28 | * Composer package name |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $packageName = 'browscap/browscap-php'; |
||
33 | |||
34 | protected $defaultValues = [ |
||
35 | |||
36 | 'general' => [ |
||
37 | '/^unknown$/i', |
||
38 | ], |
||
39 | |||
40 | 'browser' => [ |
||
41 | 'name' => [ |
||
42 | '/^Default Browser$/i', |
||
43 | ], |
||
44 | ], |
||
45 | |||
46 | 'device' => [ |
||
47 | 'model' => [ |
||
48 | '/^general/i', |
||
49 | '/desktop$/i', |
||
50 | ], |
||
51 | ], |
||
52 | |||
53 | 'bot' => [ |
||
54 | 'name' => [ |
||
55 | '/^General Crawlers/i', |
||
56 | '/^Generic/i', |
||
57 | ], |
||
58 | ], |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * |
||
63 | * @var Browscap |
||
64 | */ |
||
65 | private $parser; |
||
66 | |||
67 | 22 | public function __construct(Browscap $parser, $expectedType = '') |
|
68 | { |
||
69 | 22 | $this->parser = $parser; |
|
70 | |||
71 | 22 | if ($parser->getCache()->getType() === null) { |
|
72 | 1 | throw new InvalidArgumentException('You need to warm-up the cache first to use this provider'); |
|
73 | } |
||
74 | |||
75 | 21 | if ($expectedType !== $parser->getCache()->getType()) { |
|
76 | 1 | throw new InvalidArgumentException('Expected the "' . $expectedType . '" data file. Instead got the "' . $parser->getCache()->getType() . '" data file'); |
|
77 | } |
||
78 | 20 | } |
|
79 | |||
80 | 1 | public function getVersion() |
|
86 | |||
87 | 1 | public function getUpdateDate() |
|
95 | |||
96 | /** |
||
97 | * |
||
98 | * @return Browscap |
||
99 | */ |
||
100 | 15 | public function getParser() |
|
104 | |||
105 | /** |
||
106 | * |
||
107 | * @param stdClass $resultRaw |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | 12 | private function hasResult(stdClass $resultRaw) |
|
119 | |||
120 | /** |
||
121 | * |
||
122 | * @param stdClass $resultRaw |
||
123 | * @return boolean |
||
124 | */ |
||
125 | 8 | private function isBot(stdClass $resultRaw) |
|
133 | |||
134 | /** |
||
135 | * |
||
136 | * @param Model\Bot $bot |
||
137 | * @param stdClass $resultRaw |
||
138 | */ |
||
139 | 4 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
153 | |||
154 | /** |
||
155 | * |
||
156 | * @param Model\Browser $browser |
||
157 | * @param stdClass $resultRaw |
||
158 | */ |
||
159 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
169 | |||
170 | /** |
||
171 | * |
||
172 | * @param Model\RenderingEngine $engine |
||
173 | * @param stdClass $resultRaw |
||
174 | */ |
||
175 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
185 | |||
186 | /** |
||
187 | * |
||
188 | * @param Model\OperatingSystem $os |
||
189 | * @param stdClass $resultRaw |
||
190 | */ |
||
191 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
201 | |||
202 | /** |
||
203 | * |
||
204 | * @param Model\UserAgent $device |
||
205 | * @param stdClass $resultRaw |
||
206 | */ |
||
207 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
230 | |||
231 | 12 | public function parse($userAgent, array $headers = []) |
|
270 | } |
||
271 |