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
06:21
created

PiwikDeviceDetector   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 300
Duplicated Lines 11.33 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 99.11%

Importance

Changes 13
Bugs 0 Features 0
Metric Value
wmc 42
c 13
b 0
f 0
lcom 1
cbo 11
dl 34
loc 300
ccs 111
cts 112
cp 0.9911
rs 8.2951

13 Methods

Rating   Name   Duplication   Size   Complexity  
B hydrateDevice() 0 22 6
A __construct() 3 6 2
A getName() 0 4 1
A getComposerPackageName() 0 4 1
A setParser() 0 4 1
A getParser() 0 10 2
A getResultRaw() 0 50 1
C hasResult() 0 22 7
B hydrateBot() 11 11 5
B hydrateBrowser() 10 10 5
A hydrateRenderingEngine() 0 6 3
B hydrateOperatingSystem() 10 10 5
B parse() 0 39 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PiwikDeviceDetector 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 PiwikDeviceDetector, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace UserAgentParser\Provider;
3
4
use DeviceDetector\DeviceDetector;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class PiwikDeviceDetector extends AbstractProvider
9
{
10
    protected $detectionCapabilities = [
11
12
        'browser' => [
13
            'name'    => true,
14
            'version' => true,
15
        ],
16
17
        'renderingEngine' => [
18
            'name'    => true,
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'     => true,
31
            'isMobile' => true,
32
            'isTouch'  => true,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => true,
39
        ],
40
    ];
41
42
    protected $defaultValues = [
43
        DeviceDetector::UNKNOWN,
44
    ];
45
46
    /**
47
     *
48
     * @var DeviceDetector
49
     */
50
    private $parser;
51
52 10
    public function __construct()
53
    {
54 10 View Code Duplication
        if (! class_exists('DeviceDetector\Cache\StaticCache', 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...
55
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
56
        }
57 10
    }
58
59 1
    public function getName()
60
    {
61 1
        return 'PiwikDeviceDetector';
62
    }
63
64 2
    public function getComposerPackageName()
65
    {
66 2
        return 'piwik/device-detector';
67
    }
68
69
    /**
70
     *
71
     * @param DeviceDetector $parser
72
     */
73 6
    public function setParser(DeviceDetector $parser = null)
74
    {
75 6
        $this->parser = $parser;
76 6
    }
77
78
    /**
79
     *
80
     * @return DeviceDetector
81
     */
82 6
    public function getParser()
83
    {
84 6
        if ($this->parser !== null) {
85 6
            return $this->parser;
86
        }
87
88 1
        $this->parser = new DeviceDetector();
89
90 1
        return $this->parser;
91
    }
92
93
    /**
94
     *
95
     * @param DeviceDetector $dd
96
     *
97
     * @return array
98
     */
99 4
    private function getResultRaw(DeviceDetector $dd)
100
    {
101
        $raw = [
102 4
            'client'          => $dd->getClient(),
103 4
            'operatingSystem' => $dd->getOs(),
104
105
            'device' => [
106 4
                'brand'     => $dd->getBrand(),
107 4
                'brandName' => $dd->getBrandName(),
108
109 4
                'model' => $dd->getModel(),
110
111 4
                'device'     => $dd->getDevice(),
112 4
                'deviceName' => $dd->getDeviceName(),
113 4
            ],
114
115 4
            'bot' => $dd->getBot(),
116
117
            'extra' => [
118 4
                'isBot' => $dd->isBot(),
119
120
                // client
121 4
                'isBrowser'     => $dd->isBrowser(),
122 4
                'isFeedReader'  => $dd->isFeedReader(),
123 4
                'isMobileApp'   => $dd->isMobileApp(),
124 4
                'isPIM'         => $dd->isPIM(),
125 4
                'isLibrary'     => $dd->isLibrary(),
126 4
                'isMediaPlayer' => $dd->isMediaPlayer(),
127
128
                // deviceType
129 4
                'isCamera'              => $dd->isCamera(),
130 4
                'isCarBrowser'          => $dd->isCarBrowser(),
131 4
                'isConsole'             => $dd->isConsole(),
132 4
                'isFeaturePhone'        => $dd->isFeaturePhone(),
133 4
                'isPhablet'             => $dd->isPhablet(),
134 4
                'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(),
135 4
                'isSmartDisplay'        => $dd->isSmartDisplay(),
136 4
                'isSmartphone'          => $dd->isSmartphone(),
137 4
                'isTablet'              => $dd->isTablet(),
138 4
                'isTV'                  => $dd->isTV(),
139
140
                // other special
141 4
                'isDesktop'      => $dd->isDesktop(),
142 4
                'isMobile'       => $dd->isMobile(),
143 4
                'isTouchEnabled' => $dd->isTouchEnabled(),
144 4
            ],
145 4
        ];
146
147 4
        return $raw;
148
    }
149
150
    /**
151
     *
152
     * @param DeviceDetector $dd
153
     *
154
     * @return bool
155
     */
156 5
    private function hasResult(DeviceDetector $dd)
157
    {
158 5
        if ($dd->isBot() === true) {
159 1
            return true;
160
        }
161
162 4
        $client = $dd->getClient();
163 4
        if (isset($client['name']) && $this->isRealResult($client['name'])) {
164 1
            return true;
165
        }
166
167 3
        $os = $dd->getOs();
168 3
        if (isset($os['name']) && $this->isRealResult($os['name'])) {
169 1
            return true;
170
        }
171
172 2
        if ($dd->getDevice() !== null) {
173 1
            return true;
174
        }
175
176 1
        return false;
177
    }
178
179
    /**
180
     *
181
     * @param Model\Bot     $bot
182
     * @param array|boolean $botRaw
183
     */
184 1 View Code Duplication
    private function hydrateBot(Model\Bot $bot, $botRaw)
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...
185
    {
186 1
        $bot->setIsBot(true);
187
188 1
        if (isset($botRaw['name']) && $this->isRealResult($botRaw['name'])) {
189 1
            $bot->setName($botRaw['name']);
190
        }
191 1
        if (isset($botRaw['category']) && $this->isRealResult($botRaw['category'])) {
192 1
            $bot->setType($botRaw['category']);
193
        }
194 1
    }
195
196
    /**
197
     *
198
     * @param Model\Browser $browser
199
     * @param array|string  $clientRaw
200
     */
201 3 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, $clientRaw)
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...
202
    {
203 3
        if (isset($clientRaw['name']) && $this->isRealResult($clientRaw['name']) === true) {
204 1
            $browser->setName($clientRaw['name']);
205
        }
206
207 3
        if (isset($clientRaw['version']) && $this->isRealResult($clientRaw['version']) === true) {
208 1
            $browser->getVersion()->setComplete($clientRaw['version']);
209
        }
210 3
    }
211
212
    /**
213
     *
214
     * @param Model\RenderingEngine $engine
215
     * @param array|string          $clientRaw
216
     */
217 3
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw)
218
    {
219 3
        if (isset($clientRaw['engine']) && $this->isRealResult($clientRaw['engine']) === true) {
220 1
            $engine->setName($clientRaw['engine']);
221
        }
222 3
    }
223
224
    /**
225
     *
226
     * @param Model\OperatingSystem $os
227
     * @param array|string          $osRaw
228
     */
229 3 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $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...
230
    {
231 3
        if (isset($osRaw['name']) && $this->isRealResult($osRaw['name']) === true) {
232 1
            $os->setName($osRaw['name']);
233
        }
234
235 3
        if (isset($osRaw['version']) && $this->isRealResult($osRaw['version']) === true) {
236 1
            $os->getVersion()->setComplete($osRaw['version']);
237
        }
238 3
    }
239
240
    /**
241
     *
242
     * @param Model\UserAgent $device
243
     * @param DeviceDetector  $dd
244
     */
245 3
    private function hydrateDevice(Model\Device $device, DeviceDetector $dd)
246
    {
247 3
        if ($this->isRealResult($dd->getModel()) === true) {
248 1
            $device->setModel($dd->getModel());
249
        }
250
251 3
        if ($this->isRealResult($dd->getBrandName()) === true) {
252 1
            $device->setBrand($dd->getBrandName());
253
        }
254
255 3
        if ($this->isRealResult($dd->getDeviceName()) === true) {
256 1
            $device->setType($dd->getDeviceName());
257
        }
258
259 3
        if ($dd->isMobile() === true) {
260 1
            $device->setIsMobile(true);
261
        }
262
263 3
        if ($dd->isTouchEnabled() === true) {
264 1
            $device->setIsTouch(true);
265
        }
266 3
    }
267
268 5
    public function parse($userAgent, array $headers = [])
269
    {
270 5
        $dd = $this->getParser();
271
272 5
        $dd->setUserAgent($userAgent);
273 5
        $dd->parse();
274
275
        /*
276
         * No result found?
277
         */
278 5
        if ($this->hasResult($dd) !== true) {
279 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
280
        }
281
282
        /*
283
         * Hydrate the model
284
         */
285 4
        $result = new Model\UserAgent();
286 4
        $result->setProviderResultRaw($this->getResultRaw($dd));
287
288
        /*
289
         * Bot detection
290
         */
291 4
        if ($dd->isBot() === true) {
292 1
            $this->hydrateBot($result->getBot(), $dd->getBot());
293
294 1
            return $result;
295
        }
296
297
        /*
298
         * hydrate the result
299
         */
300 3
        $this->hydrateBrowser($result->getBrowser(), $dd->getClient());
301 3
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $dd->getClient());
302 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $dd->getOs());
303 3
        $this->hydrateDevice($result->getDevice(), $dd);
304
305 3
        return $result;
306
    }
307
}
308