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 |
||
| 12 | class ServiceTest extends TestCase |
||
| 13 | { |
||
| 14 | private $_realGuzzleClient; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @test |
||
| 18 | * @dataProvider constructWithInvalidArgumentsData |
||
| 19 | * @expectedException Exception |
||
| 20 | * @covers ::__construct |
||
| 21 | */ |
||
| 22 | public function constructWithInvalidArguments($pubkey, $privkey, $hashkey) |
||
| 26 | |||
| 27 | public function constructWithInvalidArgumentsData() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @test |
||
| 37 | * @covers ::getHtml |
||
| 38 | */ |
||
| 39 | public function getHtmlDefault() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @test |
||
| 52 | * @covers ::getHtml |
||
| 53 | */ |
||
| 54 | public function getHtmlWithArguments() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @test |
||
| 64 | * @dataProvider checkAnswerNoRemoteIpData |
||
| 65 | * @expectedException Exception |
||
| 66 | * @covers ::checkAnswer |
||
| 67 | */ |
||
| 68 | public function checkAnswerNoRemoteIp($remoteIp) |
||
| 73 | |||
| 74 | public function checkAnswerNoRemoteIpData() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @test |
||
| 83 | * @dataProvider checkAnswerEmptyArgumentsData |
||
| 84 | * @covers ::checkAnswer |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function checkAnswerEmptyArguments($challenge, $response) |
|
| 95 | |||
| 96 | public function checkAnswerEmptyArgumentsData() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @test |
||
| 112 | * @dataProvider checkAnswerErrorResponseData |
||
| 113 | * @covers ::checkAnswer |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function checkAnswerErrorResponse($hashKey, Response $guzzleResponse, $message) |
|
| 122 | |||
| 123 | public function checkAnswerErrorResponseData() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @test |
||
| 136 | * @dataProvider checkAnswerValidResponseData |
||
| 137 | * @covers ::checkAnswer |
||
| 138 | */ |
||
| 139 | public function checkAnswerValidResponse($hashKey, Response $guzzleResponse) |
||
| 145 | |||
| 146 | public function checkAnswerValidResponseData() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @test |
||
| 156 | * @covers ::getSignupUrl |
||
| 157 | */ |
||
| 158 | public function getSignupUrl() |
||
| 163 | |||
| 164 | private function getGuzzleClient(Response $response = null) : ClientInterface |
||
| 170 | } |
||
| 171 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: