| Conditions | 14 |
| Paths | 48 |
| Total Lines | 65 |
| Code Lines | 40 |
| Lines | 16 |
| Ratio | 24.62 % |
| 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 |
||
| 72 | public static function analyse($uaString='UA', $pathToData='auto') |
||
|
|
|||
| 73 | { |
||
| 74 | $ua = $uaString; |
||
| 75 | if($uaString == 'UA') |
||
| 76 | { |
||
| 77 | $ua = $_SERVER['HTTP_USER_AGENT']; |
||
| 78 | } |
||
| 79 | |||
| 80 | $detector = new Detector($pathToData); |
||
| 81 | $xml = $detector->getXmlData(); |
||
| 82 | $data = array(); |
||
| 83 | foreach($xml as $key => $item) |
||
| 84 | { |
||
| 85 | $data[$key] = self::analysePart($xml,$key,$ua); |
||
| 86 | } |
||
| 87 | |||
| 88 | $detectorResult = new DetectorResult(); |
||
| 89 | $detectorResult->uaString = $ua; |
||
| 90 | $isRobot = false; |
||
| 91 | |||
| 92 | foreach($data as $key => $result) |
||
| 93 | { |
||
| 94 | if(!$isRobot) |
||
| 95 | { |
||
| 96 | switch ($key) |
||
| 97 | { |
||
| 98 | case 'robot': |
||
| 99 | if($result !== null) |
||
| 100 | { |
||
| 101 | $robot = new Robot($result); |
||
| 102 | $detectorResult->Robot = $robot; |
||
| 103 | if ($robot->getName() != 'N\A') { |
||
| 104 | $isRobot = true; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | break; |
||
| 108 | View Code Duplication | case 'os': |
|
| 109 | if($result !== null) |
||
| 110 | { |
||
| 111 | $os = new OS($result); |
||
| 112 | $os->setVersion(self::getVersion($result, $ua)); |
||
| 113 | $detectorResult->OS = $os; |
||
| 114 | } |
||
| 115 | break; |
||
| 116 | case 'device': |
||
| 117 | if($result !== null) |
||
| 118 | { |
||
| 119 | $device = new Device($result); |
||
| 120 | $detectorResult->Device = $device; |
||
| 121 | } |
||
| 122 | break; |
||
| 123 | View Code Duplication | case 'browser': |
|
| 124 | if($result !== null) |
||
| 125 | { |
||
| 126 | $browser = new Browser($result); |
||
| 127 | $browser->setVersion(self::getVersion($result, $ua)); |
||
| 128 | $detectorResult->Browser = $browser; |
||
| 129 | } |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return $detectorResult; |
||
| 136 | } |
||
| 137 | |||
| 211 |
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: