Conditions | 20 |
Paths | 281 |
Total Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
77 | public static function getBrowserInfo() { |
||
78 | $browserFullName = $browserShortName = $browserVersion = null; |
||
79 | $userAgent = self::getUserAgent(); |
||
80 | |||
81 | if (empty($userAgent)) { |
||
82 | return null; |
||
83 | } |
||
84 | |||
85 | // Browser identifiers |
||
86 | $identifiers = ['Version', 'other']; |
||
87 | |||
88 | // Detect browser |
||
89 | if (preg_match('/MSIE|Trident/i', $userAgent) && !preg_match('/Opera|OPR/i', $userAgent)) { |
||
90 | $browserFullName = 'Internet Explorer'; |
||
91 | $browserShortName = "MSIE"; |
||
92 | $identifiers[] = "Trident"; |
||
93 | } elseif (preg_match('/Firefox/i', $userAgent)) { |
||
94 | $browserFullName = 'Mozilla Firefox'; |
||
95 | $browserShortName = "Firefox"; |
||
96 | } elseif (preg_match('/Chrome/i', $userAgent) && !preg_match('/Opera|OPR/i', $userAgent)) { |
||
97 | $browserFullName = 'Google Chrome'; |
||
98 | $browserShortName = "Chrome"; |
||
99 | } elseif (preg_match('/Safari|AppleWebKit/i', $userAgent) && !preg_match('/Opera|OPR/i', $userAgent)) { |
||
100 | $browserFullName = 'Apple Safari'; |
||
101 | $browserShortName = "Safari"; |
||
102 | } elseif (preg_match('/Opera|OPR/i', $userAgent)) { |
||
103 | $browserFullName = 'Opera'; |
||
104 | $browserShortName = "Opera"; |
||
105 | $identifiers[] = "OPR"; |
||
106 | } elseif (preg_match('/Netscape/i', $userAgent)) { |
||
107 | $browserFullName = 'Netscape'; |
||
108 | $browserShortName = "Netscape"; |
||
109 | } |
||
110 | |||
111 | /* |
||
112 | * Detect browser version number |
||
113 | */ |
||
114 | $identifiers[] = $browserShortName; |
||
115 | self::$pattern = '#(?<browser>' . implode('|', $identifiers) . |
||
|
|||
116 | ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; |
||
117 | if (!preg_match_all(self::$pattern, $userAgent, $matches)) { |
||
118 | // we have no matching number just continue |
||
119 | } |
||
120 | |||
121 | // see how many we have |
||
122 | $chunks = count($matches['browser']); |
||
123 | if ($chunks > 1) { //we will have two since we are not using 'other' argument yet |
||
124 | //see if version is before or after the name |
||
125 | if (strripos($userAgent, "Version") < strripos($userAgent, $browserShortName) |
||
126 | && isset($matches['version'][0])) { |
||
127 | $browserVersion = $matches['version'][0]; |
||
128 | } elseif (isset($matches['version'][1])) { |
||
129 | $browserVersion = $matches['version'][1]; |
||
130 | } |
||
131 | } elseif (isset($matches['version'][0])) { |
||
132 | $browserVersion = $matches['version'][0]; |
||
133 | } |
||
134 | |||
135 | // New IE version detection |
||
136 | if (isset($matches['browser'][0]) && $matches['browser'][0] == 'Trident') { |
||
137 | $browserVersion = sprintf("%.1f", (int)$browserVersion + 4); |
||
138 | } |
||
139 | |||
140 | // check if we have a number |
||
141 | if (empty($browserVersion)) { |
||
142 | $browserVersion = null; |
||
143 | } |
||
144 | |||
145 | return [ |
||
146 | 'full_name' => $browserFullName, |
||
147 | 'short_name' => $browserShortName, |
||
148 | 'version' => $browserVersion |
||
149 | ]; |
||
150 | } |
||
151 | |||
162 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..