| Conditions | 14 |
| Paths | 48 |
| Total Lines | 82 |
| Code Lines | 50 |
| Lines | 26 |
| Ratio | 31.71 % |
| Changes | 3 | ||
| 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 |
||
| 75 | public static function analyse($uaString='UA', $pathToData='auto') |
||
|
|
|||
| 76 | { |
||
| 77 | $ua = $uaString; |
||
| 78 | if($uaString == 'UA') |
||
| 79 | { |
||
| 80 | $ua = $_SERVER['HTTP_USER_AGENT']; |
||
| 81 | } |
||
| 82 | |||
| 83 | $detector = new Detector($pathToData); |
||
| 84 | $xml = $detector->getXmlData(); |
||
| 85 | $data = array(); |
||
| 86 | foreach($xml as $key => $item) |
||
| 87 | { |
||
| 88 | $data[$key] = self::analysePart($xml,$key,$ua); |
||
| 89 | } |
||
| 90 | |||
| 91 | $detectorResult = new DetectorResult(); |
||
| 92 | $detectorResult->uaString = $ua; |
||
| 93 | $isRobot = false; |
||
| 94 | |||
| 95 | foreach($data as $key => $result) |
||
| 96 | { |
||
| 97 | if(!$isRobot) |
||
| 98 | { |
||
| 99 | switch ($key) |
||
| 100 | { |
||
| 101 | case 'robot': |
||
| 102 | if($result !== null) |
||
| 103 | { |
||
| 104 | $robot = new Robot($result); |
||
| 105 | $detectorResult->Robot = $robot; |
||
| 106 | if ($robot->getName() != 'N\A') { |
||
| 107 | $isRobot = true; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | break; |
||
| 111 | View Code Duplication | case 'os': |
|
| 112 | if($result !== null) |
||
| 113 | { |
||
| 114 | $os = new OS($result); |
||
| 115 | $os->setVersion(self::getVersion($result, $ua)); |
||
| 116 | $detectorResult->OS = $os; |
||
| 117 | } |
||
| 118 | else |
||
| 119 | { |
||
| 120 | $os = OS::initEmpty(); |
||
| 121 | $detectorResult->OS = $os; |
||
| 122 | } |
||
| 123 | break; |
||
| 124 | case 'device': |
||
| 125 | if($result !== null) |
||
| 126 | { |
||
| 127 | $device = new Device($result); |
||
| 128 | $detectorResult->Device = $device; |
||
| 129 | } |
||
| 130 | else |
||
| 131 | { |
||
| 132 | $device = Device::initEmpty(); |
||
| 133 | $detectorResult->Device = $device; |
||
| 134 | } |
||
| 135 | break; |
||
| 136 | View Code Duplication | case 'browser': |
|
| 137 | if($result !== null) |
||
| 138 | { |
||
| 139 | $browser = new Browser($result); |
||
| 140 | $browser->setVersion(self::getVersion($result, $ua)); |
||
| 141 | $detectorResult->Browser = $browser; |
||
| 142 | } |
||
| 143 | else |
||
| 144 | { |
||
| 145 | $browser = Browser::initEmpty(); |
||
| 146 | $detectorResult->Browser = $browser; |
||
| 147 | } |
||
| 148 | break; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $detectorResult = self::checkRules($detector,$detectorResult); |
||
| 154 | |||
| 155 | return $detectorResult; |
||
| 156 | } |
||
| 157 | |||
| 259 |
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: