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

BrowscapPhp::hydrateBot()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 7.7561
cc 9
eloc 10
nc 8
nop 2
crap 9
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use BrowscapPHP\Browscap;
5
use stdClass;
6
use UserAgentParser\Exception;
7
use UserAgentParser\Model;
8
9
class BrowscapPhp extends AbstractProvider
10
{
11
    protected $defaultValues = [
12
        'DefaultProperties',
13
        'Default Browser',
14
15
        'unknown',
16
    ];
17
18
    /**
19
     *
20
     * @var Browscap
21
     */
22
    private $parser;
23
24 15
    public function __construct(Browscap $parser)
25
    {
26 15
        $this->setParser($parser);
27 15
    }
28
29 1
    public function getName()
30
    {
31 1
        return 'BrowscapPhp';
32
    }
33
34 1
    public function getComposerPackageName()
35
    {
36 1
        return 'browscap/browscap-php';
37
    }
38
39 1
    public function getVersion()
40
    {
41 1
        return $this->getParser()
42 1
            ->getCache()
43 1
            ->getVersion();
44
    }
45
46
    /**
47
     *
48
     * @param Browscap $parser
49
     */
50 15
    public function setParser(Browscap $parser)
51
    {
52 15
        $this->parser = $parser;
53 15
    }
54
55
    /**
56
     *
57
     * @return Browscap
58
     */
59 13
    public function getParser()
60
    {
61 13
        return $this->parser;
62
    }
63
64
    /**
65
     *
66
     * @return array
67
     */
68 1
    private function getDeviceModelDefaultValues()
69
    {
70
        return [
71 1
            'general Desktop',
72 1
            'general Mobile Device',
73 1
            'general Mobile Phone',
74 1
            'general Tablet',
75 1
        ];
76
    }
77
78
    /**
79
     *
80
     * @param stdClass $resultRaw
81
     *
82
     * @return bool
83
     */
84 11
    private function hasResult(stdClass $resultRaw)
85
    {
86 11
        if (! isset($resultRaw->browser)) {
87 2
            return false;
88
        }
89
90 9
        if ($this->isRealResult($resultRaw->browser) !== true) {
91 4
            return false;
92
        }
93
94 5
        return true;
95
    }
96
97
    /**
98
     *
99
     * @param  stdClass $resultRaw
100
     * @return boolean
101
     */
102 5
    private function isBot(stdClass $resultRaw)
103
    {
104 5
        if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) {
105 2
            return false;
106
        }
107
108 3
        return true;
109
    }
110
111
    /**
112
     *
113
     * @param Model\Bot $bot
114
     * @param stdClass  $resultRaw
115
     */
116 3
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
117
    {
118 3
        $bot->setIsBot(true);
119
120 3
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
121 3
            $bot->setName($resultRaw->browser);
122
        }
123
124
        // @todo convert to a common set of types (over all vendors)
125 3
        if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) {
126 1
            $bot->setType('RSS');
127 3
        } elseif (isset($resultRaw->browser_type) && $resultRaw->browser_type === 'Bot/Crawler') {
128 1
            $bot->setType('Crawler');
129 2
        } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) {
130 1
            $bot->setType($resultRaw->browser_type);
131
        }
132 3
    }
133
134
    /**
135
     *
136
     * @param Model\Browser $browser
137
     * @param stdClass      $resultRaw
138
     */
139 2 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
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...
140
    {
141 2
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
142 2
            $browser->setName($resultRaw->browser);
143
        }
144
145 2
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
146 2
            $browser->getVersion()->setComplete($resultRaw->version);
147
        }
148 2
    }
149
150
    /**
151
     *
152
     * @param Model\RenderingEngine $engine
153
     * @param stdClass              $resultRaw
154
     */
155 2 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
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...
156
    {
157 2
        if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) {
158 1
            $engine->setName($resultRaw->renderingengine_name);
159
        }
160
161 2
        if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) {
162 1
            $engine->getVersion()->setComplete($resultRaw->renderingengine_version);
163
        }
164 2
    }
165
166
    /**
167
     *
168
     * @param Model\OperatingSystem $os
169
     * @param stdClass              $resultRaw
170
     */
171 2 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
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...
172
    {
173 2
        if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) {
174 1
            $os->setName($resultRaw->platform);
175
        }
176
177 2
        if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) {
178 1
            $os->getVersion()->setComplete($resultRaw->platform_version);
179
        }
180 2
    }
181
182
    /**
183
     *
184
     * @param Model\UserAgent $device
185
     * @param stdClass        $resultRaw
186
     */
187 2
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
188
    {
189 2
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, $this->getDeviceModelDefaultValues()) === true) {
190 1
            $device->setModel($resultRaw->device_name);
191
        }
192
193 2
        if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) {
194 1
            $device->setBrand($resultRaw->device_brand_name);
195
        }
196
197 2
        if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) {
198
            // @todo convert to a common set of types (over all vendors)
199 1
            $device->setType($resultRaw->device_type);
200
        }
201
202 2
        if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) {
203 1
            $device->setIsMobile(true);
204
        }
205
206 2
        if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') {
207 1
            $device->setIsTouch(true);
208
        }
209 2
    }
210
211 11 View Code Duplication
    public function parse($userAgent, array $headers = [])
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...
212
    {
213 11
        $parser = $this->getParser();
214
215
        /* @var $resultRaw \stdClass */
216 11
        $resultRaw = $parser->getBrowser($userAgent);
217
218
        /*
219
         * No result found?
220
         */
221 11
        if ($this->hasResult($resultRaw) !== true) {
222 6
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
223
        }
224
225
        /*
226
         * Hydrate the model
227
         */
228 5
        $result = new Model\UserAgent();
229 5
        $result->setProviderResultRaw($resultRaw);
230
231
        /*
232
         * Bot detection (does only work with full_php_browscap.ini)
233
         */
234 5
        if ($this->isBot($resultRaw) === true) {
235 3
            $this->hydrateBot($result->getBot(), $resultRaw);
236
237 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...
238
        }
239
240
        /*
241
         * hydrate the result
242
         */
243 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
244 2
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
245 2
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
246 2
        $this->hydrateDevice($result->getDevice(), $resultRaw);
247
248 2
        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...
249
    }
250
}
251