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
Push — master ( d31fa5...89d544 )
by Martin
04:29
created

Woothee   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 195
Duplicated Lines 24.62 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 20
Bugs 1 Features 1
Metric Value
wmc 24
c 20
b 1
f 1
lcom 1
cbo 9
dl 48
loc 195
ccs 47
cts 47
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getParser() 0 10 2
A hasResult() 0 10 3
A isBot() 0 8 3
A hydrateBot() 0 8 3
B hydrateBrowser() 10 10 5
A hydrateDevice() 0 6 3
B parse() 38 38 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 UserAgentParser\Exception;
5
use UserAgentParser\Model;
6
use Woothee\Classifier;
7
use Woothee\DataSet;
8
9
class Woothee extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'Woothee';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/woothee/woothee-php';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'woothee/woothee';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => false,
41
            'version' => false,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => false,
46
            'version' => false,
47
        ],
48
49
        'device' => [
50
            'model'    => false,
51
            'brand'    => false,
52
            'type'     => true,
53
            'isMobile' => false,
54
            'isTouch'  => false,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => true,
60
            'type'  => false,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
        DataSet::VALUE_UNKNOWN,
66
    ];
67
68
    private $parser;
69
70 13
    public function __construct()
71
    {
72 13
        if (! class_exists('Woothee\Classifier', true)) {
73 1
            throw new Exception\PackageNotLoadedException('You need to install ' . $this->getHomepage() . ' to use this provider');
74
        }
75 12
    }
76
77
    /**
78
     *
79
     * @return Classifier
80
     */
81 6
    public function getParser()
82
    {
83 6
        if ($this->parser !== null) {
84 5
            return $this->parser;
85
        }
86
87 1
        $this->parser = new Classifier();
88
89 1
        return $this->parser;
90
    }
91
92
    /**
93
     *
94
     * @param array $resultRaw
95
     *
96
     * @return bool
97
     */
98 5
    private function hasResult(array $resultRaw)
99
    {
100 5
        foreach ($resultRaw as $value) {
101 4
            if ($this->isRealResult($value) === true) {
102 4
                return true;
103
            }
104 1
        }
105
106 1
        return false;
107
    }
108
109
    /**
110
     *
111
     * @param  array   $resultRaw
112
     * @return boolean
113
     */
114 4
    private function isBot(array $resultRaw)
115
    {
116 4
        if (isset($resultRaw['category']) && $resultRaw['category'] === DataSet::DATASET_CATEGORY_CRAWLER) {
117 1
            return true;
118
        }
119
120 3
        return false;
121
    }
122
123
    /**
124
     *
125
     * @param Model\Bot $bot
126
     * @param array     $resultRaw
127
     */
128 1
    private function hydrateBot(Model\Bot $bot, array $resultRaw)
129
    {
130 1
        $bot->setIsBot(true);
131
132 1
        if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name']) === true) {
133 1
            $bot->setName($resultRaw['name']);
134
        }
135 1
    }
136
137
    /**
138
     *
139
     * @param Model\Browser $browser
140
     * @param array         $resultRaw
141
     */
142 3 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, array $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...
143
    {
144 3
        if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name']) === true) {
145 1
            $browser->setName($resultRaw['name']);
146
        }
147
148 3
        if (isset($resultRaw['version']) && $this->isRealResult($resultRaw['version']) === true) {
149 1
            $browser->getVersion()->setComplete($resultRaw['version']);
150
        }
151 3
    }
152
153
    /**
154
     *
155
     * @param Model\Device $device
156
     * @param array        $resultRaw
157
     */
158 3
    private function hydrateDevice(Model\Device $device, array $resultRaw)
159
    {
160 3
        if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category']) === true) {
161 2
            $device->setType($resultRaw['category']);
162
        }
163 3
    }
164
165 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...
166
    {
167 5
        $parser = $this->getParser();
168
169 5
        $resultRaw = $parser->parse($userAgent);
170
171
        /*
172
         * No result found?
173
         */
174 5
        if ($this->hasResult($resultRaw) !== 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($resultRaw);
183
184
        /*
185
         * Bot detection
186
         */
187 4
        if ($this->isBot($resultRaw) === true) {
188 1
            $this->hydrateBot($result->getBot(), $resultRaw);
189
190 1
            return $result;
191
        }
192
193
        /*
194
         * hydrate the result
195
         */
196 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
197
        // renderingEngine not available
198
        // operatingSystem filled OS is mixed! Examples: iPod, iPhone, Android...
199 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
200
201 3
        return $result;
202
    }
203
}
204