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 |
||
28 | class Search extends AbstractOperation |
||
29 | { |
||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 1 | public function getName() |
|
37 | |||
38 | /** |
||
39 | * Return the amazon category |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | 1 | public function getCategory() |
|
47 | |||
48 | /** |
||
49 | * Sets the amazon category |
||
50 | * |
||
51 | * @param string $category |
||
52 | * |
||
53 | * @return \ApaiIO\Operations\Search |
||
54 | */ |
||
55 | 1 | public function setCategory($category) |
|
61 | |||
62 | /** |
||
63 | * Returns the keywords |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 1 | public function getKeywords() |
|
71 | |||
72 | /** |
||
73 | * Sets the keywords |
||
74 | * |
||
75 | * @param string $keywords |
||
76 | * |
||
77 | * @return \ApaiIO\Operations\Search |
||
78 | */ |
||
79 | 1 | public function setKeywords($keywords) |
|
85 | |||
86 | /** |
||
87 | * Returns the sort |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 1 | public function getSort() |
|
95 | |||
96 | /** |
||
97 | * Sets the sort |
||
98 | * |
||
99 | * @param string $sort |
||
100 | * |
||
101 | * @return \ApaiIO\Operations\Search |
||
102 | */ |
||
103 | 1 | public function setSort($sort) |
|
109 | |||
110 | /** |
||
111 | * Return the resultpage |
||
112 | * |
||
113 | * @return integer |
||
114 | */ |
||
115 | 1 | public function getPage() |
|
119 | |||
120 | /** |
||
121 | * Sets the resultpage to a specified value |
||
122 | * Allows to browse resultsets which have more than one page |
||
123 | * |
||
124 | * @param integer $page |
||
125 | * |
||
126 | * @return \ApaiIO\Operations\Search |
||
127 | */ |
||
128 | 3 | public function setPage($page) |
|
129 | { |
||
130 | 3 | View Code Duplication | if (false === is_numeric($page) || $page < 1 || $page > 10) { |
|
|||
131 | 1 | throw new \InvalidArgumentException(sprintf('%s is an invalid page value. It has to be numeric, positive and between 1 and 10', |
|
132 | $page)); |
||
133 | } |
||
134 | |||
135 | 2 | $this->parameters['ItemPage'] = $page; |
|
136 | |||
137 | 2 | return $this; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Return the minimum price as integer so 8.99$ will be returned as 899 |
||
142 | * |
||
143 | * @return integer |
||
144 | */ |
||
145 | 2 | public function getMinimumPrice() |
|
149 | |||
150 | /** |
||
151 | * Sets the minimum price to a specified value for the search |
||
152 | * Currency will be given by the site you are querying: EUR for IT, USD for COM |
||
153 | * Price should be given as integer. 8.99$ USD becomes 899 |
||
154 | * |
||
155 | * @param integer $price |
||
156 | * |
||
157 | * @return \ApaiIO\Operations\Search |
||
158 | */ |
||
159 | 3 | public function setMinimumPrice($price) |
|
166 | |||
167 | /** |
||
168 | * Returns the maximum price as integer so 8.99$ will be returned as 899 |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 2 | public function getMaximumPrice() |
|
175 | |||
176 | /** |
||
177 | * Sets the maximum price to a specified value for the search |
||
178 | * Currency will be given by the site you are querying: EUR for IT, USD for COM |
||
179 | * Price should be given as integer. 8.99$ USD becomes 899 |
||
180 | * |
||
181 | * @param integer $price |
||
182 | * |
||
183 | * @return \ApaiIO\Operations\Search |
||
184 | */ |
||
185 | 3 | public function setMaximumPrice($price) |
|
192 | |||
193 | /** |
||
194 | * Returns the condition of the items to return. New | Used | Collectible | Refurbished | All |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 2 | public function getCondition() |
|
202 | |||
203 | /** |
||
204 | * Sets the condition of the items to return: New | Used | Collectible | Refurbished | All |
||
205 | * |
||
206 | * Defaults to New. |
||
207 | * |
||
208 | * @param string $condition |
||
209 | * |
||
210 | * @return \ApaiIO\Operations\Search |
||
211 | */ |
||
212 | 2 | public function setCondition($condition) |
|
218 | |||
219 | /** |
||
220 | * Returns the availability. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 2 | public function getAvailability() |
|
228 | |||
229 | /** |
||
230 | * Sets the availability. Don't use method if you want the default Amazon behaviour. |
||
231 | * Only valid value = Available |
||
232 | * |
||
233 | * @param string $availability |
||
234 | * @see http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ReturningPriceAndAvailabilityInformation-itemsearch.html |
||
235 | * |
||
236 | * @return \ApaiIO\Operations\Search |
||
237 | */ |
||
238 | 2 | public function setAvailability($availability) |
|
244 | |||
245 | /** |
||
246 | * Returns the browseNodeId |
||
247 | * |
||
248 | * @return integer |
||
249 | */ |
||
250 | 2 | public function getBrowseNode() |
|
254 | |||
255 | /** |
||
256 | * Sets the browseNodeId |
||
257 | * |
||
258 | * @param integer $browseNodeId |
||
259 | * |
||
260 | * @return \ApaiIO\Operations\Search |
||
261 | */ |
||
262 | 2 | public function setBrowseNode($browseNodeId) |
|
268 | |||
269 | /** |
||
270 | * Validates the given price. |
||
271 | * |
||
272 | * @param integer $price |
||
273 | */ |
||
274 | 6 | protected function validatePrice($price) |
|
281 | } |
||
282 |
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.