| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sinergi\BrowserDetector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class DeviceDetector implements DetectorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * Determine the user's device. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param Device $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @param UserAgent $userAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 2 |  |     public static function detect(Device $device, UserAgent $userAgent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 2 |  |         $device->setName($device::UNKNOWN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         return ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 2 |  |             self::checkIpad($device, $userAgent) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 2 |  |             self::checkIphone($device, $userAgent) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 2 |  |             self::checkWindowsPhone($device, $userAgent) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 2 |  |             self::checkSamsungPhone($device, $userAgent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Determine if the device is iPad. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Device $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param UserAgent $userAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 33 | 2 | View Code Duplication |     private static function checkIpad(Device $device, UserAgent $userAgent) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |         if (stripos($userAgent->getUserAgentString(), 'ipad') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |             $device->setName(Device::IPAD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Determine if the device is iPhone. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param Device $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param UserAgent $userAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 50 | 2 | View Code Duplication |     private static function checkIphone(Device $device, UserAgent $userAgent) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |         if (stripos($userAgent->getUserAgentString(), 'iphone;') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |             $device->setName(Device::IPHONE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Determine if the device is Windows Phone. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param Device $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param UserAgent $userAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 67 | 2 | View Code Duplication |     private static function checkWindowsPhone(Device $device, UserAgent $userAgent) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             if (preg_match('/Microsoft; (Lumia [^)]*)\)/', $userAgent->getUserAgentString(), $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                 $device->setName($matches[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $device->setName($device::WINDOWS_PHONE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Determine if the device is Windows Phone. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param Device $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param UserAgent $userAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 | 2 |  |     private static function checkSamsungPhone(Device $device, UserAgent $userAgent) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 | 2 |  |             if (preg_match('/SAMSUNG SM-([^ ]*)/i', $userAgent->getUserAgentString(), $matches)) { | 
            
                                                        
            
                                    
            
            
                | 91 | 1 |  |                 $device->setName(str_ireplace('SAMSUNG', 'Samsung', $matches[0])); | 
            
                                                        
            
                                    
            
            
                | 92 | 1 |  |                 return true; | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 | 2 |  |         return false; | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                        
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.