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 |
||
| 20 | class OpenWeatherMapExceptionTest extends \PHPUnit_Framework_TestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $apiKey; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var OpenWeatherMap |
||
| 29 | */ |
||
| 30 | protected $owm; |
||
| 31 | |||
| 32 | protected function setUp() |
||
| 33 | { |
||
| 34 | $this->apiKey = 'unicorn-rainbow'; |
||
| 35 | $this->owm = new OpenWeatherMap($this->apiKey, new TestFetcher(), false, 600); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @expectedException \InvalidArgumentException |
||
| 40 | */ |
||
| 41 | public function testCacheException() |
||
| 42 | { |
||
| 43 | new OpenWeatherMap($this->apiKey, null, true, 600); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @expectedException \InvalidArgumentException |
||
| 48 | */ |
||
| 49 | public function testSecondNotNumericException() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @expectedException \InvalidArgumentException |
||
| 56 | */ |
||
| 57 | public function testGetWeatherForecastException() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @expectedException \InvalidArgumentException |
||
| 65 | */ |
||
| 66 | public function testGetDailyWeatherForecastException() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @expectedException \Cmfcmf\OpenWeatherMap\Exception |
||
| 74 | */ |
||
| 75 | public function testGetWeatherHistoryException() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @expectedException \Cmfcmf\OpenWeatherMap\Exception |
||
| 82 | */ |
||
| 83 | public function testGetWeatherHistoryWithEndException() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @expectedException \InvalidArgumentException |
||
| 90 | */ |
||
| 91 | public function testGetWeatherHistoryInvalidArgumentException() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @expectedException \InvalidArgumentException |
||
| 98 | */ |
||
| 99 | public function testGetRawDailyForecastDataInvalidArgumentException() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @expectedException \InvalidArgumentException |
||
| 106 | */ |
||
| 107 | public function testGetRawWeatherHistoryException() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @expectedException \InvalidArgumentException |
||
| 114 | */ |
||
| 115 | public function testGetRawWeatherHistoryWithEndDateException() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @expectedException \InvalidArgumentException |
||
| 122 | * @dataProvider uvIndexExceptionDataProvider |
||
| 123 | */ |
||
| 124 | public function testGetRawUVIndexWithQueryErrorException($lat, $lon, $dateTime) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @expectedException \InvalidArgumentException |
||
| 131 | * @dataProvider currentUVIndexExceptionDataProvider |
||
| 132 | */ |
||
| 133 | public function testGetRawCurrentUVIndexWithQueryErrorException($lat, $lon) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @expectedException \RuntimeException |
||
| 140 | */ |
||
| 141 | public function testGetRawUVIndexWithoutApiKey() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @expectedException \RuntimeException |
||
| 149 | */ |
||
| 150 | public function testGetRawCurrentUVIndexWithoutApiKey() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @expectedException \InvalidArgumentException |
||
| 158 | */ |
||
| 159 | public function testBuildQueryUrlParameterException() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @expectedException \Cmfcmf\OpenWeatherMap\Exception |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function testParseXMLException() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @expectedException \Cmfcmf\OpenWeatherMap\Exception |
||
| 178 | */ |
||
| 179 | public function testParseXMLWithIsJsonException() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @expectedException \Cmfcmf\OpenWeatherMap\Exception |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function testParseJsonException() |
|
| 200 | |||
| 201 | public function uvIndexExceptionDataProvider() |
||
| 209 | |||
| 210 | public function currentUVIndexExceptionDataProvider() |
||
| 218 | } |
||
| 219 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: