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 |
||
| 11 | class ServiceTest extends TestCase |
||
| 12 | { |
||
| 13 | private $_realGuzzleClient; |
||
| 14 | |||
| 15 | public function setUp() |
||
| 16 | { |
||
| 17 | $this->_realGuzzleClient = new GuzzleClient(); |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @test |
||
| 22 | * @dataProvider constructWithInvalidArgumentsData |
||
| 23 | * @expectedException Exception |
||
| 24 | * @covers ::__construct |
||
| 25 | */ |
||
| 26 | public function constructWithInvalidArguments($pubkey, $privkey, $hashkey) |
||
| 27 | { |
||
| 28 | new Service($this->_realGuzzleClient, $pubkey, $privkey, $hashkey); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function constructWithInvalidArgumentsData() |
||
| 32 | { |
||
| 33 | return [ |
||
| 34 | [$this->_realGuzzleClient, null, null, null], |
||
| 35 | [$this->_realGuzzleClient, '', null, null], |
||
| 36 | [$this->_realGuzzleClient, 0, null, null], |
||
| 37 | [$this->_realGuzzleClient, false, null, null], |
||
| 38 | [$this->_realGuzzleClient, 'test', null, null], |
||
| 39 | [$this->_realGuzzleClient, 'test', '', null], |
||
| 40 | [$this->_realGuzzleClient, 'test', 0, null], |
||
| 41 | [$this->_realGuzzleClient, 'test', false, null], |
||
| 42 | ]; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @test |
||
| 47 | * @covers ::__construct |
||
| 48 | */ |
||
| 49 | public function constructWithValidArguments() |
||
| 50 | { |
||
| 51 | $this->assertNotNull(new Service($this->_realGuzzleClient, 'test', 'test')); |
||
| 52 | $this->assertNotNull(new Service($this->_realGuzzleClient, 'test', 'test', 'test')); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @test |
||
| 57 | * @covers ::getHtml |
||
| 58 | */ |
||
| 59 | public function getHtmlDefault() |
||
| 60 | { |
||
| 61 | $client = new GuzzleClient(); |
||
|
|
|||
| 62 | $pubkey = 'MyTestPubKeyStringToTestFor'; |
||
| 63 | $service = new Service($this->_realGuzzleClient, $pubkey, 'notest'); |
||
| 64 | |||
| 65 | $html = $service->getHtml(); |
||
| 66 | $this->assertRegExp("/k={$pubkey}/", $html); |
||
| 67 | $this->assertNotRegExp('/;error=1/', $html); |
||
| 68 | $this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SERVER, '/') . '/', $html); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @test |
||
| 73 | * @covers ::getHtml |
||
| 74 | */ |
||
| 75 | public function getHtmlWithArguments() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @test |
||
| 85 | * @dataProvider checkAnswerNoRemoteIpData |
||
| 86 | * @expectedException Exception |
||
| 87 | * @covers ::checkAnswer |
||
| 88 | */ |
||
| 89 | public function checkAnswerNoRemoteIp($remoteIp) |
||
| 94 | |||
| 95 | public function checkAnswerNoRemoteIpData() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @test |
||
| 105 | * @dataProvider checkAnswerEmptyArgumentsData |
||
| 106 | * @covers ::checkAnswer |
||
| 107 | */ |
||
| 108 | public function checkAnswerEmptyArguments($challenge, $response) |
||
| 117 | |||
| 118 | public function checkAnswerEmptyArgumentsData() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @test |
||
| 134 | * @dataProvider checkAnswerErrorResponseData |
||
| 135 | * @covers ::checkAnswer |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function checkAnswerErrorResponse($hashKey, GuzzleResponse $guzzleResponse, $message) |
|
| 150 | |||
| 151 | public function checkAnswerErrorResponseData() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @test |
||
| 164 | * @dataProvider checkAnswerValidResponseData |
||
| 165 | * @covers ::checkAnswer |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function checkAnswerValidResponse($hashKey, GuzzleResponse $guzzleResponse) |
|
| 179 | |||
| 180 | public function checkAnswerValidResponseData() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @test |
||
| 190 | * @covers ::getSignupUrl |
||
| 191 | */ |
||
| 192 | public function getSignupUrl() |
||
| 197 | } |
||
| 198 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.