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 (#42)
by Martin
13:41
created

WhichBrowser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 3
Ratio 50 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception;
5
use UserAgentParser\Model;
6
use WhichBrowser\Parser as WhichBrowserParser;
7
8
class WhichBrowser extends AbstractProvider
9
{
10
    protected $detectionCapabilities = [
11
12
        'browser' => [
13
            'name'    => true,
14
            'version' => true,
15
        ],
16
17
        'renderingEngine' => [
18
            'name'    => true,
19
            'version' => true,
20
        ],
21
22
        'operatingSystem' => [
23
            'name'    => true,
24
            'version' => true,
25
        ],
26
27
        'device' => [
28
            'model'    => true,
29
            'brand'    => true,
30
            'type'     => true,
31
            'isMobile' => true,
32
            'isTouch'  => false,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => false,
39
        ],
40
    ];
41
42
    /**
43
     * Used for unitTests mocking
44
     *
45
     * @var WhichBrowserParser
46
     */
47
    private $parser;
48
49 1
    public function __construct()
50
    {
51 1 View Code Duplication
        if (! class_exists('WhichBrowser\Parser', 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...
52
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
53
        }
54 2
    }
55
56 2
    public function getName()
57
    {
58
        return 'WhichBrowser';
59
    }
60
61
    public function getComposerPackageName()
62
    {
63
        return 'whichbrowser/parser';
64 8
    }
65
66 8
    /**
67 7
     *
68
     * @param  array              $headers
69
     * @return WhichBrowserParser
70 1
     */
71
    public function getParser(array $headers)
72
    {
73
        if ($this->parser !== null) {
74
            return $this->parser;
75
        }
76
77
        return new WhichBrowserParser($headers);
78 1
    }
79
80 1
    /**
81
     *
82 1
     * @param Model\Bot                   $bot
83 1
     * @param \WhichBrowser\Model\Browser $browserRaw
84
     */
85 1
    private function hydrateBot(Model\Bot $bot, \WhichBrowser\Model\Browser $browserRaw)
86
    {
87
        $bot->setIsBot(true);
88
89
        if ($this->isRealResult($browserRaw->getName()) === true) {
90
            $bot->setName($browserRaw->getName());
91
        }
92 5
    }
93
94 5
    /**
95 1
     *
96
     * @param Model\Browser               $browser
97 1
     * @param \WhichBrowser\Model\Browser $browserRaw
98 1
     */
99
    private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Model\Browser $browserRaw)
100
    {
101 1 View Code Duplication
        if ($this->isRealResult($browserRaw->getName()) === 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...
102
            $browser->setName($browserRaw->getName());
103
104 4
            if ($this->isRealResult($browserRaw->getVersion()) === true) {
105
                $browser->getVersion()->setComplete($browserRaw->getVersion());
106 1
            }
107
108 1
            return;
109 1
        }
110
111 1
        if (isset($browserRaw->using) && $browserRaw->using instanceof \WhichBrowser\Model\Using) {
0 ignored issues
show
Bug introduced by
The class WhichBrowser\Model\Using does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
112 1
            /* @var $usingRaw \WhichBrowser\Model\Using */
113
            $usingRaw = $browserRaw->using;
114
115 View Code Duplication
            if ($this->isRealResult($usingRaw->getName()) === 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...
116 4
                $browser->setName($usingRaw->getName());
117
118
                if ($this->isRealResult($usingRaw->getVersion()) === true) {
119
                    $browser->getVersion()->setComplete($usingRaw->getVersion());
120
                }
121
            }
122
        }
123 5
    }
124
125 5
    /**
126 1
     *
127
     * @param Model\RenderingEngine      $engine
128
     * @param \WhichBrowser\Model\Engine $engineRaw
129 5
     */
130 1 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, \WhichBrowser\Model\Engine $engineRaw)
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...
131
    {
132 5
        if ($this->isRealResult($engineRaw->getName()) === true) {
133
            $engine->setName($engineRaw->getName());
134
        }
135
136
        if ($this->isRealResult($engineRaw->getVersion()) === true) {
137
            $engine->getVersion()->setComplete($engineRaw->getVersion());
138
        }
139 5
    }
140
141 5
    /**
142 1
     *
143
     * @param Model\OperatingSystem  $os
144
     * @param \WhichBrowser\Model\Os $osRaw
145 5
     */
146 1 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \WhichBrowser\Model\Os $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...
147
    {
148 5
        if ($this->isRealResult($osRaw->getName()) === true) {
149
            $os->setName($osRaw->getName());
150
        }
151
152
        if ($this->isRealResult($osRaw->getVersion()) === true) {
153
            $os->getVersion()->setComplete($osRaw->getVersion());
154
        }
155
    }
156 5
157
    /**
158 5
     *
159 1
     * @param Model\Device               $device
160
     * @param \WhichBrowser\Model\Device $deviceRaw
161
     * @param WhichBrowserParser         $parser
162 5
     */
163 1
    private function hydrateDevice(Model\Device $device, \WhichBrowser\Model\Device $deviceRaw, WhichBrowserParser $parser)
164
    {
165
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
166 5
            $device->setModel($deviceRaw->getModel());
167 1
        }
168
169
        if ($this->isRealResult($deviceRaw->getManufacturer()) === true) {
170 5
            $device->setBrand($deviceRaw->getManufacturer());
171 1
        }
172
173 5
        if ($this->isRealResult($parser->getType()) === true) {
174
            $device->setType($parser->getType());
175 7
        }
176
177 7
        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
178
            $device->setIsMobile(true);
179 7
        }
180
    }
181
182
    public function parse($userAgent, array $headers = [])
183
    {
184 7
        $headers['User-Agent'] = $userAgent;
185 1
186
        $parser = $this->getParser($headers);
187
188
        /*
189
         * No result found?
190
         */
191 6
        if ($parser->isDetected() !== true) {
192 6
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
193
        }
194
195
        /*
196
         * Hydrate the model
197 6
         */
198 1
        $result = new Model\UserAgent();
199
        $result->setProviderResultRaw($parser->toArray());
200 1
201
        /*
202
         * Bot detection
203
         */
204
        if ($parser->getType() === 'bot') {
205
            $this->hydrateBot($result->getBot(), $parser->browser);
206 5
207 5
            return $result;
208 5
        }
209 5
210
        /*
211 5
         * hydrate the result
212
         */
213
        $this->hydrateBrowser($result->getBrowser(), $parser->browser);
214
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->engine);
215
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->os);
216
        $this->hydrateDevice($result->getDevice(), $parser->device, $parser);
217
218
        return $result;
219
    }
220
}
221