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 |
||
9 | class ServiceTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | private $_realGuzzleClient; |
||
12 | |||
13 | public function setUp() |
||
17 | |||
18 | /** |
||
19 | * @test |
||
20 | * @dataProvider constructWithInvalidArgumentsData |
||
21 | * @expectedException Exception |
||
22 | * @covers ::__construct |
||
23 | */ |
||
24 | public function constructWithInvalidArguments($pubkey, $privkey, $hashkey) |
||
28 | |||
29 | public function constructWithInvalidArgumentsData() |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | * @covers ::__construct |
||
46 | */ |
||
47 | public function constructWithValidArguments() |
||
52 | |||
53 | /** |
||
54 | * @test |
||
55 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
56 | * @covers ::getHtml |
||
57 | */ |
||
58 | public function getHtmlDefault() |
||
69 | |||
70 | /** |
||
71 | * @test |
||
72 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
73 | * @covers ::getHtml |
||
74 | */ |
||
75 | public function getHtmlWithArguments() |
||
82 | |||
83 | /** |
||
84 | * @test |
||
85 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
86 | * @dataProvider checkAnswerNoRemoteIpData |
||
87 | * @expectedException Exception |
||
88 | * @covers ::checkAnswer |
||
89 | */ |
||
90 | public function checkAnswerNoRemoteIp($remoteIp) |
||
95 | |||
96 | public function checkAnswerNoRemoteIpData() |
||
103 | |||
104 | /** |
||
105 | * @test |
||
106 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
107 | * @uses \TraderInteractive\SolveMedia\Response::<public> |
||
108 | * @dataProvider checkAnswerEmptyArgumentsData |
||
109 | * @covers ::checkAnswer |
||
110 | */ |
||
111 | public function checkAnswerEmptyArguments($challenge, $response) |
||
120 | |||
121 | public function checkAnswerEmptyArgumentsData() |
||
134 | |||
135 | /** |
||
136 | * @test |
||
137 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
138 | * @uses \TraderInteractive\SolveMedia\Response::__construct |
||
139 | * @uses \TraderInteractive\SolveMedia\Response::valid |
||
140 | * @uses \TraderInteractive\SolveMedia\Response::getMessage |
||
141 | * @dataProvider checkAnswerErrorResponseData |
||
142 | * @covers ::checkAnswer |
||
143 | */ |
||
144 | View Code Duplication | public function checkAnswerErrorResponse($hashKey, GuzzleResponse $guzzleResponse, $message) |
|
157 | |||
158 | public function checkAnswerErrorResponseData() |
||
168 | |||
169 | /** |
||
170 | * @test |
||
171 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
172 | * @uses \TraderInteractive\SolveMedia\Response::__construct |
||
173 | * @uses \TraderInteractive\SolveMedia\Response::valid |
||
174 | * @dataProvider checkAnswerValidResponseData |
||
175 | * @covers ::checkAnswer |
||
176 | */ |
||
177 | View Code Duplication | public function checkAnswerValidResponse($hashKey, GuzzleResponse $guzzleResponse) |
|
189 | |||
190 | public function checkAnswerValidResponseData() |
||
197 | |||
198 | /** |
||
199 | * @test |
||
200 | * @uses \TraderInteractive\SolveMedia\Service::__construct |
||
201 | * @covers ::getSignupUrl |
||
202 | */ |
||
203 | public function getSignupUrl() |
||
208 | } |
||
209 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.