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 = '') |
|
60 | { |
||
61 | 27 | $this->parser = $parser; |
|
62 | |||
63 | 27 | if ($expectedType !== $parser->getCache()->getType()) { |
|
64 | 1 | throw new InvalidArgumentException('Expected the "' . $expectedType . '" data file. Instead got the "' . $parser->getCache()->getType() . '" data file'); |
|
65 | } |
||
66 | 26 | } |
|
67 | |||
68 | 1 | public function getVersion() |
|
74 | |||
75 | 1 | public function getUpdateDate() |
|
83 | |||
84 | /** |
||
85 | * |
||
86 | * @return Browscap |
||
87 | */ |
||
88 | 15 | public function getParser() |
|
92 | |||
93 | /** |
||
94 | * |
||
95 | * @param stdClass $resultRaw |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 12 | private function hasResult(stdClass $resultRaw) |
|
107 | |||
108 | /** |
||
109 | * |
||
110 | * @param stdClass $resultRaw |
||
111 | * @return boolean |
||
112 | */ |
||
113 | 8 | private function isBot(stdClass $resultRaw) |
|
121 | |||
122 | /** |
||
123 | * |
||
124 | * @param Model\Bot $bot |
||
125 | * @param stdClass $resultRaw |
||
126 | */ |
||
127 | 4 | private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
128 | { |
||
129 | 4 | $bot->setIsBot(true); |
|
130 | |||
131 | 4 | if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'bot', 'name') === true) { |
|
132 | 3 | $bot->setName($resultRaw->browser); |
|
133 | } |
||
134 | |||
135 | 4 | if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) { |
|
136 | 1 | $bot->setType('RSS'); |
|
137 | 3 | } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) { |
|
138 | 2 | $bot->setType($resultRaw->browser_type); |
|
139 | } |
||
140 | 4 | } |
|
141 | |||
142 | /** |
||
143 | * |
||
144 | * @param Model\Browser $browser |
||
145 | * @param stdClass $resultRaw |
||
146 | */ |
||
147 | 4 | private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
157 | |||
158 | /** |
||
159 | * |
||
160 | * @param Model\RenderingEngine $engine |
||
161 | * @param stdClass $resultRaw |
||
162 | */ |
||
163 | 4 | private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
|
173 | |||
174 | /** |
||
175 | * |
||
176 | * @param Model\OperatingSystem $os |
||
177 | * @param stdClass $resultRaw |
||
178 | */ |
||
179 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
|
189 | |||
190 | /** |
||
191 | * |
||
192 | * @param Model\UserAgent $device |
||
193 | * @param stdClass $resultRaw |
||
194 | */ |
||
195 | 4 | private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
|
218 | |||
219 | 12 | public function parse($userAgent, array $headers = []) |
|
258 | } |
||
259 |