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 |
||
| 11 | class CurrentWeatherDataTest extends \PHPUnit_Framework_TestCase |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Client|\PHPUnit_Framework_MockObject_MockObject |
||
| 15 | */ |
||
| 16 | protected $client; |
||
| 17 | /** |
||
| 18 | * @var CurrentWeatherData |
||
| 19 | */ |
||
| 20 | protected $currentWeatherData; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get OpenWeatherMap response example |
||
| 24 | * |
||
| 25 | * @return string |
||
| 26 | */ |
||
| 27 | protected function getWeatherData() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | protected function setUp() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Override Guzzle get() method to throw exception |
||
| 82 | * |
||
| 83 | * @param string $message |
||
| 84 | * @param int $statusCode |
||
| 85 | * @throws \InvalidArgumentException |
||
| 86 | */ |
||
| 87 | protected function overrideClientGetMethod(string $message, int $statusCode) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\BadRequestException |
||
| 101 | * @expectedExceptionMessage You doesn't set APPID parameter or it has incorrect value |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function testAppIdProblem() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\BadRequestException |
||
| 117 | * @expectedExceptionMessage Data available only on commercial terms |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function testPaidFeatures() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @expectedException \GuzzleHttp\Exception\ClientException |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function testCatchNonExpectedException() |
|
| 139 | |||
| 140 | public function testLoadByCityNameWithoutCountry() |
||
| 146 | |||
| 147 | public function testLoadByCityNameWithCorrectCountryCode() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\InvalidCountryCodeException |
||
| 156 | * @expectedExceptionMessage You should provide ISO 3166-1 alpha-2 country code |
||
| 157 | */ |
||
| 158 | public function testLoadByCityNameWithIncorrectCountryCode() |
||
| 162 | |||
| 163 | public function testLoadByCityId() |
||
| 169 | |||
| 170 | public function testLoadByGeographicCoordinates() |
||
| 176 | |||
| 177 | public function testLoadByZipCodeWithoutCountry() |
||
| 183 | |||
| 184 | public function testLoadByZipCodeWithCorrectCountryCode() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @expectedException \Savchenko\Bundle\OpenWeatherMapBundle\Exception\InvalidCountryCodeException |
||
| 193 | * @expectedExceptionMessage You should provide ISO 3166-1 alpha-2 country code |
||
| 194 | */ |
||
| 195 | public function testLoadByZipCodeWithIncorrectCountryCode() |
||
| 199 | } |
||
| 200 |
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.