| Conditions | 3 |
| Paths | 3 |
| Total Lines | 58 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 176 | public function parse($userAgent, array $headers = []) |
||
| 177 | { |
||
| 178 | $parser = $this->getParser(); |
||
| 179 | $parser->setHttpHeaders($headers); |
||
| 180 | $parser->setUserAgent($userAgent); |
||
| 181 | |||
| 182 | /* |
||
| 183 | * Since Mobile_Detect to a regex comparison on every call |
||
| 184 | * We cache it here for all checks and hydration |
||
| 185 | */ |
||
| 186 | $browserName = $parser->browser(); |
||
| 187 | $osName = $parser->platform(); |
||
| 188 | |||
| 189 | $resultCache = [ |
||
| 190 | 'browserName' => $browserName, |
||
| 191 | 'browserVersion' => $parser->version($browserName), |
||
| 192 | |||
| 193 | 'osName' => $osName, |
||
| 194 | 'osVersion' => $parser->version($osName), |
||
| 195 | |||
| 196 | 'deviceModel' => $parser->device(), |
||
| 197 | 'isMobile' => $parser->isMobile(), |
||
| 198 | |||
| 199 | 'isRobot' => $parser->isRobot(), |
||
| 200 | 'botName' => $parser->robot(), |
||
| 201 | ]; |
||
| 202 | |||
| 203 | /* |
||
| 204 | * No result found? |
||
| 205 | */ |
||
| 206 | if ($this->hasResult($resultCache) !== true) { |
||
| 207 | throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
||
| 208 | } |
||
| 209 | |||
| 210 | /* |
||
| 211 | * Hydrate the model |
||
| 212 | */ |
||
| 213 | $result = new Model\UserAgent(); |
||
| 214 | $result->setProviderResultRaw($resultCache); |
||
| 215 | |||
| 216 | /* |
||
| 217 | * Bot detection |
||
| 218 | */ |
||
| 219 | if ($resultCache['isRobot'] === true) { |
||
| 220 | $this->hydrateBot($result->getBot(), $resultCache); |
||
| 221 | |||
| 222 | return $result; |
||
| 223 | } |
||
| 224 | |||
| 225 | /* |
||
| 226 | * hydrate the result |
||
| 227 | */ |
||
| 228 | $this->hydrateBrowser($result->getBrowser(), $resultCache); |
||
| 229 | $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultCache); |
||
| 230 | $this->hydrateDevice($result->getDevice(), $resultCache); |
||
| 231 | |||
| 232 | return $result; |
||
| 233 | } |
||
| 234 | } |
||
| 235 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.