Test Failed
Pull Request — master (#76)
by
unknown
02:35
created

Browser::getUserAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sinergi\BrowserDetector;
4
5
/**
6
 * Browser Detection.
7
 */
8
class Browser
9
{
10
    const UNKNOWN = 'unknown';
11
    const VIVALDI = 'Vivaldi';
12
    const OPERA = 'Opera';
13
    const OPERA_MINI = 'Opera Mini';
14
    const WEBTV = 'WebTV';
15
    const IE = 'Internet Explorer';
16
    const POCKET_IE = 'Pocket Internet Explorer';
17
    const KONQUEROR = 'Konqueror';
18
    const ICAB = 'iCab';
19
    const OMNIWEB = 'OmniWeb';
20
    const FIREBIRD = 'Firebird';
21
    const FIREFOX = 'Firefox';
22
    const SEAMONKEY = 'SeaMonkey';
23
    const ICEWEASEL = 'Iceweasel';
24
    const SHIRETOKO = 'Shiretoko';
25
    const MOZILLA = 'Mozilla';
26
    const AMAYA = 'Amaya';
27
    const LYNX = 'Lynx';
28
    const SAFARI = 'Safari';
29
    const SAMSUNG_BROWSER = 'SamsungBrowser';
30
    const CHROME = 'Chrome';
31
    const NAVIGATOR = 'Android Navigator';
32
    const BLACKBERRY = 'BlackBerry';
33
    const ICECAT = 'IceCat';
34
    const NOKIA_S60 = 'Nokia S60 OSS Browser';
35
    const NOKIA = 'Nokia Browser';
36
    const MSN = 'MSN Browser';
37
    const NETSCAPE_NAVIGATOR = 'Netscape Navigator';
38
    const GALEON = 'Galeon';
39
    const NETPOSITIVE = 'NetPositive';
40
    const PHOENIX = 'Phoenix';
41
    const GSA = 'Google Search Appliance';
42
    const YANDEX = 'Yandex';
43
    const EDGE = 'Edge';
44
    const DRAGON = 'Dragon';
45
    const NSPLAYER = 'Windows Media Player';
46
    const UCBROWSER = 'UC Browser';
47
    const MICROSOFT_OFFICE = 'Microsoft Office';
48
    const APPLE_NEWS = 'Apple News';
49
    const DALVIK = 'Android';
50
51
    const VERSION_UNKNOWN = 'unknown';
52
53
    /**
54
     * @var UserAgent
55
     */
56
    private $userAgent;
57
58
    /**
59
     * @var string
60
     */
61
    private $name;
62
    /**
63
     * @var string
64
     */
65
    private $version;
66
67
    /**
68
     * @var bool
69
     */
70
    private $isChromeFrame = false;
71
72
    /**
73
     * @var bool
74
     */
75
    private $isWebkit = false;
76
77
    /**
78
     * @var bool
79
     */
80
    private $isFacebookWebView = false;
81
82
    /**
83
     * @var bool
84
     */
85
    private $isTwitterWebView = false;
86
87
    /**
88
     * @var bool
89
     */
90
    private $isCompatibilityMode = false;
91
92 8
    /**
93
     * @param null|string|UserAgent $userAgent
94 8
     *
95
     * @throws \Sinergi\BrowserDetector\InvalidArgumentException
96 8
     */
97 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...
98 8
    {
99
        if ($userAgent instanceof UserAgent) {
100
            $this->setUserAgent($userAgent);
101 8
        } elseif (null === $userAgent || is_string($userAgent)) {
102
            $this->setUserAgent(new UserAgent($userAgent));
103
        } else {
104
            throw new InvalidArgumentException();
105
        }
106
    }
107
108
    /**
109
     * Set the name of the Browser.
110 8
     *
111
     * @param string $name
112 8
     *
113
     * @return $this
114 8
     */
115
    public function setName($name)
116
    {
117
        $this->name = (string)$name;
118
119
        return $this;
120
    }
121
122 7
    /**
123
     * Return the name of the Browser.
124 7
     *
125 7
     * @return string
126 7
     */
127
    public function getName()
128 7
    {
129
        if (!isset($this->name)) {
130
            BrowserDetector::detect($this, $this->getUserAgent());
131
        }
132
133
        return $this->name;
134
    }
135
136
    /**
137
     * Check to see if the specific browser is valid.
138
     *
139
     * @param string $name
140
     *
141
     * @return bool
142
     */
143
    public function isBrowser($name)
144
    {
145
        return (0 == strcasecmp($this->getName(), trim($name)));
146
    }
147
148
    /**
149
     * Set the version of the browser.
150 8
     *
151
     * @param string $version
152 8
     *
153
     * @return $this
154 8
     */
155
    public function setVersion($version)
156
    {
157
        $this->version = (string)$version;
158
159
        return $this;
160
    }
161
162 7
    /**
163
     * The version of the browser.
164 7
     *
165
     * @return string
166
     */
167
    public function getVersion()
168 7
    {
169
        if (!isset($this->name)) {
170
            BrowserDetector::detect($this, $this->getUserAgent());
171
        }
172
173
        return (string) $this->version;
174
    }
175
176
    /**
177
     * Detects scripted agents (robots / bots)
178
     * Returns a resolved ScriptedAgent object if detected.
179
     * Otherwise returns false.
180
     *
181
     * @return ScriptedAgent|bool
182
     */
183
    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...
184
    {
185
        $ua = $this->getUserAgent()->getUserAgentString();
186
        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...
187
            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...
188
            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...
189
            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...
190
            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...
191
            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...
192
            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...
193
            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...
194
            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...
195
            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...
196
            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...
197
            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...
198
            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...
199
            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...
200
            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...
201
            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...
202
            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...
203
            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...
204
            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...
205
            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...
206
            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...
207
            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...
208
            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...
209
            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...
210
            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...
211
            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...
212
            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...
213
            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...
214
            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...
215
            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...
216
            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...
217
            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...
218
            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...
219
            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...
220
            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...
221
            (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...
222
        )
223
        {
224
            $scriptedAgent = new ScriptedAgent($ua);
225
            if ($scriptedAgent->getName()==ScriptedAgent::UNKNOWN)
226
            {
227
                return false;
228
            }
229
            else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
230
            {
231
                return $scriptedAgent;
232
            }
233
        }
234
        else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
235
        {
236
            return false;
237
        }
238
    }
239
240
    /**
241
     * @param bool $isChromeFrame
242
     *
243
     * @return $this
244
     */
245
    public function setIsChromeFrame($isChromeFrame)
246 1
    {
247
        $this->isChromeFrame = (bool)$isChromeFrame;
248 1
249
        return $this;
250 1
    }
251
252
    /**
253
     * Used to determine if the browser is actually "chromeframe".
254
     *
255
     * @return bool
256
     */
257
    public function getIsChromeFrame()
258 1
    {
259
        if (!isset($this->name)) {
260 1
            BrowserDetector::detect($this, $this->getUserAgent());
261 1
        }
262 1
263
        return $this->isChromeFrame;
264 1
    }
265
266
    /**
267
     * @return bool
268
     */
269
    public function isChromeFrame()
270 1
    {
271
        return $this->getIsChromeFrame();
272 1
    }
273
274
    /**
275
     * @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...
276
     *
277
     * @return $this
278
     */
279
    public function setIsWebkit($isWebkit)
280 8
    {
281
        $this->isWebkit = (bool)$isWebkit;
282 8
283
        return $this;
284 8
    }
285
286
    /**
287
     * Used to determine if the browser is actually "chromeframe".
288
     *
289
     * @return bool
290 8
     */
291
    public function getIsWebkit()
292 8
    {
293
        if (!isset($this->name)) {
294
            BrowserDetector::detect($this, $this->getUserAgent());
295
        }
296
297
        return $this->isWebkit;
298
    }
299
300 1
    /**
301
     * @return bool
302 1
     */
303
    public function isWebkit()
304 1
    {
305
        return $this->getIsWebkit();
306
    }
307
308
    /**
309
     * @param bool $isFacebookWebView
310
     *
311
     * @return $this
312
     */
313
    public function setIsFacebookWebView($isFacebookWebView)
314
    {
315
        $this->isFacebookWebView = (bool) $isFacebookWebView;
316
317
        return $this;
318
    }
319
320
    /**
321
     * Used to determine if the browser is actually "facebook".
322
     *
323
     * @return bool
324
     */
325
    public function getIsFacebookWebView()
326
    {
327
        if (!isset($this->name)) {
328
            BrowserDetector::detect($this, $this->getUserAgent());
329
        }
330
331
        return $this->isFacebookWebView;
332
    }
333
334
    /**
335
     * @return bool
336
     */
337
    public function isFacebookWebView()
338
    {
339
        return $this->getIsFacebookWebView();
340
    }
341
342
    /**
343
     * @param bool $isTwitterWebView
344
     *
345
     * @return $this
346
     */
347
    public function setIsTwitterWebView($isTwitterWebView)
348
    {
349
        $this->isTwitterWebView = (bool) $isTwitterWebView;
350
351
        return $this;
352
    }
353
354
    /**
355
     * Used to determine if the browser is actually "Twitter".
356
     *
357
     * @return bool
358
     */
359
    public function getIsTwitterWebView()
360
    {
361
        if (!isset($this->name)) {
362
            BrowserDetector::detect($this, $this->getUserAgent());
363
        }
364
365
        return $this->isTwitterWebView;
366
    }
367
368
    /**
369
     * @return bool
370
     */
371
    public function isTwitterWebView()
372
    {
373
        return $this->getIsTwitterWebView();
374
    }
375
376
    /**
377
     * @param UserAgent $userAgent
378
     *
379
     * @return $this
380
     */
381
    public function setUserAgent(UserAgent $userAgent)
382
    {
383
        $this->userAgent = $userAgent;
384
385
        return $this;
386
    }
387
388
    /**
389
     * @return UserAgent
390
     */
391
    public function getUserAgent()
392
    {
393
        return $this->userAgent;
394
    }
395
396
    /**
397
     * @param bool
398
     *
399
     * @return $this
400
     */
401
    public function setIsCompatibilityMode($isCompatibilityMode)
402
    {
403
        $this->isCompatibilityMode = $isCompatibilityMode;
404
405
        return $this;
406
    }
407
408
    /**
409
     * @return bool
410
     */
411
    public function isCompatibilityMode()
412
    {
413
        return $this->isCompatibilityMode;
414
    }
415
416
    /**
417
     * Render pages outside of IE's compatibility mode.
418
     */
419
    public function endCompatibilityMode()
420
    {
421
        header('X-UA-Compatible: IE=edge');
422
    }
423
}
424