Completed
Push — master ( 6e8bed...47806b )
by Gabriel
03:00
created

OsDetector::detect()   D

Complexity

Conditions 17
Paths 17

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 17

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 31
ccs 23
cts 23
cp 1
rs 4.9807
cc 17
eloc 23
nc 17
nop 2
crap 17

How to fix   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 OsDetector implements DetectorInterface
6
{
7
    /**
8
     * Determine the user's operating system.
9
     *
10
     * @param Os $os
11
     * @param UserAgent $userAgent
12
     *
13
     * @return bool
14
     */
15 9
    public static function detect(Os $os, UserAgent $userAgent)
16
    {
17 9
        $os->setName($os::UNKNOWN);
18 9
        $os->setVersion($os::VERSION_UNKNOWN);
19 9
        $os->setIsMobile(false);
20
21 9
        self::checkMobileBrowsers($os, $userAgent);
22
23
        return (
24
            // Chrome OS before OS X
25 9
            self::checkChromeOs($os, $userAgent) ||
26
            // iOS before OS X
27 9
            self::checkIOS($os, $userAgent) ||
28 7
            self::checkOSX($os, $userAgent) ||
29 5
            self::checkSymbOS($os, $userAgent) ||
30 5
            self::checkWindows($os, $userAgent) ||
31 2
            self::checkWindowsPhone($os, $userAgent) ||
32 2
            self::checkFreeBSD($os, $userAgent) ||
33 2
            self::checkOpenBSD($os, $userAgent) ||
34 2
            self::checkNetBSD($os, $userAgent) ||
35 2
            self::checkOpenSolaris($os, $userAgent) ||
36 2
            self::checkSunOS($os, $userAgent) ||
37 2
            self::checkOS2($os, $userAgent) ||
38 2
            self::checkBeOS($os, $userAgent) ||
39
            // Android before Linux
40 2
            self::checkAndroid($os, $userAgent) ||
41 2
            self::checkLinux($os, $userAgent) ||
42 2
            self::checkNokia($os, $userAgent) ||
43 2
            self::checkBlackBerry($os, $userAgent)
44 9
        );
45
    }
46
47
    /**
48
     * Determine if the user's browser is on a mobile device.
49
     *
50
     * @param Os $os
51
     * @param UserAgent $userAgent
52
     *
53
     * @return bool
54
     */
55 9
    public static function checkMobileBrowsers(Os $os, UserAgent $userAgent)
56
    {
57
        // Check for Opera Mini
58 9
        if (stripos($userAgent->getUserAgentString(), 'opera mini') !== false) {
59
            $os->setIsMobile(true);
60
        } // Set is mobile for Pocket IE
61 9
        elseif (stripos($userAgent->getUserAgentString(), 'mspie') !== false || stripos($userAgent->getUserAgentString(), 'pocket') !== false) {
62
            $os->setIsMobile(true);
63
        }
64 9
    }
65
66
    /**
67
     * Determine if the user's operating system is iOS.
68
     *
69
     * @param Os $os
70
     * @param UserAgent $userAgent
71
     *
72
     * @return bool
73
     */
74 9
    private static function checkIOS(Os $os, UserAgent $userAgent)
75
    {
76 9
        if (stripos($userAgent->getUserAgentString(), 'CPU OS') !== false || stripos($userAgent->getUserAgentString(), 'iPhone OS') !== false && stripos($userAgent->getUserAgentString(), 'OS X')) {
77 3
            $os->setName($os::IOS);
78 3
            if (preg_match('/CPU( iPhone)? OS ([\d_]*)/i', $userAgent->getUserAgentString(), $matches)) {
79 3
                $os->setVersion(str_replace('_', '.', $matches[2]));
80 3
            }
81 3
            $os->setIsMobile(true);
82
83 3
            return true;
84
        }
85
86 7
        return false;
87
    }
88
89
    /**
90
     * Determine if the user's operating system is Chrome OS.
91
     *
92
     * @param Os $os
93
     * @param UserAgent $userAgent
94
     *
95
     * @return bool
96
     */
97 9 View Code Duplication
    private static function checkChromeOs(Os $os, UserAgent $userAgent)
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
    {
99 9
        if (stripos($userAgent->getUserAgentString(), 'CrOS') !== false) {
100 1
            $os->setName($os::CHROME_OS);
101 1
            if (preg_match('/Chrome\/([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
102 1
                $os->setVersion($matches[1]);
103 1
            }
104 1
            return true;
105
        }
106
107 9
        return false;
108
    }
109
110
    /**
111
     * Determine if the user's operating system is OS X.
112
     *
113
     * @param Os $os
114
     * @param UserAgent $userAgent
115
     *
116
     * @return bool
117
     */
118 7 View Code Duplication
    private static function checkOSX(Os $os, UserAgent $userAgent)
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...
119
    {
120 7
        if (stripos($userAgent->getUserAgentString(), 'OS X') !== false) {
121 3
            $os->setName($os::OSX);
122 3
            if (preg_match('/OS X ([\d\._]*)/i', $userAgent->getUserAgentString(), $matches)) {
123 3
                if (isset($matches[1])) {
124 3
                    $os->setVersion(str_replace('_', '.', $matches[1]));
125 3
                }
126 3
            }
127
128 3
            return true;
129
        }
130
131 5
        return false;
132
    }
133
134
    /**
135
     * Determine if the user's operating system is Windows.
136
     *
137
     * @param Os $os
138
     * @param UserAgent $userAgent
139
     *
140
     * @return bool
141
     */
142 5
    private static function checkWindows(Os $os, UserAgent $userAgent)
143
    {
144 5
        if (stripos($userAgent->getUserAgentString(), 'Windows NT') !== false) {
145 3
            $os->setName($os::WINDOWS);
146
            // Windows version
147 3
            if (preg_match('/Windows NT ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
148 3
                if (isset($matches[1])) {
149 3
                    switch (str_replace('_', '.', $matches[1])) {
150 3
                        case '6.3':
151
                            $os->setVersion('8.1');
152
                            break;
153 3
                        case '6.2':
154
                            $os->setVersion('8');
155
                            break;
156 3
                        case '6.1':
157 3
                            $os->setVersion('7');
158 3
                            break;
159 1
                        case '6.0':
160
                            $os->setVersion('Vista');
161
                            break;
162 1
                        case '5.2':
163 1
                        case '5.1':
164
                            $os->setVersion('XP');
165
                            break;
166 1
                        case '5.01':
167 1
                        case '5.0':
168
                            $os->setVersion('2000');
169
                            break;
170 1
                        case '4.0':
171
                            $os->setVersion('NT 4.0');
172
                            break;
173 1
                        default:
174 1
                            if ((float)$matches[1] >= 10.0) {
175 1
                                $os->setVersion((float)$matches[1]);
176 1
                            }
177 1
                            break;
178 3
                    }
179 3
                }
180 3
            }
181
182 3
            return true;
183
        } // Windows Me, Windows 98, Windows 95, Windows CE
184 2
        elseif (preg_match('/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i', $userAgent->getUserAgentString(), $matches)) {
185
            $os->setName($os::WINDOWS);
186
            switch (strtolower($matches[0])) {
187
                case 'windows 98; win 9x 4.90':
188
                    $os->setVersion('Me');
189
                    break;
190
                case 'windows 98':
191
                    $os->setVersion('98');
192
                    break;
193
                case 'windows 95':
194
                    $os->setVersion('95');
195
                    break;
196
                case 'windows ce':
197
                    $os->setVersion('CE');
198
                    break;
199
            }
200
201
            return true;
202
        }
203
204 2
        return false;
205
    }
206
    
207
    /**
208
     * Determine if the user's operating system is Windows Phone.
209
     *
210
     * @param Os $os
211
     * @param UserAgent $userAgent
212
     *
213
     * @return bool
214
     */
215 2 View Code Duplication
    private static function checkWindowsPhone(Os $os, UserAgent $userAgent)
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...
216
    {
217 2
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
218
            $os->setName($os::WINDOWS_PHONE);
219
            // Windows version
220
            if (preg_match('/Windows Phone ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
221
                if (isset($matches[1])) {
222
                    $os->setVersion((float)$matches[1]);
223
                }
224
            }
225
    
226
            return true;
227
        }
228 2
        return false;
229
    }
230
231
    /**
232
     * Determine if the user's operating system is SymbOS.
233
     *
234
     * @param Os $os
235
     * @param UserAgent $userAgent
236
     *
237
     * @return bool
238
     */
239 5
    private static function checkSymbOS(Os $os, UserAgent $userAgent)
240
    {
241 5
        if (stripos($userAgent->getUserAgentString(), 'SymbOS') !== false) {
242
            $os->setName($os::SYMBOS);
243
244
            return true;
245
        }
246
247 5
        return false;
248
    }
249
250
    /**
251
     * Determine if the user's operating system is Linux.
252
     *
253
     * @param Os $os
254
     * @param UserAgent $userAgent
255
     *
256
     * @return bool
257
     */
258 2 View Code Duplication
    private static function checkLinux(Os $os, UserAgent $userAgent)
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...
259
    {
260 2
        if (stripos($userAgent->getUserAgentString(), 'Linux') !== false) {
261
            $os->setVersion($os::VERSION_UNKNOWN);
262
            $os->setName($os::LINUX);
263
264
            return true;
265
        }
266
267 2
        return false;
268
    }
269
270
    /**
271
     * Determine if the user's operating system is Nokia.
272
     *
273
     * @param Os $os
274
     * @param UserAgent $userAgent
275
     *
276
     * @return bool
277
     */
278 2 View Code Duplication
    private static function checkNokia(Os $os, UserAgent $userAgent)
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...
279
    {
280 2
        if (stripos($userAgent->getUserAgentString(), 'Nokia') !== false) {
281
            $os->setVersion($os::VERSION_UNKNOWN);
282
            $os->setName($os::NOKIA);
283
            $os->setIsMobile(true);
284
285
            return true;
286
        }
287
288 2
        return false;
289
    }
290
291
    /**
292
     * Determine if the user's operating system is BlackBerry.
293
     *
294
     * @param Os $os
295
     * @param UserAgent $userAgent
296
     *
297
     * @return bool
298
     */
299 2 View Code Duplication
    private static function checkBlackBerry(Os $os, UserAgent $userAgent)
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...
300
    {
301 2
        if (stripos($userAgent->getUserAgentString(), 'BlackBerry') !== false) {
302 1
            $os->setVersion($os::VERSION_UNKNOWN);
303 1
            $os->setName($os::BLACKBERRY);
304 1
            $os->setIsMobile(true);
305
306 1
            return true;
307
        }
308
309 1
        return false;
310
    }
311
312
    /**
313
     * Determine if the user's operating system is Android.
314
     *
315
     * @param Os $os
316
     * @param UserAgent $userAgent
317
     *
318
     * @return bool
319
     */
320 2
    private static function checkAndroid(Os $os, UserAgent $userAgent)
321
    {
322 2
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
323
            if (preg_match('/Android ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) {
324
                if (isset($matches[1])) {
325
                    $os->setVersion($matches[1]);
326
                }
327
            } else {
328
                $os->setVersion($os::VERSION_UNKNOWN);
329
            }
330
            $os->setName($os::ANDROID);
331
            $os->setIsMobile(true);
332
333
            return true;
334
        }
335
336 2
        return false;
337
    }
338
339
    /**
340
     * Determine if the user's operating system is FreeBSD.
341
     *
342
     * @param Os $os
343
     * @param UserAgent $userAgent
344
     *
345
     * @return bool
346
     */
347 2 View Code Duplication
    private static function checkFreeBSD(Os $os, UserAgent $userAgent)
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...
348
    {
349 2
        if (stripos($userAgent->getUserAgentString(), 'FreeBSD') !== false) {
350
            $os->setVersion($os::VERSION_UNKNOWN);
351
            $os->setName($os::FREEBSD);
352
353
            return true;
354
        }
355
356 2
        return false;
357
    }
358
359
    /**
360
     * Determine if the user's operating system is OpenBSD.
361
     *
362
     * @param Os $os
363
     * @param UserAgent $userAgent
364
     *
365
     * @return bool
366
     */
367 2 View Code Duplication
    private static function checkOpenBSD(Os $os, UserAgent $userAgent)
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...
368
    {
369 2
        if (stripos($userAgent->getUserAgentString(), 'OpenBSD') !== false) {
370
            $os->setVersion($os::VERSION_UNKNOWN);
371
            $os->setName($os::OPENBSD);
372
373
            return true;
374
        }
375
376 2
        return false;
377
    }
378
379
    /**
380
     * Determine if the user's operating system is SunOS.
381
     *
382
     * @param Os $os
383
     * @param UserAgent $userAgent
384
     *
385
     * @return bool
386
     */
387 2 View Code Duplication
    private static function checkSunOS(Os $os, UserAgent $userAgent)
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...
388
    {
389 2
        if (stripos($userAgent->getUserAgentString(), 'SunOS') !== false) {
390
            $os->setVersion($os::VERSION_UNKNOWN);
391
            $os->setName($os::SUNOS);
392
393
            return true;
394
        }
395
396 2
        return false;
397
    }
398
399
    /**
400
     * Determine if the user's operating system is NetBSD.
401
     *
402
     * @param Os $os
403
     * @param UserAgent $userAgent
404
     *
405
     * @return bool
406
     */
407 2 View Code Duplication
    private static function checkNetBSD(Os $os, UserAgent $userAgent)
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...
408
    {
409 2
        if (stripos($userAgent->getUserAgentString(), 'NetBSD') !== false) {
410
            $os->setVersion($os::VERSION_UNKNOWN);
411
            $os->setName($os::NETBSD);
412
413
            return true;
414
        }
415
416 2
        return false;
417
    }
418
419
    /**
420
     * Determine if the user's operating system is OpenSolaris.
421
     *
422
     * @param Os $os
423
     * @param UserAgent $userAgent
424
     *
425
     * @return bool
426
     */
427 2 View Code Duplication
    private static function checkOpenSolaris(Os $os, UserAgent $userAgent)
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...
428
    {
429 2
        if (stripos($userAgent->getUserAgentString(), 'OpenSolaris') !== false) {
430
            $os->setVersion($os::VERSION_UNKNOWN);
431
            $os->setName($os::OPENSOLARIS);
432
433
            return true;
434
        }
435
436 2
        return false;
437
    }
438
439
    /**
440
     * Determine if the user's operating system is OS2.
441
     *
442
     * @param Os $os
443
     * @param UserAgent $userAgent
444
     *
445
     * @return bool
446
     */
447 2 View Code Duplication
    private static function checkOS2(Os $os, UserAgent $userAgent)
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...
448
    {
449 2
        if (stripos($userAgent->getUserAgentString(), 'OS\/2') !== false) {
450
            $os->setVersion($os::VERSION_UNKNOWN);
451
            $os->setName($os::OS2);
452
453
            return true;
454
        }
455
456 2
        return false;
457
    }
458
459
    /**
460
     * Determine if the user's operating system is BeOS.
461
     *
462
     * @param Os $os
463
     * @param UserAgent $userAgent
464
     *
465
     * @return bool
466
     */
467 2 View Code Duplication
    private static function checkBeOS(Os $os, UserAgent $userAgent)
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...
468
    {
469 2
        if (stripos($userAgent->getUserAgentString(), 'BeOS') !== false) {
470
            $os->setVersion($os::VERSION_UNKNOWN);
471
            $os->setName($os::BEOS);
472
473
            return true;
474
        }
475
476 2
        return false;
477
    }
478
}
479