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

WhichBrowser::hydrateBot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception;
5
use UserAgentParser\Model;
6
use WhichBrowser\Parser as WhichBrowserParser;
7
8
class WhichBrowser extends AbstractProvider
9
{
10
    /**
11
     * Used for unitTests mocking
12
     *
13
     * @var WhichBrowserParser
14
     */
15
    private $parser;
16
17 1
    public function getName()
18
    {
19 1
        return 'WhichBrowser';
20
    }
21
22 2
    public function getComposerPackageName()
23
    {
24 2
        return 'whichbrowser/parser';
25
    }
26
27
    /**
28
     *
29
     * @param  array              $headers
30
     * @return WhichBrowserParser
31
     */
32 8
    public function getParser(array $headers)
33
    {
34 8
        if ($this->parser !== null) {
35 7
            return $this->parser;
36
        }
37
38 1
        return new WhichBrowserParser($headers);
39
    }
40
41
    /**
42
     *
43
     * @param Model\Bot             $bot
44
     * @param \WhichBrowser\Browser $browserRaw
45
     */
46 1
    private function hydrateBot(Model\Bot $bot, \WhichBrowser\Browser $browserRaw)
47
    {
48 1
        $bot->setIsBot(true);
49
50 1
        if ($this->isRealResult($browserRaw->getName()) === true) {
51 1
            $bot->setName($browserRaw->getName());
52
        }
53 1
    }
54
55
    /**
56
     *
57
     * @param Model\Browser         $browser
58
     * @param \WhichBrowser\Browser $browserRaw
59
     */
60 5
    private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Browser $browserRaw)
61
    {
62 5 View Code Duplication
        if ($this->isRealResult($browserRaw->getName()) === 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...
63 1
            $browser->setName($browserRaw->getName());
64
65 1
            if ($this->isRealResult($browserRaw->getVersion()) === true) {
66 1
                $browser->getVersion()->setComplete($browserRaw->getVersion());
67
            }
68
69 1
            return;
70
        }
71
72 4
        if (isset($browserRaw->using) && $browserRaw->using instanceof \WhichBrowser\Using) {
73
            /* @var $usingRaw \WhichBrowser\Using */
74 1
            $usingRaw = $browserRaw->using;
75
76 1 View Code Duplication
            if ($this->isRealResult($usingRaw->getName()) === 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...
77 1
                $browser->setName($usingRaw->getName());
78
79 1
                if ($this->isRealResult($usingRaw->getVersion()) === true) {
80 1
                    $browser->getVersion()->setComplete($usingRaw->getVersion());
81
                }
82
            }
83
        }
84 4
    }
85
86
    /**
87
     *
88
     * @param Model\RenderingEngine $engine
89
     * @param \WhichBrowser\Engine  $engineRaw
90
     */
91 5 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, \WhichBrowser\Engine $engineRaw)
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...
92
    {
93 5
        if ($this->isRealResult($engineRaw->getName()) === true) {
94 1
            $engine->setName($engineRaw->getName());
95
        }
96
97 5
        if ($this->isRealResult($engineRaw->getVersion()) === true) {
98 1
            $engine->getVersion()->setComplete($engineRaw->getVersion());
99
        }
100 5
    }
101
102
    /**
103
     *
104
     * @param Model\OperatingSystem $os
105
     * @param \WhichBrowser\Os      $osRaw
106
     */
107 5 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \WhichBrowser\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...
108
    {
109 5
        if ($this->isRealResult($osRaw->getName()) === true) {
110 1
            $os->setName($osRaw->getName());
111
        }
112
113 5
        if ($this->isRealResult($osRaw->getVersion()) === true) {
114 1
            $os->getVersion()->setComplete($osRaw->getVersion());
115
        }
116 5
    }
117
118
    /**
119
     *
120
     * @param Model\Device         $device
121
     * @param \WhichBrowser\Device $deviceRaw
122
     * @param WhichBrowserParser   $parser
123
     */
124 5
    private function hydrateDevice(Model\Device $device, \WhichBrowser\Device $deviceRaw, WhichBrowserParser $parser)
125
    {
126 5
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
127 1
            $device->setModel($deviceRaw->getModel());
128
        }
129
130 5
        if ($this->isRealResult($deviceRaw->getManufacturer()) === true) {
131 1
            $device->setBrand($deviceRaw->getManufacturer());
132
        }
133
134 5
        $device->setType($parser->getType());
135
136 5
        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
137 1
            $device->setIsMobile(true);
138
        }
139 5
    }
140
141 7
    public function parse($userAgent, array $headers = [])
142
    {
143 7
        $headers['User-Agent'] = $userAgent;
144
145 7
        $parser = $this->getParser($headers);
146
147
        /*
148
         * No result found?
149
         */
150 7
        if ($parser->isDetected() !== true) {
151 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
152
        }
153
154
        /*
155
         * Hydrate the model
156
         */
157 6
        $result = new Model\UserAgent();
158 6
        $result->setProviderResultRaw($parser->toArray());
159
160
        /*
161
         * Bot detection
162
         */
163 6
        if ($parser->getType() === 'bot') {
164 1
            $this->hydrateBot($result->getBot(), $parser->browser);
165
166 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...
167
        }
168
169
        /*
170
         * hydrate the result
171
         */
172 5
        $this->hydrateBrowser($result->getBrowser(), $parser->browser);
173 5
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->engine);
174 5
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->os);
175 5
        $this->hydrateDevice($result->getDevice(), $parser->device, $parser);
176
177 5
        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...
178
    }
179
}
180