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
Push — master ( 192bb3...47c5f3 )
by Martin
06:04
created

UAParser::setParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UAParser\Parser;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class UAParser extends AbstractProvider
9
{
10
    protected $detectionCapabilities = [
11
12
        'browser' => [
13
            'name'    => true,
14
            'version' => true,
15
        ],
16
17
        'renderingEngine' => [
18
            'name'    => false,
19
            'version' => false,
20
        ],
21
22
        'operatingSystem' => [
23
            'name'    => true,
24
            'version' => true,
25
        ],
26
27
        'device' => [
28
            'model'    => true,
29
            'brand'    => true,
30
            'type'     => false,
31
            'isMobile' => false,
32
            'isTouch'  => false,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => false,
39
        ],
40
    ];
41
42
    protected $defaultValues = [
43
        'Other',
44
    ];
45
46
    private $parser;
47
48 11 View Code Duplication
    public function __construct(Parser $parser = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
49
    {
50 11
        if (! class_exists('UAParser\Parser', true)) {
51 1
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
52
        }
53
54 10
        $this->parser = $parser;
55 10
    }
56
57 1
    public function getName()
58
    {
59 1
        return 'UAParser';
60
    }
61
62 3
    public function getComposerPackageName()
63
    {
64 3
        return 'ua-parser/uap-php';
65
    }
66
67
    /**
68
     *
69
     * @return Parser
70
     */
71 6
    public function getParser()
72
    {
73 6
        if ($this->parser !== null) {
74 6
            return $this->parser;
75
        }
76
77 1
        $this->parser = Parser::create();
78
79 1
        return $this->parser;
80
    }
81
82
    /**
83
     *
84
     * @param \UAParser\Result\Client $resultRaw
85
     *
86
     * @return bool
87
     */
88 5
    private function hasResult(\UAParser\Result\Client $resultRaw)
89
    {
90 5
        if ($this->isRealResult($resultRaw->ua->family)) {
91 2
            return true;
92
        }
93
94 3
        if ($this->isRealResult($resultRaw->os->family)) {
95 1
            return true;
96
        }
97
98 2
        if ($this->isRealResult($resultRaw->device->model)) {
99 1
            return true;
100
        }
101
102 1
        return false;
103
    }
104
105 3
    private function getDeviceModelDefaultValues()
106
    {
107
        return [
108 3
            'Feature Phone',
109 3
            'iOS-Device',
110 3
            'Smartphone',
111 3
        ];
112
    }
113
114 3
    private function getDeviceBrandDefaultValues()
115
    {
116
        return [
117 3
            'Generic',
118 3
            'Generic_Android',
119 3
            'Generic_Inettv',
120 3
        ];
121
    }
122
123
    /**
124
     *
125
     * @param \UAParser\Result\Client $resultRaw
126
     *
127
     * @return bool
128
     */
129 4
    private function isBot(\UAParser\Result\Client $resultRaw)
130
    {
131 4
        if ($resultRaw->device->family === 'Spider') {
132 1
            return true;
133
        }
134
135 3
        return false;
136
    }
137
138
    /**
139
     *
140
     * @param Model\Bot               $bot
141
     * @param \UAParser\Result\Client $resultRaw
142
     */
143 1
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
144
    {
145 1
        $bot->setIsBot(true);
146
147 1
        if ($this->isRealResult($resultRaw->ua->family) === true) {
148 1
            $bot->setName($resultRaw->ua->family);
149
        }
150 1
    }
151
152
    /**
153
     *
154
     * @param Model\Browser              $browser
155
     * @param \UAParser\Result\UserAgent $uaRaw
156
     */
157 3 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
158
    {
159 3
        if ($this->isRealResult($uaRaw->family) === true) {
160 1
            $browser->setName($uaRaw->family);
161
        }
162
163 3
        if ($this->isRealResult($uaRaw->major) === true) {
164 1
            $browser->getVersion()->setMajor($uaRaw->major);
165
        }
166
167 3
        if ($this->isRealResult($uaRaw->minor) === true) {
168 1
            $browser->getVersion()->setMinor($uaRaw->minor);
169
        }
170
171 3
        if ($this->isRealResult($uaRaw->patch) === true) {
172 1
            $browser->getVersion()->setPatch($uaRaw->patch);
173
        }
174 3
    }
175
176
    /**
177
     *
178
     * @param Model\OperatingSystem            $os
179
     * @param \UAParser\Result\OperatingSystem $osRaw
180
     */
181 3 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
182
    {
183 3
        if ($this->isRealResult($osRaw->family) === true) {
184 1
            $os->setName($osRaw->family);
185
        }
186
187 3
        if ($this->isRealResult($osRaw->major) === true) {
188 1
            $os->getVersion()->setMajor($osRaw->major);
189
        }
190
191 3
        if ($this->isRealResult($osRaw->minor) === true) {
192 1
            $os->getVersion()->setMinor($osRaw->minor);
193
        }
194
195 3
        if ($this->isRealResult($osRaw->patch) === true) {
196 1
            $os->getVersion()->setPatch($osRaw->patch);
197
        }
198 3
    }
199
200
    /**
201
     *
202
     * @param Model\UserAgent         $device
203
     * @param \UAParser\Result\Device $deviceRaw
204
     */
205 3
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw)
206
    {
207 3
        if ($this->isRealResult($deviceRaw->model, $this->getDeviceModelDefaultValues()) === true) {
208 1
            $device->setModel($deviceRaw->model);
209
        }
210
211 3
        if ($this->isRealResult($deviceRaw->brand, $this->getDeviceBrandDefaultValues()) === true) {
212 1
            $device->setBrand($deviceRaw->brand);
213
        }
214 3
    }
215
216 5
    public function parse($userAgent, array $headers = [])
217
    {
218 5
        $parser = $this->getParser();
219
220
        /* @var $resultRaw \UAParser\Result\Client */
221 5
        $resultRaw = $parser->parse($userAgent);
222
223
        /*
224
         * No result found?
225
         */
226 5
        if ($this->hasResult($resultRaw) !== true) {
227 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
228
        }
229
230
        /*
231
         * Hydrate the model
232
         */
233 4
        $result = new Model\UserAgent();
234 4
        $result->setProviderResultRaw($resultRaw);
235
236
        /*
237
         * Bot detection
238
         */
239 4
        if ($this->isBot($resultRaw) === true) {
240 1
            $this->hydrateBot($result->getBot(), $resultRaw);
241
242 1
            return $result;
243
        }
244
245
        /*
246
         * hydrate the result
247
         */
248 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->ua);
249
        // renderingEngine not available
250 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os);
251 3
        $this->hydrateDevice($result->getDevice(), $resultRaw->device);
252
253 3
        return $result;
254
    }
255
}
256