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 ( 0994ac...8bf298 )
by Martin
04:01
created

PiwikDeviceDetector   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 235
Duplicated Lines 2.55 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 35
c 9
b 0
f 0
lcom 1
cbo 10
dl 6
loc 235
ccs 101
cts 101
cp 1
rs 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getComposerPackageName() 0 4 1
A setParser() 0 4 1
A getParser() 0 10 2
C hasResult() 0 22 7
A getResultRaw() 0 50 1
F parse() 6 102 22

How to fix   Duplicated Code   

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:

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 $defaultValues = [
11
        DeviceDetector::UNKNOWN,
12
    ];
13
14
    /**
15
     *
16
     * @var DeviceDetector
17
     */
18
    private $parser;
19
20 1
    public function getName()
21
    {
22 1
        return 'PiwikDeviceDetector';
23
    }
24
25 2
    public function getComposerPackageName()
26
    {
27 2
        return 'piwik/device-detector';
28
    }
29
30
    /**
31
     *
32
     * @param DeviceDetector $parser
33
     */
34 6
    public function setParser(DeviceDetector $parser = null)
35
    {
36 6
        $this->parser = $parser;
37 6
    }
38
39
    /**
40
     *
41
     * @return DeviceDetector
42
     */
43 6
    public function getParser()
44
    {
45 6
        if ($this->parser !== null) {
46 6
            return $this->parser;
47
        }
48
49 1
        $this->parser = new DeviceDetector();
50
51 1
        return $this->parser;
52
    }
53
54
    /**
55
     *
56
     * @param DeviceDetector $dd
57
     *
58
     * @return bool
59
     */
60 5
    private function hasResult(DeviceDetector $dd)
61
    {
62 5
        if ($dd->isBot() === true) {
63 1
            return true;
64
        }
65
66 4
        $client = $dd->getClient();
67 4
        if (isset($client['name']) && $this->isRealResult($client['name'])) {
68 1
            return true;
69
        }
70
71 3
        $os = $dd->getOs();
72 3
        if (isset($os['name']) && $this->isRealResult($os['name'])) {
73 1
            return true;
74
        }
75
76 2
        if ($dd->getDevice() !== null) {
77 1
            return true;
78
        }
79
80 1
        return false;
81
    }
82
83
    /**
84
     *
85
     * @param DeviceDetector $dd
86
     *
87
     * @return array
88
     */
89 4
    private function getResultRaw(DeviceDetector $dd)
90
    {
91
        $raw = [
92 4
            'client'          => $dd->getClient(),
93 4
            'operatingSystem' => $dd->getOs(),
94
95
            'device' => [
96 4
                'brand'     => $dd->getBrand(),
97 4
                'brandName' => $dd->getBrandName(),
98
99 4
                'model' => $dd->getModel(),
100
101 4
                'device'     => $dd->getDevice(),
102 4
                'deviceName' => $dd->getDeviceName(),
103 4
            ],
104
105 4
            'bot' => $dd->getBot(),
106
107
            'extra' => [
108 4
                'isBot' => $dd->isBot(),
109
110
                // client
111 4
                'isBrowser'     => $dd->isBrowser(),
112 4
                'isFeedReader'  => $dd->isFeedReader(),
113 4
                'isMobileApp'   => $dd->isMobileApp(),
114 4
                'isPIM'         => $dd->isPIM(),
115 4
                'isLibrary'     => $dd->isLibrary(),
116 4
                'isMediaPlayer' => $dd->isMediaPlayer(),
117
118
                // deviceType
119 4
                'isCamera'              => $dd->isCamera(),
120 4
                'isCarBrowser'          => $dd->isCarBrowser(),
121 4
                'isConsole'             => $dd->isConsole(),
122 4
                'isFeaturePhone'        => $dd->isFeaturePhone(),
123 4
                'isPhablet'             => $dd->isPhablet(),
124 4
                'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(),
125 4
                'isSmartDisplay'        => $dd->isSmartDisplay(),
126 4
                'isSmartphone'          => $dd->isSmartphone(),
127 4
                'isTablet'              => $dd->isTablet(),
128 4
                'isTV'                  => $dd->isTV(),
129
130
                // other special
131 4
                'isDesktop'      => $dd->isDesktop(),
132 4
                'isMobile'       => $dd->isMobile(),
133 4
                'isTouchEnabled' => $dd->isTouchEnabled(),
134 4
            ],
135 4
        ];
136
137 4
        return $raw;
138
    }
139
140 5
    public function parse($userAgent, array $headers = [])
141
    {
142 5
        $dd = $this->getParser();
143
144 5
        $dd->setUserAgent($userAgent);
145 5
        $dd->parse();
146
147
        /*
148
         * No result found?
149
         */
150 5
        if ($this->hasResult($dd) !== true) {
151 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
152
        }
153
154
        /*
155
         * Hydrate the model
156
         */
157 4
        $result = new Model\UserAgent();
158 4
        $result->setProviderResultRaw($this->getResultRaw($dd));
159
160
        /*
161
         * Bot detection
162
         */
163 4
        if ($dd->isBot() === true) {
164 1
            $bot = $result->getBot();
165 1
            $bot->setIsBot(true);
166
167 1
            $botRaw = $dd->getBot();
168 1
            if (isset($botRaw['name']) && $this->isRealResult($botRaw['name'])) {
169 1
                $bot->setName($botRaw['name']);
170
            }
171 1
            if (isset($botRaw['category']) && $this->isRealResult($botRaw['category'])) {
172 1
                $bot->setType($botRaw['category']);
173
            }
174
175 1
            return $result;
176
        }
177
178
        /*
179
         * Browser
180
         */
181 3
        $browser = $result->getBrowser();
182
183 3
        $ddClient = $dd->getClient();
184 3
        if (isset($ddClient['name']) && $this->isRealResult($ddClient['name']) === true) {
185 1
            $browser->setName($ddClient['name']);
186
        }
187
188 3 View Code Duplication
        if (isset($ddClient['version']) && $this->isRealResult($ddClient['version']) === 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...
189 1
            $browser->getVersion()->setComplete($ddClient['version']);
190
        }
191
192
        /*
193
         * renderingEngine
194
         */
195 3
        $renderingEngine = $result->getRenderingEngine();
196
197 3
        if (isset($ddClient['engine']) && $this->isRealResult($ddClient['engine']) === true) {
198 1
            $renderingEngine->setName($ddClient['engine']);
199
        }
200
201
        /*
202
         * operatingSystem
203
         */
204 3
        $operatingSystem = $result->getOperatingSystem();
205
206 3
        $ddOs = $dd->getOs();
207 3
        if (isset($ddOs['name']) && $this->isRealResult($ddOs['name']) === true) {
208 1
            $operatingSystem->setName($ddOs['name']);
209
        }
210
211 3 View Code Duplication
        if (isset($ddOs['version']) && $this->isRealResult($ddOs['version']) === 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...
212 1
            $operatingSystem->getVersion()->setComplete($ddOs['version']);
213
        }
214
215
        /*
216
         * device
217
         */
218 3
        $device = $result->getDevice();
219
220 3
        if ($this->isRealResult($dd->getModel()) === true) {
221 1
            $device->setModel($dd->getModel());
222
        }
223
224 3
        if ($this->isRealResult($dd->getBrandName()) === true) {
225 1
            $device->setBrand($dd->getBrandName());
226
        }
227
228 3
        if ($this->isRealResult($dd->getDeviceName()) === true) {
229 1
            $device->setType($dd->getDeviceName());
230
        }
231
232 3
        if ($dd->isMobile() === true) {
233 1
            $device->setIsMobile(true);
234
        }
235
236 3
        if ($dd->isTouchEnabled() === true) {
237 1
            $device->setIsTouch(true);
238
        }
239
240 3
        return $result;
241
    }
242
}
243