Test Failed
Pull Request — master (#78)
by Gabriel
06:05
created

Browser::detectScriptedAgent()   C

Complexity

Conditions 40
Paths 3

Size

Total Lines 56
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1640

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 56
ccs 0
cts 17
cp 0
rs 6.1497
cc 40
eloc 45
nc 3
nop 0
crap 1640

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sinergi\BrowserDetector;
4
5
class Browser
6
{
7
    const UNKNOWN = 'unknown';
8
    const VIVALDI = 'Vivaldi';
9
    const OPERA = 'Opera';
10
    const OPERA_MINI = 'Opera Mini';
11
    const WEBTV = 'WebTV';
12
    const IE = 'Internet Explorer';
13
    const POCKET_IE = 'Pocket Internet Explorer';
14
    const KONQUEROR = 'Konqueror';
15
    const ICAB = 'iCab';
16
    const OMNIWEB = 'OmniWeb';
17
    const FIREBIRD = 'Firebird';
18
    const FIREFOX = 'Firefox';
19
    const SEAMONKEY = 'SeaMonkey';
20
    const ICEWEASEL = 'Iceweasel';
21
    const SHIRETOKO = 'Shiretoko';
22
    const MOZILLA = 'Mozilla';
23
    const AMAYA = 'Amaya';
24
    const LYNX = 'Lynx';
25
    const SAFARI = 'Safari';
26
    const SAMSUNG_BROWSER = 'SamsungBrowser';
27
    const CHROME = 'Chrome';
28
    const NAVIGATOR = 'Android Navigator';
29
    const BLACKBERRY = 'BlackBerry';
30
    const ICECAT = 'IceCat';
31
    const NOKIA_S60 = 'Nokia S60 OSS Browser';
32
    const NOKIA = 'Nokia Browser';
33
    const MSN = 'MSN Browser';
34
    const NETSCAPE_NAVIGATOR = 'Netscape Navigator';
35
    const GALEON = 'Galeon';
36
    const NETPOSITIVE = 'NetPositive';
37
    const PHOENIX = 'Phoenix';
38
    const GSA = 'Google Search Appliance';
39
    const YANDEX = 'Yandex';
40
    const EDGE = 'Edge';
41
    const DRAGON = 'Dragon';
42
    const NSPLAYER = 'Windows Media Player';
43
    const UCBROWSER = 'UC Browser';
44
    const MICROSOFT_OFFICE = 'Microsoft Office';
45
    const APPLE_NEWS = 'Apple News';
46
    const DALVIK = 'Android';
47
48
    const VERSION_UNKNOWN = 'unknown';
49
50
    /**
51
     * @var UserAgent
52
     */
53
    private $userAgent;
54
55
    /**
56
     * @var string
57
     */
58
    private $name;
59
    /**
60
     * @var string
61
     */
62
    private $version;
63
64
    /**
65
     * @var bool
66
     */
67
    private $isChromeFrame = false;
68
69
    /**
70
     * @var bool
71
     */
72
    private $isWebkit = false;
73
74
    /**
75
     * @var bool
76
     */
77
    private $isFacebookWebView = false;
78
79
    /**
80
     * @var bool
81
     */
82
    private $isTwitterWebView = false;
83
84
    /**
85
     * @var bool
86
     */
87
    private $isCompatibilityMode = false;
88
89
    /**
90
     * @param null|string|UserAgent $userAgent
91
     *
92 8
     * @throws \Sinergi\BrowserDetector\InvalidArgumentException
93
     */
94 8 View Code Duplication
    public function __construct($userAgent = null)
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...
95
    {
96 8
        if ($userAgent instanceof UserAgent) {
97 8
            $this->setUserAgent($userAgent);
98 8
        } elseif (null === $userAgent || is_string($userAgent)) {
99
            $this->setUserAgent(new UserAgent($userAgent));
100
        } else {
101 8
            throw new InvalidArgumentException();
102
        }
103
    }
104
105
    /**
106
     * Set the name of the Browser.
107
     *
108
     * @param string $name
109
     *
110 8
     * @return $this
111
     */
112 8
    public function setName($name)
113
    {
114 8
        $this->name = (string)$name;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Return the name of the Browser.
121
     *
122 7
     * @return string
123
     */
124 7
    public function getName()
125 7
    {
126 7
        if (!isset($this->name)) {
127
            BrowserDetector::detect($this, $this->getUserAgent());
128 7
        }
129
130
        return $this->name;
131
    }
132
133
    /**
134
     * Check to see if the specific browser is valid.
135
     *
136
     * @param string $name
137
     *
138
     * @return bool
139
     */
140
    public function isBrowser($name)
141
    {
142
        return (0 == strcasecmp($this->getName(), trim($name)));
143
    }
144
145
    /**
146
     * Set the version of the browser.
147
     *
148
     * @param string $version
149
     *
150 8
     * @return $this
151
     */
152 8
    public function setVersion($version)
153
    {
154 8
        $this->version = (string)$version;
155
156
        return $this;
157
    }
158
159
    /**
160
     * The version of the browser.
161
     *
162 7
     * @return string
163
     */
164 7
    public function getVersion()
165
    {
166
        if (!isset($this->name)) {
167
            BrowserDetector::detect($this, $this->getUserAgent());
168 7
        }
169
170
        return (string) $this->version;
171
    }
172
173
    /**
174
     * Detects scripted agents (robots / bots)
175
     * Returns a resolved ScriptedAgent object if detected.
176
     * Otherwise returns false.
177
     *
178
     * @return ScriptedAgent|bool
179
     */
180
    public function detectScriptedAgent()
0 ignored issues
show
Coding Style introduced by
detectScriptedAgent uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
181
    {
182
        $ua = $this->getUserAgent()->getUserAgentString();
183
        if (stripos($ua, 'bot') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
184
            stripos($ua, 'spider') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
185
            stripos($ua, 'crawler') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
186
            stripos($ua, 'preview') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
187
            stripos($ua, 'slurp') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
188
            stripos($ua, 'facebookexternalhit') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
189
            stripos($ua, 'mediapartners') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
190
            stripos($ua, 'google-adwords') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
191
            stripos($ua, 'adxvastfetcher') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
192
            stripos($ua, 'adbeat') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
193
            stripos($ua, 'google favicon') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
194
            stripos($ua, 'webdav client') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
195
            stripos($ua, 'metauri api') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
196
            stripos($ua, 'tlsprobe') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
197
            stripos($ua, 'wpif') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
198
            stripos($ua, 'imgsizer') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
199
            stripos($ua, 'netcraft ssl server survey') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
200
            stripos($ua, 'curl/') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
201
            stripos($ua, 'go-http-client/') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
202
            stripos($ua, 'python') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
203
            stripos($ua, 'libwww') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
204
            stripos($ua, 'wget/') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
205
            stripos($ua, 'zgrab/') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
206
            stripos($ua, 'Java/') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
207
            stripos($ua, '() { :;}; /bin/bash -c') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
208
            stripos($ua, 'browsershots') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
209
            stripos($ua, 'magereport') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
210
            stripos($ua, 'ubermetrics-technologies') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
211
            stripos($ua, 'W3C') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
212
            stripos($ua, 'Validator') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
213
            stripos($ua, 'Jigsaw/') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
214
            stripos($ua, 'bing') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
215
            stripos($ua, 'msn') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
216
            stripos($ua, 'Google Web Preview') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
217
            stripos($ua, 'ips-agent') !== FALSE ||
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
218
            (stripos($ua, 'Chrome/51.0.2704.103') !== FALSE && !isset($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) && stristr($_SERVER['HTTP_ACCEPT_LANGUAGE'], "ru-RU") !== FALSE) //ICQ Preview
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
219
        )
220
        {
221
            $scriptedAgent = new ScriptedAgent($ua);
222
            if ($scriptedAgent->getName()==ScriptedAgent::UNKNOWN)
223
            {
224
                return false;
225
            }
226
            else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
227
            {
228
                return $scriptedAgent;
229
            }
230
        }
231
        else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
232
        {
233
            return false;
234
        }
235
    }
236
237
    /**
238
     * @param bool $isChromeFrame
239
     *
240
     * @return $this
241
     */
242
    public function setIsChromeFrame($isChromeFrame)
243
    {
244
        $this->isChromeFrame = (bool)$isChromeFrame;
245
246 1
        return $this;
247
    }
248 1
249
    /**
250 1
     * Used to determine if the browser is actually "chromeframe".
251
     *
252
     * @return bool
253
     */
254
    public function getIsChromeFrame()
255
    {
256
        if (!isset($this->name)) {
257
            BrowserDetector::detect($this, $this->getUserAgent());
258 1
        }
259
260 1
        return $this->isChromeFrame;
261 1
    }
262 1
263
    /**
264 1
     * @return bool
265
     */
266
    public function isChromeFrame()
267
    {
268
        return $this->getIsChromeFrame();
269
    }
270 1
271
    /**
272 1
     * @param bool $isChromeFrame
0 ignored issues
show
Bug introduced by
There is no parameter named $isChromeFrame. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
273
     *
274
     * @return $this
275
     */
276
    public function setIsWebkit($isWebkit)
277
    {
278
        $this->isWebkit = (bool)$isWebkit;
279
280 8
        return $this;
281
    }
282 8
283
    /**
284 8
     * Used to determine if the browser is actually "chromeframe".
285
     *
286
     * @return bool
287
     */
288
    public function getIsWebkit()
289
    {
290 8
        if (!isset($this->name)) {
291
            BrowserDetector::detect($this, $this->getUserAgent());
292 8
        }
293
294
        return $this->isWebkit;
295
    }
296
297
    /**
298
     * @return bool
299
     */
300 1
    public function isWebkit()
301
    {
302 1
        return $this->getIsWebkit();
303
    }
304 1
305
    /**
306
     * @param bool $isFacebookWebView
307
     *
308
     * @return $this
309
     */
310
    public function setIsFacebookWebView($isFacebookWebView)
311
    {
312
        $this->isFacebookWebView = (bool) $isFacebookWebView;
313
314
        return $this;
315
    }
316
317
    /**
318
     * Used to determine if the browser is actually "facebook".
319
     *
320
     * @return bool
321
     */
322
    public function getIsFacebookWebView()
323
    {
324
        if (!isset($this->name)) {
325
            BrowserDetector::detect($this, $this->getUserAgent());
326
        }
327
328
        return $this->isFacebookWebView;
329
    }
330
331
    /**
332
     * @return bool
333
     */
334
    public function isFacebookWebView()
335
    {
336
        return $this->getIsFacebookWebView();
337
    }
338
339
    /**
340
     * @param bool $isTwitterWebView
341
     *
342
     * @return $this
343
     */
344
    public function setIsTwitterWebView($isTwitterWebView)
345
    {
346
        $this->isTwitterWebView = (bool) $isTwitterWebView;
347
348
        return $this;
349
    }
350
351
    /**
352
     * Used to determine if the browser is actually "Twitter".
353
     *
354
     * @return bool
355
     */
356
    public function getIsTwitterWebView()
357
    {
358
        if (!isset($this->name)) {
359
            BrowserDetector::detect($this, $this->getUserAgent());
360
        }
361
362
        return $this->isTwitterWebView;
363
    }
364
365
    /**
366
     * @return bool
367
     */
368
    public function isTwitterWebView()
369
    {
370
        return $this->getIsTwitterWebView();
371
    }
372
373
    /**
374
     * @param UserAgent $userAgent
375
     *
376
     * @return $this
377
     */
378
    public function setUserAgent(UserAgent $userAgent)
379
    {
380
        $this->userAgent = $userAgent;
381
382
        return $this;
383
    }
384
385
    /**
386
     * @return UserAgent
387
     */
388
    public function getUserAgent()
389
    {
390
        return $this->userAgent;
391
    }
392
393
    /**
394
     * @param bool
395
     *
396
     * @return $this
397
     */
398
    public function setIsCompatibilityMode($isCompatibilityMode)
399
    {
400
        $this->isCompatibilityMode = $isCompatibilityMode;
401
402
        return $this;
403
    }
404
405
    /**
406
     * @return bool
407
     */
408
    public function isCompatibilityMode()
409
    {
410
        return $this->isCompatibilityMode;
411
    }
412
413
    /**
414
     * Render pages outside of IE's compatibility mode.
415
     */
416
    public function endCompatibilityMode()
417
    {
418
        header('X-UA-Compatible: IE=edge');
419
    }
420
}
421