Test Failed
Pull Request — master (#76)
by
unknown
05:20
created

Browser::getIsWebkit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
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
        //The regex for the Firefox version lets through a linebreak, causing the test to fail
158
        $this->version = str_replace("\n","",(string)$version);
159
160
        return $this;
161
    }
162 7
163
    /**
164 7
     * The version of the browser.
165
     *
166
     * @return string
167
     */
168 7
    public function getVersion()
169
    {
170
        if (!isset($this->name)) {
171
            BrowserDetector::detect($this, $this->getUserAgent());
172
        }
173
174
        return (string) $this->version;
175
    }
176
177
    /**
178
     * Detects scripted agents (robots / bots)
179
     * Returns a resolved ScriptedAgent object if detected.
180
     * Otherwise returns false.
181
     *
182
     * @return ScriptedAgent|bool
183
     */
184
    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...
185
    {
186
        $ua = $this->getUserAgent()->getUserAgentString();
187
        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...
188
            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...
189
            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...
190
            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...
191
            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...
192
            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...
193
            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...
194
            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...
195
            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...
196
            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...
197
            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...
198
            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...
199
            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...
200
            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...
201
            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...
202
            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...
203
            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...
204
            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...
205
            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...
206
            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...
207
            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...
208
            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...
209
            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...
210
            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...
211
            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...
212
            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...
213
            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...
214
            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...
215
            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...
216
            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...
217
            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...
218
            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...
219
            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...
220
            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...
221
            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...
222
            (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...
223
        )
224
        {
225
            $scriptedAgent = new ScriptedAgent($ua);
226
            if ($scriptedAgent->getName()==ScriptedAgent::UNKNOWN)
227
            {
228
                return false;
229
            }
230
            else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
231
            {
232
                return $scriptedAgent;
233
            }
234
        }
235
        else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
236
        {
237
            return false;
238
        }
239
    }
240
241
    /**
242
     * @param bool $isChromeFrame
243
     *
244
     * @return $this
245
     */
246 1
    public function setIsChromeFrame($isChromeFrame)
247
    {
248 1
        $this->isChromeFrame = (bool)$isChromeFrame;
249
250 1
        return $this;
251
    }
252
253
    /**
254
     * Used to determine if the browser is actually "chromeframe".
255
     *
256
     * @return bool
257
     */
258 1
    public function getIsChromeFrame()
259
    {
260 1
        if (!isset($this->name)) {
261 1
            BrowserDetector::detect($this, $this->getUserAgent());
262 1
        }
263
264 1
        return $this->isChromeFrame;
265
    }
266
267
    /**
268
     * @return bool
269
     */
270 1
    public function isChromeFrame()
271
    {
272 1
        return $this->getIsChromeFrame();
273
    }
274
275
    /**
276
     * @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...
277
     *
278
     * @return $this
279
     */
280 8
    public function setIsWebkit($isWebkit)
281
    {
282 8
        $this->isWebkit = (bool)$isWebkit;
283
284 8
        return $this;
285
    }
286
287
    /**
288
     * Used to determine if the browser is actually "chromeframe".
289
     *
290 8
     * @return bool
291
     */
292 8
    public function getIsWebkit()
293
    {
294
        if (!isset($this->name)) {
295
            BrowserDetector::detect($this, $this->getUserAgent());
296
        }
297
298
        return $this->isWebkit;
299
    }
300 1
301
    /**
302 1
     * @return bool
303
     */
304 1
    public function isWebkit()
305
    {
306
        return $this->getIsWebkit();
307
    }
308
309
    /**
310
     * @param bool $isFacebookWebView
311
     *
312
     * @return $this
313
     */
314
    public function setIsFacebookWebView($isFacebookWebView)
315
    {
316
        $this->isFacebookWebView = (bool) $isFacebookWebView;
317
318
        return $this;
319
    }
320
321
    /**
322
     * Used to determine if the browser is actually "facebook".
323
     *
324
     * @return bool
325
     */
326
    public function getIsFacebookWebView()
327
    {
328
        if (!isset($this->name)) {
329
            BrowserDetector::detect($this, $this->getUserAgent());
330
        }
331
332
        return $this->isFacebookWebView;
333
    }
334
335
    /**
336
     * @return bool
337
     */
338
    public function isFacebookWebView()
339
    {
340
        return $this->getIsFacebookWebView();
341
    }
342
343
    /**
344
     * @param bool $isTwitterWebView
345
     *
346
     * @return $this
347
     */
348
    public function setIsTwitterWebView($isTwitterWebView)
349
    {
350
        $this->isTwitterWebView = (bool) $isTwitterWebView;
351
352
        return $this;
353
    }
354
355
    /**
356
     * Used to determine if the browser is actually "Twitter".
357
     *
358
     * @return bool
359
     */
360
    public function getIsTwitterWebView()
361
    {
362
        if (!isset($this->name)) {
363
            BrowserDetector::detect($this, $this->getUserAgent());
364
        }
365
366
        return $this->isTwitterWebView;
367
    }
368
369
    /**
370
     * @return bool
371
     */
372
    public function isTwitterWebView()
373
    {
374
        return $this->getIsTwitterWebView();
375
    }
376
377
    /**
378
     * @param UserAgent $userAgent
379
     *
380
     * @return $this
381
     */
382
    public function setUserAgent(UserAgent $userAgent)
383
    {
384
        $this->userAgent = $userAgent;
385
386
        return $this;
387
    }
388
389
    /**
390
     * @return UserAgent
391
     */
392
    public function getUserAgent()
393
    {
394
        return $this->userAgent;
395
    }
396
397
    /**
398
     * @param bool
399
     *
400
     * @return $this
401
     */
402
    public function setIsCompatibilityMode($isCompatibilityMode)
403
    {
404
        $this->isCompatibilityMode = $isCompatibilityMode;
405
406
        return $this;
407
    }
408
409
    /**
410
     * @return bool
411
     */
412
    public function isCompatibilityMode()
413
    {
414
        return $this->isCompatibilityMode;
415
    }
416
417
    /**
418
     * Render pages outside of IE's compatibility mode.
419
     */
420
    public function endCompatibilityMode()
421
    {
422
        header('X-UA-Compatible: IE=edge');
423
    }
424
}
425