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 |
||
17 | class YahooWeatherClientTests extends TestCase |
||
18 | { |
||
19 | /** @test */ |
||
20 | View Code Duplication | public function testCallApiWoeidException() |
|
|
|||
21 | { |
||
22 | $service = new YahooWeatherAPI(); |
||
23 | |||
24 | $this->expectException(\Exception::class); |
||
25 | |||
26 | try { |
||
27 | $response = $service->callApiWoeid(null); |
||
28 | } catch (\Exception $e) { |
||
29 | throw $e; |
||
30 | } |
||
31 | } |
||
32 | |||
33 | /** @test */ |
||
34 | View Code Duplication | public function testCallApiCityNameException() |
|
35 | { |
||
36 | $service = new YahooWeatherAPI(); |
||
37 | |||
38 | $this->expectException(\Exception::class); |
||
39 | |||
40 | try { |
||
41 | $response = $service->callApiCityName(null); |
||
42 | } catch (\Exception $e) { |
||
43 | throw $e; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** @test */ |
||
48 | View Code Duplication | public function testCallApiTestException() |
|
49 | { |
||
50 | $service = new YahooWeatherAPI(); |
||
51 | |||
52 | $this->expectException(\Exception::class); |
||
53 | |||
54 | try { |
||
55 | $response = $service->callApi(null); |
||
56 | } catch (\Exception $e) { |
||
57 | throw $e; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** @test */ |
||
62 | View Code Duplication | public function testCallApiLastRes() |
|
63 | { |
||
64 | $errYql = 'https://query.yahooapis.com/v1/public/yql?q=from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Taipei%22)&format=json&env=store://datatables.org/alltableswithkeys'; |
||
65 | |||
66 | $service = new YahooWeatherAPI(); |
||
67 | |||
68 | $this->expectException(\Exception::class); |
||
69 | |||
70 | try { |
||
71 | $response = $service->callApi($errYql); |
||
72 | } catch (\Exception $e) { |
||
73 | throw $e; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** @test */ |
||
78 | public function setClientTest() |
||
84 | |||
85 | /** @test */ |
||
86 | public function setLastResTest() |
||
93 | |||
94 | /** @test */ |
||
95 | public function callApiWoeidTest() |
||
96 | { |
||
97 | $service = new YahooWeatherAPI(); |
||
98 | $response = $service->callApiWoeid(1232345678); |
||
99 | $this->assertSame($response, false); |
||
100 | |||
101 | $response = $service->callApiWoeid(2306179); |
||
102 | $city = str_replace(' ', '', $response['location']['city']); |
||
103 | $country = str_replace(' ', '', $response['location']['country']); |
||
104 | $region = str_replace(' ', '', $response['location']['region']); |
||
105 | $this->assertSame($city, 'TaipeiCity'); |
||
106 | $this->assertSame($country, 'Taiwan'); |
||
107 | $this->assertSame($region, 'TaipeiCity'); |
||
108 | |||
109 | $woeid = 2306179; |
||
110 | $response = $this->getWoeidTest($service); |
||
111 | $this->assertSame($response, $woeid); |
||
112 | } |
||
113 | |||
114 | /** @test */ |
||
115 | public function callApiCityNameTest() |
||
116 | { |
||
117 | $service = new YahooWeatherAPI(); |
||
118 | $response = $service->callApiCityName('Taipei'); |
||
119 | $city = str_replace(' ', '', $response['location']['city']); |
||
120 | $country = str_replace(' ', '', $response['location']['country']); |
||
121 | $region = str_replace(' ', '', $response['location']['region']); |
||
122 | $this->assertSame($city, 'TaipeiCity'); |
||
123 | $this->assertSame($country, 'Taiwan'); |
||
124 | $this->assertSame($region, 'TaipeiCity'); |
||
125 | |||
126 | $city = 'Taipei'; |
||
127 | $response = $this->getCityTest($service); |
||
128 | $this->assertSame($response, $city); |
||
129 | } |
||
130 | |||
131 | /** @test */ |
||
132 | public function callApiTest() |
||
133 | { |
||
134 | $service = new YahooWeatherAPI(); |
||
135 | $yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="123456789")'; |
||
136 | $url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys'; |
||
137 | $url = $this->encodeURI($url); |
||
138 | $response = $service->callApi($url); |
||
139 | $this->assertSame($response, false); |
||
140 | |||
141 | $yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="Taipei")'; |
||
142 | $url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys'; |
||
143 | $url = $this->encodeURI($url); |
||
144 | $response = $service->callApi($url); |
||
145 | $city = str_replace(' ', '', $response['location']['city']); |
||
146 | $country = str_replace(' ', '', $response['location']['country']); |
||
147 | $region = str_replace(' ', '', $response['location']['region']); |
||
148 | $this->assertSame($city, 'TaipeiCity'); |
||
149 | $this->assertSame($country, 'Taiwan'); |
||
150 | $this->assertSame($region, 'TaipeiCity'); |
||
151 | |||
152 | $response = $this->getYqlTest($service); |
||
153 | $this->assertSame($response, $url); |
||
154 | } |
||
155 | |||
156 | /** @test */ |
||
157 | public function getLastResTest() |
||
158 | { |
||
159 | $service = new YahooWeatherAPI(); |
||
160 | |||
161 | $service->setLastResponse($this->getResponseData()); |
||
162 | |||
163 | $response = $service->getLastResponse(false); |
||
164 | $this->assertSame(is_array($response), true); |
||
165 | |||
166 | $response = $service->getLastResponse(true); |
||
167 | $this->assertSame(is_array($response), false); |
||
168 | } |
||
169 | |||
170 | /** @test */ |
||
171 | public function getTemperatureTest() |
||
172 | { |
||
173 | $service = new YahooWeatherAPI(); |
||
174 | $response = $this->getResponseData(); |
||
175 | $storeRes = $response; |
||
176 | |||
177 | $service->setLastResponse(null); |
||
178 | $service->getLastResponse(true); |
||
179 | $response = $service->getTemperature(true); |
||
180 | $this->assertSame($response, ''); |
||
181 | |||
182 | $noTemp = $storeRes; |
||
183 | $noTemp['item']['condition']['temp'] = null; |
||
184 | $service->setLastResponse($noTemp); |
||
185 | $response = $service->getTemperature(false); |
||
186 | $this->assertSame($response, ''); |
||
187 | |||
188 | $service->setLastResponse($storeRes); |
||
189 | $response = $service->getTemperature(true); |
||
190 | $this->assertSame(is_string($response), true); |
||
191 | |||
192 | $response = $service->getTemperature(false); |
||
193 | $this->assertSame(is_string((int) $response), false); |
||
194 | } |
||
195 | |||
196 | /** @test */ |
||
197 | public function getLocationTest() |
||
198 | { |
||
199 | $service = new YahooWeatherAPI(); |
||
200 | $response = $this->getResponseData(); |
||
201 | $storeRes = $response; |
||
202 | |||
203 | $service->setLastResponse(null); |
||
204 | $response = $service->getLocation(); |
||
205 | $this->assertSame($response, ''); |
||
206 | |||
207 | $noCity = $storeRes; |
||
208 | $noCity['location']['city'] = null; |
||
209 | $service->setLastResponse($noCity); |
||
210 | $response = $service->getLocation(); |
||
211 | $this->assertSame($response, ''); |
||
212 | |||
213 | |||
214 | $service->setLastResponse($storeRes); |
||
215 | $response = $service->getLocation(); |
||
216 | $city = str_replace(' ', '', $response); |
||
217 | $this->assertSame($city, 'TaipeiCity'); |
||
218 | } |
||
219 | |||
220 | /** @test */ |
||
221 | public function getForecastTest() |
||
222 | { |
||
223 | $service = new YahooWeatherAPI(); |
||
224 | $response = $this->getResponseData(); |
||
225 | $storeRes = $response; |
||
226 | |||
227 | $service->setLastResponse(null); |
||
228 | $response = $service->getForecast(); |
||
229 | $this->assertSame(count($response), 0); |
||
230 | |||
231 | $noForecast = $storeRes; |
||
232 | $noForecast['item']['forecast'] = null; |
||
233 | $service->setLastResponse($noForecast); |
||
234 | $response = $service->getForecast(); |
||
235 | $this->assertSame(count($response), 0); |
||
236 | |||
237 | $service->setLastResponse($storeRes); |
||
238 | $response = $service->getForecast(); |
||
239 | $this->assertSame(count($response), 10); |
||
240 | } |
||
241 | |||
242 | /** @test */ |
||
243 | public function getWindTest() |
||
244 | { |
||
245 | $service = new YahooWeatherAPI(); |
||
246 | $response = $this->getResponseData(); |
||
247 | $storeRes = $response; |
||
248 | |||
249 | $service->setLastResponse(null); |
||
250 | $response = $service->getWind(true); |
||
251 | $this->assertSame(count($response), 0); |
||
252 | |||
253 | $noSpeed = $storeRes; |
||
254 | $noSpeed['wind']['speed'] = null; |
||
255 | $service->setLastResponse($noSpeed); |
||
256 | $response = $service->getWind(true); |
||
257 | $this->assertSame(count($response), 0); |
||
258 | |||
259 | $service->setLastResponse($storeRes); |
||
260 | $expectRes = array( |
||
261 | 'chill' => $storeRes['wind']['chill'], |
||
262 | 'direction' => $storeRes['wind']['direction'], |
||
263 | 'speed' => $storeRes['wind']['speed'], |
||
264 | ); |
||
265 | |||
266 | $response = $service->getWind(true); |
||
267 | $expectRes['speed'] .= ' '.$storeRes['units']['speed']; |
||
268 | $this->assertSame($response['speed'], $expectRes['speed']); |
||
269 | |||
270 | $expectRes = array( |
||
271 | 'chill' => $storeRes['wind']['chill'], |
||
272 | 'direction' => $storeRes['wind']['direction'], |
||
273 | 'speed' => $storeRes['wind']['speed'], |
||
274 | ); |
||
275 | |||
276 | $response = $service->getWind(false); |
||
277 | $this->assertSame($response['speed'], $expectRes['speed']); |
||
278 | } |
||
279 | |||
280 | public function getWoeidTest(YahooWeatherAPI $service) |
||
284 | |||
285 | public function getCityTest(YahooWeatherAPI $service) |
||
289 | |||
290 | public function getYqlTest(YahooWeatherAPI $service) |
||
294 | |||
295 | public function encodeURI($url) |
||
296 | { |
||
297 | $unescaped = array( |
||
298 | '%2D' => '-', '%5F' => '_', '%2E' => '.', '%21' => '!', '%7E' => '~', |
||
299 | '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')', |
||
300 | ); |
||
301 | $reserved = array( |
||
302 | '%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':', |
||
303 | '%40' => '@', '%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$', |
||
304 | ); |
||
305 | $score = array( |
||
306 | '%23' => '#', |
||
307 | ); |
||
308 | |||
309 | return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score)); |
||
310 | } |
||
311 | |||
312 | public function getResponseData() |
||
319 | } |
||
320 |
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.