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

UAParser::parse()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 14

Duplication

Lines 39
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 39
loc 39
ccs 14
cts 14
cp 1
rs 8.8571
cc 3
eloc 14
nc 3
nop 2
crap 3
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UAParser\Parser;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class UAParser extends AbstractProvider
9
{
10
    protected $defaultValues = [
11
        'Other',
12
    ];
13
14
    private $parser;
15
16 1
    public function getName()
17
    {
18 1
        return 'UAParser';
19
    }
20
21 2
    public function getComposerPackageName()
22
    {
23 2
        return 'ua-parser/uap-php';
24
    }
25
26
    /**
27
     *
28
     * @param Parser $parser
29
     */
30 6
    public function setParser(Parser $parser = null)
31
    {
32 6
        $this->parser = $parser;
33 6
    }
34
35
    /**
36
     *
37
     * @return Parser
38
     */
39 6
    public function getParser()
40
    {
41 6
        if ($this->parser !== null) {
42 6
            return $this->parser;
43
        }
44
45 1
        $this->parser = Parser::create();
46
47 1
        return $this->parser;
48
    }
49
50
    /**
51
     *
52
     * @param \UAParser\Result\Client $resultRaw
53
     *
54
     * @return bool
55
     */
56 5
    private function hasResult(\UAParser\Result\Client $resultRaw)
57
    {
58 5
        if ($this->isRealResult($resultRaw->ua->family)) {
59 2
            return true;
60
        }
61
62 3
        if ($this->isRealResult($resultRaw->os->family)) {
63 1
            return true;
64
        }
65
66 2
        if ($this->isRealResult($resultRaw->device->model)) {
67 1
            return true;
68
        }
69
70 1
        return false;
71
    }
72
73 3
    private function getDeviceModelDefaultValues()
74
    {
75
        return [
76 3
            'Feature Phone',
77 3
            'iOS-Device',
78 3
            'Smartphone',
79 3
        ];
80
    }
81
82 3
    private function getDeviceBrandDefaultValues()
83
    {
84
        return [
85 3
            'Generic',
86 3
            'Generic_Android',
87 3
            'Generic_Inettv',
88 3
        ];
89
    }
90
91
    /**
92
     *
93
     * @param \UAParser\Result\Client $resultRaw
94
     *
95
     * @return bool
96
     */
97 4
    private function isBot(\UAParser\Result\Client $resultRaw)
98
    {
99 4
        if ($resultRaw->device->family === 'Spider') {
100 1
            return true;
101
        }
102
103 3
        return false;
104
    }
105
106
    /**
107
     *
108
     * @param Model\Bot               $bot
109
     * @param \UAParser\Result\Client $resultRaw
110
     */
111 1
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
112
    {
113 1
        $bot->setIsBot(true);
114
115 1
        if ($this->isRealResult($resultRaw->ua->family) === true) {
116 1
            $bot->setName($resultRaw->ua->family);
117
        }
118 1
    }
119
120
    /**
121
     *
122
     * @param Model\Browser           $browser
123
     * @param \UAParser\Result\Client $resultRaw
124
     */
125 3 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\Client $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...
126
    {
127 3
        if ($this->isRealResult($resultRaw->ua->family) === true) {
128 1
            $browser->setName($resultRaw->ua->family);
129
        }
130
131 3
        if ($this->isRealResult($resultRaw->ua->major) === true) {
132 1
            $browser->getVersion()->setMajor($resultRaw->ua->major);
133
        }
134
135 3
        if ($this->isRealResult($resultRaw->ua->minor) === true) {
136 1
            $browser->getVersion()->setMinor($resultRaw->ua->minor);
137
        }
138
139 3
        if ($this->isRealResult($resultRaw->ua->patch) === true) {
140 1
            $browser->getVersion()->setPatch($resultRaw->ua->patch);
141
        }
142 3
    }
143
144
    /**
145
     *
146
     * @param Model\OperatingSystem   $os
147
     * @param \UAParser\Result\Client $resultRaw
148
     */
149 3 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\Client $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...
150
    {
151 3
        if ($this->isRealResult($resultRaw->os->family) === true) {
152 1
            $os->setName($resultRaw->os->family);
153
        }
154
155 3
        if ($this->isRealResult($resultRaw->os->major) === true) {
156 1
            $os->getVersion()->setMajor($resultRaw->os->major);
157
        }
158
159 3
        if ($this->isRealResult($resultRaw->os->minor) === true) {
160 1
            $os->getVersion()->setMinor($resultRaw->os->minor);
161
        }
162
163 3
        if ($this->isRealResult($resultRaw->os->patch) === true) {
164 1
            $os->getVersion()->setPatch($resultRaw->os->patch);
165
        }
166 3
    }
167
168
    /**
169
     *
170
     * @param Model\UserAgent         $device
171
     * @param \UAParser\Result\Client $resultRaw
172
     */
173 3
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Client $resultRaw)
174
    {
175 3
        if ($this->isRealResult($resultRaw->device->model, $this->getDeviceModelDefaultValues()) === true) {
176 1
            $device->setModel($resultRaw->device->model);
177
        }
178
179 3
        if ($this->isRealResult($resultRaw->device->brand, $this->getDeviceBrandDefaultValues()) === true) {
180 1
            $device->setBrand($resultRaw->device->brand);
181
        }
182 3
    }
183
184 5 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...
185
    {
186 5
        $parser = $this->getParser();
187
188
        /* @var $resultRaw \UAParser\Result\Client */
189 5
        $resultRaw = $parser->parse($userAgent);
190
191
        /*
192
         * No result found?
193
         */
194 5
        if ($this->hasResult($resultRaw) !== true) {
195 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
196
        }
197
198
        /*
199
         * Hydrate the model
200
         */
201 4
        $result = new Model\UserAgent();
202 4
        $result->setProviderResultRaw($resultRaw);
203
204
        /*
205
         * Bot detection
206
         */
207 4
        if ($this->isBot($resultRaw) === true) {
208 1
            $this->hydrateBot($result->getBot(), $resultRaw);
209
210 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...
211
        }
212
213
        /*
214
         * Browser
215
         */
216 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
217
        // renderingEngine is currently not possible
218 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
219 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
220
221 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...
222
    }
223
}
224