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() |
||
40 | |||
41 | /** |
||
42 | * @test |
||
43 | * @covers ::getHtml |
||
44 | */ |
||
45 | public function getHtmlDefault() |
||
55 | |||
56 | /** |
||
57 | * @test |
||
58 | * @covers ::getHtml |
||
59 | */ |
||
60 | public function getHtmlWithArguments() |
||
67 | |||
68 | /** |
||
69 | * @test |
||
70 | * @dataProvider checkAnswerNoRemoteIpData |
||
71 | * @expectedException Exception |
||
72 | * @covers ::checkAnswer |
||
73 | */ |
||
74 | public function checkAnswerNoRemoteIp($remoteIp) |
||
79 | |||
80 | public function checkAnswerNoRemoteIpData() |
||
87 | |||
88 | /** |
||
89 | * @test |
||
90 | * @dataProvider checkAnswerEmptyArgumentsData |
||
91 | * @covers ::checkAnswer |
||
92 | */ |
||
93 | View Code Duplication | public function checkAnswerEmptyArguments($challenge, $response) |
|
102 | |||
103 | public function checkAnswerEmptyArgumentsData() |
||
116 | |||
117 | /** |
||
118 | * @test |
||
119 | * @dataProvider checkAnswerErrorResponseData |
||
120 | * @covers ::checkAnswer |
||
121 | */ |
||
122 | View Code Duplication | public function checkAnswerErrorResponse($hashKey, Response $guzzleResponse, $message) |
|
129 | |||
130 | public function checkAnswerErrorResponseData() |
||
140 | |||
141 | /** |
||
142 | * @test |
||
143 | * @dataProvider checkAnswerValidResponseData |
||
144 | * @covers ::checkAnswer |
||
145 | */ |
||
146 | public function checkAnswerValidResponse($hashKey, Response $guzzleResponse) |
||
152 | |||
153 | public function checkAnswerValidResponseData() |
||
160 | |||
161 | /** |
||
162 | * @test |
||
163 | * @covers ::getSignupUrl |
||
164 | */ |
||
165 | public function getSignupUrl() |
||
170 | |||
171 | private function getGuzzleClient(Response $response = null) : ClientInterface |
||
177 | } |
||
178 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: