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

PiwikDeviceDetector::parse()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 8.8571
cc 3
eloc 16
nc 3
nop 2
crap 3
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 array
59
     */
60 4
    private function getResultRaw(DeviceDetector $dd)
61
    {
62
        $raw = [
63 4
            'client'          => $dd->getClient(),
64 4
            'operatingSystem' => $dd->getOs(),
65
66
            'device' => [
67 4
                'brand'     => $dd->getBrand(),
68 4
                'brandName' => $dd->getBrandName(),
69
70 4
                'model' => $dd->getModel(),
71
72 4
                'device'     => $dd->getDevice(),
73 4
                'deviceName' => $dd->getDeviceName(),
74 4
            ],
75
76 4
            'bot' => $dd->getBot(),
77
78
            'extra' => [
79 4
                'isBot' => $dd->isBot(),
80
81
                // client
82 4
                'isBrowser'     => $dd->isBrowser(),
83 4
                'isFeedReader'  => $dd->isFeedReader(),
84 4
                'isMobileApp'   => $dd->isMobileApp(),
85 4
                'isPIM'         => $dd->isPIM(),
86 4
                'isLibrary'     => $dd->isLibrary(),
87 4
                'isMediaPlayer' => $dd->isMediaPlayer(),
88
89
                // deviceType
90 4
                'isCamera'              => $dd->isCamera(),
91 4
                'isCarBrowser'          => $dd->isCarBrowser(),
92 4
                'isConsole'             => $dd->isConsole(),
93 4
                'isFeaturePhone'        => $dd->isFeaturePhone(),
94 4
                'isPhablet'             => $dd->isPhablet(),
95 4
                'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(),
96 4
                'isSmartDisplay'        => $dd->isSmartDisplay(),
97 4
                'isSmartphone'          => $dd->isSmartphone(),
98 4
                'isTablet'              => $dd->isTablet(),
99 4
                'isTV'                  => $dd->isTV(),
100
101
                // other special
102 4
                'isDesktop'      => $dd->isDesktop(),
103 4
                'isMobile'       => $dd->isMobile(),
104 4
                'isTouchEnabled' => $dd->isTouchEnabled(),
105 4
            ],
106 4
        ];
107
108 4
        return $raw;
109
    }
110
111
    /**
112
     *
113
     * @param DeviceDetector $dd
114
     *
115
     * @return bool
116
     */
117 5
    private function hasResult(DeviceDetector $dd)
118
    {
119 5
        if ($dd->isBot() === true) {
120 1
            return true;
121
        }
122
123 4
        $client = $dd->getClient();
124 4
        if (isset($client['name']) && $this->isRealResult($client['name'])) {
125 1
            return true;
126
        }
127
128 3
        $os = $dd->getOs();
129 3
        if (isset($os['name']) && $this->isRealResult($os['name'])) {
130 1
            return true;
131
        }
132
133 2
        if ($dd->getDevice() !== null) {
134 1
            return true;
135
        }
136
137 1
        return false;
138
    }
139
140
    /**
141
     *
142
     * @param Model\Bot     $bot
143
     * @param array|boolean $botRaw
144
     */
145 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...
146
    {
147 1
        $bot->setIsBot(true);
148
149 1
        if (isset($botRaw['name']) && $this->isRealResult($botRaw['name'])) {
150 1
            $bot->setName($botRaw['name']);
151
        }
152 1
        if (isset($botRaw['category']) && $this->isRealResult($botRaw['category'])) {
153 1
            $bot->setType($botRaw['category']);
154
        }
155 1
    }
156
157
    /**
158
     *
159
     * @param Model\Browser $browser
160
     * @param array|string  $clientRaw
161
     */
162 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...
163
    {
164 3
        if (isset($clientRaw['name']) && $this->isRealResult($clientRaw['name']) === true) {
165 1
            $browser->setName($clientRaw['name']);
166
        }
167
168 3
        if (isset($clientRaw['version']) && $this->isRealResult($clientRaw['version']) === true) {
169 1
            $browser->getVersion()->setComplete($clientRaw['version']);
170
        }
171 3
    }
172
173
    /**
174
     *
175
     * @param Model\RenderingEngine $engine
176
     * @param array|string          $clientRaw
177
     */
178 3
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw)
179
    {
180 3
        if (isset($clientRaw['engine']) && $this->isRealResult($clientRaw['engine']) === true) {
181 1
            $engine->setName($clientRaw['engine']);
182
        }
183 3
    }
184
185
    /**
186
     *
187
     * @param Model\OperatingSystem $os
188
     * @param array|string          $osRaw
189
     */
190 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...
191
    {
192 3
        if (isset($osRaw['name']) && $this->isRealResult($osRaw['name']) === true) {
193 1
            $os->setName($osRaw['name']);
194
        }
195
196 3
        if (isset($osRaw['version']) && $this->isRealResult($osRaw['version']) === true) {
197 1
            $os->getVersion()->setComplete($osRaw['version']);
198
        }
199 3
    }
200
201
    /**
202
     *
203
     * @param Model\UserAgent $device
204
     * @param DeviceDetector  $dd
205
     */
206 3
    private function hydrateDevice(Model\Device $device, DeviceDetector $dd)
207
    {
208 3
        if ($this->isRealResult($dd->getModel()) === true) {
209 1
            $device->setModel($dd->getModel());
210
        }
211
212 3
        if ($this->isRealResult($dd->getBrandName()) === true) {
213 1
            $device->setBrand($dd->getBrandName());
214
        }
215
216 3
        if ($this->isRealResult($dd->getDeviceName()) === true) {
217 1
            $device->setType($dd->getDeviceName());
218
        }
219
220 3
        if ($dd->isMobile() === true) {
221 1
            $device->setIsMobile(true);
222
        }
223
224 3
        if ($dd->isTouchEnabled() === true) {
225 1
            $device->setIsTouch(true);
226
        }
227 3
    }
228
229 5
    public function parse($userAgent, array $headers = [])
230
    {
231 5
        $dd = $this->getParser();
232
233 5
        $dd->setUserAgent($userAgent);
234 5
        $dd->parse();
235
236
        /*
237
         * No result found?
238
         */
239 5
        if ($this->hasResult($dd) !== true) {
240 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
241
        }
242
243
        /*
244
         * Hydrate the model
245
         */
246 4
        $result = new Model\UserAgent();
247 4
        $result->setProviderResultRaw($this->getResultRaw($dd));
248
249
        /*
250
         * Bot detection
251
         */
252 4
        if ($dd->isBot() === true) {
253 1
            $this->hydrateBot($result->getBot(), $dd->getBot());
254
255 1
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (UserAgentParser\Model\UserAgent) is incompatible with the return type declared by the abstract method UserAgentParser\Provider\AbstractProvider::parse of type UserAgentParser\Result\UserAgent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
256
        }
257
258
        /*
259
         * hydrate the result
260
         */
261 3
        $this->hydrateBrowser($result->getBrowser(), $dd->getClient());
262 3
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $dd->getClient());
263 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $dd->getOs());
264 3
        $this->hydrateDevice($result->getDevice(), $dd);
265
266 3
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (UserAgentParser\Model\UserAgent) is incompatible with the return type declared by the abstract method UserAgentParser\Provider\AbstractProvider::parse of type UserAgentParser\Result\UserAgent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
267
    }
268
}
269