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 |
||
16 | class SearchResultAdCrawler extends CrawlerAbstract |
||
17 | { |
||
18 | /** |
||
19 | * @var AdUrlParser |
||
20 | */ |
||
21 | protected $url; |
||
22 | |||
23 | /** |
||
24 | * @param $url |
||
25 | * @return SearchResultUrlParser |
||
26 | */ |
||
27 | 10 | protected function setUrlParser($url) |
|
31 | |||
32 | /** |
||
33 | * Return the Ad's ID |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | 10 | public function getId() |
|
41 | |||
42 | |||
43 | /** |
||
44 | * Return the title |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | 10 | public function getTitle() |
|
52 | |||
53 | /** |
||
54 | * Return the price |
||
55 | * |
||
56 | * @return int |
||
57 | */ |
||
58 | 10 | public function getPrice() |
|
68 | |||
69 | /** |
||
70 | * Return the Ad's URL |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 10 | public function getUrl() |
|
78 | |||
79 | /** |
||
80 | * Return the data and time the ad was created |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | 10 | public function getCreatedAt() |
|
91 | |||
92 | /** |
||
93 | * Return the thumb picture url |
||
94 | * |
||
95 | * @return null|string |
||
96 | */ |
||
97 | 10 | public function getThumb() |
|
112 | |||
113 | /** |
||
114 | * Return the number of picture of the ad |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | 10 | public function getNbImage() |
|
126 | |||
127 | /** |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 10 | View Code Duplication | public function getPlacement() |
138 | |||
139 | /** |
||
140 | * @return mixed |
||
141 | */ |
||
142 | 10 | View Code Duplication | public function getType() |
154 | |||
155 | /** |
||
156 | * @return object |
||
157 | */ |
||
158 | 10 | public function getAll() |
|
172 | |||
173 | /** |
||
174 | * Return the field's value |
||
175 | * |
||
176 | * @param Crawler $node |
||
177 | * @param mixed $defaultValue |
||
178 | * @param \Closure $callback |
||
179 | * @param string $funcName |
||
180 | * @param string $funcParam |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 10 | private function getFieldValue( |
|
197 | } |
||
198 |
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.