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