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 |
||
| 13 | final class CoordinatesTest extends TestCase |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @test |
||
| 17 | * @dataProvider getCoordinates |
||
| 18 | */ |
||
| 19 | public function coordinate(float $latitude, float $longitude, float $accuracyMeters) |
||
| 20 | { |
||
| 21 | $coordinate = new Coordinates($latitude, $longitude, $accuracyMeters); |
||
| 22 | |||
| 23 | self::assertSame($latitude, $coordinate->getLatitude()); |
||
| 24 | self::assertSame($longitude, $coordinate->getLongitude()); |
||
| 25 | self::assertSame($accuracyMeters, $coordinate->getAccuracyMeters()); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function getCoordinates(): array |
||
| 29 | { |
||
| 30 | return [ |
||
| 31 | 'Almere Poort, The Netherlands' => [52.3504547, 5.1511458, 10], |
||
| 32 | 'Amsterdam, The Netherlands' => [52.3702157, 4.8951679, 0], |
||
| 33 | 'Den Haag, The Netherlands' => [52.0704978, 4.3006999, -10.1], |
||
| 34 | ]; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @test |
||
| 39 | * @expectedException \LauLamanApps\IzettleApi\API\Purchase\Exceptions\InvalidLatitudeException |
||
| 40 | * @dataProvider getInvalidLatitude |
||
| 41 | */ |
||
| 42 | public function invalidLatitude(float $latitude) |
||
| 43 | { |
||
| 44 | new Coordinates($latitude, 5.1511458, 10.0); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function getInvalidLatitude(): array |
||
| 48 | { |
||
| 49 | return [ |
||
| 50 | [-90.0000001], |
||
| 51 | [90.0000001], |
||
| 52 | ]; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @test |
||
| 57 | * @expectedException \LauLamanApps\IzettleApi\API\Purchase\Exceptions\InvalidLongitudeException |
||
| 58 | * @dataProvider getInvalidLongitude |
||
| 59 | */ |
||
| 60 | public function invalidLongitude(float $longitude) |
||
| 61 | { |
||
| 62 | new Coordinates(52.3504547, $longitude, 10.0); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getInvalidLongitude(): array |
||
| 66 | { |
||
| 67 | return [ |
||
| 68 | [-180.0000001], |
||
| 69 | [180.0000001], |
||
| 70 | ]; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |