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 |
||
12 | class City { |
||
13 | |||
14 | public $city; |
||
15 | public $country; |
||
16 | public $region; |
||
17 | |||
18 | public $fact; |
||
19 | public $yesterday; |
||
20 | |||
21 | public $days; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * Get current model day |
||
26 | * @return string |
||
27 | */ |
||
28 | View Code Duplication | public function getCurrentDay() { |
|
35 | |||
36 | /** |
||
37 | * Get List models Day |
||
38 | * @return mixed |
||
39 | */ |
||
40 | View Code Duplication | public function getListDays() { |
|
47 | |||
48 | /** |
||
49 | * Get yesterday model day |
||
50 | * @return string |
||
51 | */ |
||
52 | View Code Duplication | public function getYesterday() { |
|
60 | |||
61 | |||
62 | |||
63 | |||
64 | /*public $listDay = array(); |
||
65 | private $xml; |
||
66 | |||
67 | public function __construct($fileName) { |
||
68 | |||
69 | $this->xml = simplexml_load_file($fileName); |
||
70 | /*$day = new Day(); |
||
71 | $day->p = 0; |
||
72 | $day->p1 = 1; |
||
73 | |||
74 | $this->listDay[] = $day;*/ |
||
75 | //echo $fileName; |
||
76 | //$xml = simplexml_load_file($fileName, "Day"); |
||
77 | |||
78 | |||
79 | |||
80 | //$xml1 = $file = file_get_contents($fileName, true);; |
||
81 | //var_dump($xml); |
||
82 | //$xml1 = simplexml_load_string($xml, 'Day'); |
||
83 | //var_dump($xml->day[0]); |
||
84 | //var_dump($xml); |
||
85 | //var_dump($this->listDay); |
||
86 | /*} |
||
87 | |||
88 | public function getListDay() { |
||
89 | |||
90 | |||
91 | |||
92 | |||
93 | foreach ($this->xml->day as $day) { |
||
94 | $this->listDay[] = new Day($day); |
||
95 | //var_dump($day->sunrise); |
||
96 | |||
97 | } |
||
98 | |||
99 | return $this->listDay; |
||
100 | |||
101 | }*/ |
||
102 | |||
103 | |||
104 | /*public function getListDay() { |
||
105 | return $this->listDay; |
||
106 | }*/ |
||
107 | |||
108 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.