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 |
||
18 | class ParseXml { |
||
19 | |||
20 | private $xml; |
||
21 | private $city; |
||
|
|||
22 | |||
23 | |||
24 | /** |
||
25 | * @param $fileName |
||
26 | */ |
||
27 | public function __construct($fileName) |
||
31 | |||
32 | /** |
||
33 | * Parse weather from XML and return Model City |
||
34 | * @return bool|City |
||
35 | */ |
||
36 | public function parse() { |
||
37 | |||
38 | // check xml load |
||
39 | if(!$this->xml) |
||
40 | return false; |
||
41 | |||
42 | $city = new City(); |
||
43 | View Code Duplication | foreach($this->xml->attributes() as $key => $value) { |
|
44 | if(isset(Mapping::mapCity()[$key])) |
||
45 | $city->{Mapping::mapCity()[$key]} = (string) $value; |
||
46 | } |
||
47 | |||
48 | $fact = new Detail(); |
||
49 | View Code Duplication | foreach ($this->xml->fact[0] as $key => $value) { |
|
50 | if(isset(Mapping::mapDetail()[$key])) { |
||
51 | $fact->{Mapping::mapDetail()[$key]} = (string) $value; |
||
52 | } |
||
53 | |||
54 | } |
||
55 | $city->fact = $fact; |
||
56 | |||
57 | $fact = new Detail(); |
||
58 | View Code Duplication | foreach ($this->xml->yesterday[0] as $key => $value) { |
|
59 | if(isset(Mapping::mapDetail()[$key])) { |
||
60 | $fact->{Mapping::mapDetail()[$key]} = (string) $value; |
||
61 | } |
||
62 | |||
63 | } |
||
64 | $city->yesterday = $fact; |
||
65 | |||
66 | $days = array(); |
||
67 | foreach($this->xml->day as $value) { |
||
68 | $day = new Day(); |
||
69 | foreach ($value as $key => $value1) { |
||
70 | if(isset(Mapping::mapDay()[$key])) { |
||
71 | $day->{Mapping::mapDay()[$key]} = (string) $value1; |
||
72 | } |
||
73 | //var_dump($value1->attributes()["typeid"]); |
||
74 | |||
75 | |||
76 | } |
||
77 | foreach ($value->day_part as $key2 => $value2) { |
||
78 | $detail = new Detail(); |
||
79 | foreach ($value2 as $key3 => $value3) { |
||
80 | if(isset(Mapping::mapDetail()[$key3])) { |
||
81 | $detail->{Mapping::mapDetail()[$key3]} = (string) $value3; |
||
82 | } |
||
83 | if(isset(Mapping::mapDetail()["inner"][$key3])) { |
||
84 | $detail->{Mapping::mapDetail()["inner"][$key3]} = (string) $value3->avg; |
||
85 | } |
||
86 | } |
||
87 | $day->{$value2->attributes()["type"]} = $detail; |
||
88 | } |
||
89 | $days[(string)$value->attributes()["date"]] = $day; |
||
90 | } |
||
91 | $city->days = $days; |
||
92 | |||
93 | return $city; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * static function encode string utf8 to cp1251 |
||
98 | * @param $value |
||
99 | * @return string |
||
100 | */ |
||
101 | public static function encoding($value) { |
||
104 | |||
105 | |||
106 | |||
107 | |||
108 | |||
109 | |||
110 | } |
This check marks private properties in classes that are never used. Those properties can be removed.