GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#59)
by Martin
02:55
created

BrowscapPhp::getDeviceModelDefaultValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A BrowscapPhp::getParser() 0 4 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use BrowscapPHP\Browscap;
5
use stdClass;
6
use UserAgentParser\Exception\NoResultFoundException;
7
use UserAgentParser\Model;
8
9
class BrowscapPhp extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'BrowscapPhp';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/browscap/browscap-php';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'browscap/browscap-php';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => true,
41
            'version' => true,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => true,
52
            'type'     => true,
53
            'isMobile' => true,
54
            'isTouch'  => true,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => true,
60
            'type'  => true,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
        
66
        'general' => [
67
            '/^DefaultProperties$/i',
68
            '/^Default Browser$/i',
69
70
            '/^unknown$/i',
71
        ],
72
73
        'device' => [
74
            'model' => [
75
                '/^general/i',
76
                '/desktop/i',
77 17
            ],
78
        ],
79 17
    ];
80 17
81
    /**
82 1
     *
83
     * @var Browscap
84 1
     */
85 1
    private $parser;
86 1
87
    public function __construct(Browscap $parser)
88
    {
89 1
        $this->parser = $parser;
90
    }
91 1
92
    public function getVersion()
93
    {
94
        return $this->getParser()
95
            ->getCache()
96
            ->getVersion();
97
    }
98 12
99
    public function getUpdateDate()
100 12
    {
101
        return;
102
    }
103
104
    /**
105
     *
106
     * @return Browscap
107 1
     */
108
    public function getParser()
109
    {
110 1
        return $this->parser;
111 1
    }
112 1
113 1
    /**
114 1
     *
115
     * @param stdClass $resultRaw
116
     *
117
     * @return bool
118
     */
119
    private function hasResult(stdClass $resultRaw)
120
    {
121
        if (! isset($resultRaw->browser)) {
122
            return false;
123 10
        }
124
125 10
        if ($this->isRealResult($resultRaw->browser) !== true) {
126 1
            return false;
127
        }
128
129 9
        return true;
130 4
    }
131
132
    /**
133 5
     *
134
     * @param  stdClass $resultRaw
135
     * @return boolean
136
     */
137
    private function isBot(stdClass $resultRaw)
138
    {
139
        if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) {
140
            return false;
141 5
        }
142
143 5
        return true;
144 2
    }
145
146
    /**
147 3
     *
148
     * @param Model\Bot $bot
149
     * @param stdClass  $resultRaw
150
     */
151
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
152
    {
153
        $bot->setIsBot(true);
154
155 3
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
156
            $bot->setName($resultRaw->browser);
157 3
        }
158
159 3
        if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) {
160 3
            $bot->setType('RSS');
161 3
        } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) {
162
            $bot->setType($resultRaw->browser_type);
163 3
        }
164 1
    }
165 3
166 2
    /**
167 2
     *
168 3
     * @param Model\Browser $browser
169
     * @param stdClass      $resultRaw
170
     */
171
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
172
    {
173
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
174
            $browser->setName($resultRaw->browser);
175 2
        }
176
177 2
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
178 2
            $browser->getVersion()->setComplete($resultRaw->version);
179 2
        }
180
    }
181 2
182 2
    /**
183 2
     *
184 2
     * @param Model\RenderingEngine $engine
185
     * @param stdClass              $resultRaw
186
     */
187
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
188
    {
189 View Code Duplication
        if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
            $engine->setName($resultRaw->renderingengine_name);
191 2
        }
192
193 2
        if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) {
194 1
            $engine->getVersion()->setComplete($resultRaw->renderingengine_version);
195 1
        }
196
    }
197 2
198 1
    /**
199 1
     *
200 2
     * @param Model\OperatingSystem $os
201
     * @param stdClass              $resultRaw
202
     */
203
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
204
    {
205
        if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) {
206
            $os->setName($resultRaw->platform);
207 2
        }
208
209 2
        if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) {
210 1
            $os->getVersion()->setComplete($resultRaw->platform_version);
211 1
        }
212
    }
213 2
214 1
    /**
215 1
     *
216 2
     * @param Model\UserAgent $device
217
     * @param stdClass        $resultRaw
218
     */
219
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
220
    {
221
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, 'device', 'model') === true) {
222
            $device->setModel($resultRaw->device_name);
223 2
        }
224
225 2 View Code Duplication
        if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226 1
            $device->setBrand($resultRaw->device_brand_name);
227 1
        }
228
229 2 View Code Duplication
        if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230 1
            // @todo convert to a common set of types (over all vendors)
231 1
            $device->setType($resultRaw->device_type);
232
        }
233 2
234
        if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) {
235 1
            $device->setIsMobile(true);
236 1
        }
237
238 2
        if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') {
239 1
            $device->setIsTouch(true);
240 1
        }
241
    }
242 2
243 1
    public function parse($userAgent, array $headers = [])
244 1
    {
245 2
        $parser = $this->getParser();
246
247 10
        /* @var $resultRaw \stdClass */
248
        $resultRaw = $parser->getBrowser($userAgent);
249 10
250
        /*
251
         * No result found?
252 10
         */
253
        if ($this->hasResult($resultRaw) !== true) {
254
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
255
        }
256
257 10
        /*
258 5
         * Hydrate the model
259
         */
260
        $result = new Model\UserAgent();
261
        $result->setProviderResultRaw($resultRaw);
262
263
        /*
264 5
         * Bot detection (does only work with full_php_browscap.ini)
265 5
         */
266
        if ($this->isBot($resultRaw) === true) {
267
            $this->hydrateBot($result->getBot(), $resultRaw);
268
269
            return $result;
270 5
        }
271 3
272
        /*
273 3
         * hydrate the result
274
         */
275
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
276
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
277
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
278
        $this->hydrateDevice($result->getDevice(), $resultRaw);
279 2
280 2
        return $result;
281 2
    }
282
}
283