Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
13 | class Detector |
||
14 | { |
||
15 | /** |
||
16 | * @return string Path to directory with xml data files |
||
17 | */ |
||
18 | public function getPathToData() |
||
22 | |||
23 | /** |
||
24 | * @return array Xml data object |
||
25 | */ |
||
26 | public function getXmlData() |
||
30 | |||
31 | /** |
||
32 | * @param string $PathToData |
||
33 | */ |
||
34 | public function setPathToData($PathToData) |
||
38 | |||
39 | /** |
||
40 | * @param array $xmlData Xml data object |
||
41 | */ |
||
42 | public function setXmlData($xmlData) |
||
46 | /** @var string Path to directory with xml data */ |
||
47 | private $PathToData; |
||
48 | |||
49 | /** @var array Xml Data */ |
||
50 | private $xmlData; |
||
51 | |||
52 | /** |
||
53 | * Detector constructor. |
||
54 | * @param string $pathToData Path to directory with xml data files |
||
55 | */ |
||
56 | private function __construct($pathToData='auto') |
||
71 | |||
72 | public static function analyse($uaString='UA', $pathToData='auto') |
||
137 | |||
138 | /** |
||
139 | * @param array $xmlData Xml data array |
||
140 | * @param string $key Key in data array |
||
141 | * @param string $uaString User agent |
||
142 | * @return \SimpleXMLElement xml element |
||
143 | */ |
||
144 | private static function analysePart($xmlData,$key,$uaString) |
||
157 | |||
158 | /** |
||
159 | * @param \SimpleXMLElement $xmlItem xmlItem |
||
160 | * @param string $uaString User agent |
||
161 | * @return string Version |
||
162 | */ |
||
163 | private static function getVersion(\SimpleXMLElement $xmlItem,$uaString) |
||
191 | |||
192 | /** |
||
193 | * @param $version Windows number version |
||
194 | * @return string Windows version |
||
195 | */ |
||
196 | private static function getWindowsVersion($version) |
||
210 | } |
||
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: