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 (#44)
by Martin
05:39
created

UAParser::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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