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 |
||
5 | class GeocodeTest extends \PHPUnit_Framework_TestCase |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @dataProvider providerTestGeocodeProvider |
||
10 | */ |
||
11 | public function testGeocode($address, $expected) |
||
12 | { |
||
13 | $actual = new Geocode(); |
||
14 | $location = $actual->get($address); |
||
15 | $methods = array_keys($expected); |
||
16 | $this->assertTrue($location->isValid()); |
||
17 | foreach ($methods as $method) { |
||
18 | $this->assertEquals($expected[$method], $location->$method()); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | public function testInvalidLocation() |
||
28 | |||
29 | /** |
||
30 | * @test |
||
31 | * @expectedException \Exception |
||
32 | * @expectedExceptionMessage Address is required in order to process |
||
33 | */ |
||
34 | public function testEmptyOrNullAdress() |
||
39 | |||
40 | View Code Duplication | public function testGeocodeProtocol() |
|
54 | |||
55 | View Code Duplication | public function testGeocodeKey() |
|
69 | |||
70 | |||
71 | public function providerTestGeocodeProvider() |
||
110 | } |
||
111 |
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.