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:45
created

SinergiBrowserDetector   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 200
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 25
c 4
b 0
f 0
lcom 1
cbo 11
dl 20
loc 200
ccs 64
cts 64
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getComposerPackageName() 0 4 1
A getBrowserParser() 0 8 2
A getOperatingSystemParser() 0 8 2
A getDeviceParser() 0 8 2
B hasResult() 0 20 5
A hydrateBrowser() 10 10 3
A hydrateOperatingSystem() 10 10 3
A hydrateDevice() 0 10 3
B parse() 0 42 3

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 Sinergi\BrowserDetector;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class SinergiBrowserDetector extends AbstractProvider
9
{
10
    protected $defaultValues = [
11
        BrowserDetector\Browser::UNKNOWN,
12
    ];
13
14
    /**
15
     * Used for unitTests mocking
16
     *
17
     * @var BrowserDetector\Browser
18
     */
19
    private $browserParser;
20
21
    /**
22
     * Used for unitTests mocking
23
     *
24
     * @var BrowserDetector\Os
25
     */
26
    private $osParser;
27
28
    /**
29
     * Used for unitTests mocking
30
     *
31
     * @var BrowserDetector\Device
32
     */
33
    private $deviceParser;
34
35 1
    public function getName()
36
    {
37 1
        return 'SinergiBrowserDetector';
38
    }
39
40 2
    public function getComposerPackageName()
41
    {
42 2
        return 'sinergi/browser-detector';
43
    }
44
45
    /**
46
     *
47
     * @param  string                  $userAgent
48
     * @return BrowserDetector\Browser
49
     */
50 6
    public function getBrowserParser($userAgent)
51
    {
52 6
        if ($this->browserParser !== null) {
53 5
            return $this->browserParser;
54
        }
55
56 1
        return new BrowserDetector\Browser($userAgent);
57
    }
58
59
    /**
60
     *
61
     * @param  string             $userAgent
62
     * @return BrowserDetector\Os
63
     */
64 6
    public function getOperatingSystemParser($userAgent)
65
    {
66 6
        if ($this->osParser !== null) {
67 5
            return $this->osParser;
68
        }
69
70 1
        return new BrowserDetector\Os($userAgent);
71
    }
72
73
    /**
74
     *
75
     * @param  string                 $userAgent
76
     * @return BrowserDetector\Device
77
     */
78 6
    public function getDeviceParser($userAgent)
79
    {
80 6
        if ($this->deviceParser !== null) {
81 5
            return $this->deviceParser;
82
        }
83
84 1
        return new BrowserDetector\Device($userAgent);
85
    }
86
87
    /**
88
     *
89
     * @param BrowserDetector\Browser $browserRaw
90
     * @param BrowserDetector\Os      $osRaw
91
     * @param BrowserDetector\Device  $deviceRaw
92
     *
93
     * @return boolean
94
     */
95 5
    private function hasResult(BrowserDetector\Browser $browserRaw, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
96
    {
97 5
        if ($this->isRealResult($browserRaw->getName())) {
98 1
            return true;
99
        }
100
101 4
        if ($this->isRealResult($osRaw->getName())) {
102 1
            return true;
103
        }
104
105 3
        if ($this->isRealResult($deviceRaw->getName())) {
106 1
            return true;
107
        }
108
109 2
        if ($browserRaw->isRobot() === true) {
110 1
            return true;
111
        }
112
113 1
        return false;
114
    }
115
116
    /**
117
     *
118
     * @param Model\Browser           $browser
119
     * @param BrowserDetector\Browser $browserRaw
120
     */
121 3 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, BrowserDetector\Browser $browserRaw)
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...
122
    {
123 3
        if ($this->isRealResult($browserRaw->getName()) === true) {
124 1
            $browser->setName($browserRaw->getName());
125
        }
126
127 3
        if ($this->isRealResult($browserRaw->getVersion()) === true) {
128 1
            $browser->getVersion()->setComplete($browserRaw->getVersion());
129
        }
130 3
    }
131
132
    /**
133
     *
134
     * @param Model\OperatingSystem $os
135
     * @param BrowserDetector\Os    $osRaw
136
     */
137 3 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, BrowserDetector\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...
138
    {
139 3
        if ($this->isRealResult($osRaw->getName()) === true) {
140 1
            $os->setName($osRaw->getName());
141
        }
142
143 3
        if ($this->isRealResult($osRaw->getVersion()) === true) {
144 1
            $os->getVersion()->setComplete($osRaw->getVersion());
145
        }
146 3
    }
147
148
    /**
149
     *
150
     * @param Model\UserAgent        $device
151
     * @param BrowserDetector\Os     $osRaw
152
     * @param BrowserDetector\Device $deviceRaw
153
     */
154 3
    private function hydrateDevice(Model\Device $device, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
155
    {
156 3
        if ($this->isRealResult($deviceRaw->getName()) === true) {
157 1
            $device->setModel($deviceRaw->getName());
158
        }
159
160 3
        if ($osRaw->isMobile() === true) {
161 1
            $device->setIsMobile(true);
162
        }
163 3
    }
164
165 5
    public function parse($userAgent, array $headers = [])
166
    {
167 5
        $browserRaw = $this->getBrowserParser($userAgent);
168 5
        $osRaw      = $this->getOperatingSystemParser($userAgent);
169 5
        $deviceRaw  = $this->getDeviceParser($userAgent);
170
171
        /*
172
         * No result found?
173
         */
174 5
        if ($this->hasResult($browserRaw, $osRaw, $deviceRaw) !== true) {
175 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
176
        }
177
178
        /*
179
         * Hydrate the model
180
         */
181 4
        $result = new Model\UserAgent();
182 4
        $result->setProviderResultRaw([
183 4
            'browser'         => $browserRaw,
184 4
            'operatingSystem' => $osRaw,
185 4
            'device'          => $deviceRaw,
186 4
        ]);
187
188
        /*
189
         * Bot detection
190
         */
191 4
        if ($browserRaw->isRobot() === true) {
192 1
            $bot = $result->getBot();
193 1
            $bot->setIsBot(true);
194
195 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...
196
        }
197
198
        /*
199
         * hydrate the result
200
         */
201 3
        $this->hydrateBrowser($result->getBrowser(), $browserRaw);
202 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $osRaw);
203 3
        $this->hydrateDevice($result->getDevice(), $osRaw, $deviceRaw);
204
205 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...
206
    }
207
}
208