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 |
||
| 6 | class DeCaptchaAbstractTest extends PHPUnit_Framework_TestCase |
||
|
1 ignored issue
–
show
|
|||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @return PHPUnit_Framework_MockObject_MockObject|\jumper423\decaptcha\core\DeCaptchaAbstract |
||
| 10 | */ |
||
| 11 | public function newInstance() |
||
| 18 | |||
| 19 | public function testGetBaseUrl() |
||
| 29 | |||
| 30 | // public function testGetActionUrl() |
||
|
1 ignored issue
–
show
|
|||
| 31 | // { |
||
| 32 | // $abstract = $this->newInstance(); |
||
| 33 | // $getBaseUrlGetCodeCaller = function () { |
||
| 34 | // $this->captchaId = 123; |
||
| 35 | // |
||
| 36 | // return $this->getActionUrl('get_code'); |
||
| 37 | // }; |
||
| 38 | // $getBaseUrlGetBalanceCaller = function ($action, $key, $id) { |
||
| 39 | // $this->domain = 'domain'; |
||
| 40 | // $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY, $key); |
||
| 41 | // $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_CAPTCHA, $id); |
||
| 42 | // return $this->getActionUrl('get_balance'); |
||
| 43 | // }; |
||
| 44 | // $abstract->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY '123456'); |
||
| 45 | // $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract); |
||
| 46 | // $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound()); |
||
| 47 | // $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract); |
||
| 48 | // $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound()); |
||
| 49 | // } |
||
| 50 | |||
| 51 | public function testGetFilePath() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 67 | * @expectedExceptionCode 16 |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function testGetFilePathErrorFileNotFound() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 81 | * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
||
| 82 | * @expectedExceptionCode 15 |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function testGetFilePathErrorFileIsNotLoaded() |
|
| 93 | |||
| 94 | // public function testGetResponse() |
||
|
1 ignored issue
–
show
|
|||
| 95 | // { |
||
| 96 | // $abstract = $this->newInstance(); |
||
| 97 | // $getResponseCaller = function ($val) { |
||
| 98 | // $this->domain = 'echo.jsontest.com/aaa/bbb'; |
||
| 99 | // return $this->getResponse($val); |
||
| 100 | // }; |
||
| 101 | // $bound = $getResponseCaller->bindTo($abstract, $abstract); |
||
| 102 | // $res = $bound(''); |
||
| 103 | // $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res))); |
||
| 104 | // } |
||
| 105 | |||
| 106 | // public function testExecutionDelayed() |
||
| 107 | // { |
||
| 108 | // $abstract = $this->newInstance(); |
||
| 109 | // $executionDelayedCaller = function ($second, $call = null) { |
||
| 110 | // return $this->executionDelayed($second, $call); |
||
| 111 | // }; |
||
| 112 | // $bound = $executionDelayedCaller->bindTo($abstract, $abstract); |
||
| 113 | // $start = microtime(true); |
||
| 114 | // $bound(0); |
||
| 115 | // $bound(0.1); |
||
| 116 | // $timePassed = microtime(true) - $start; |
||
| 117 | // $this->assertTrue(abs($timePassed - 0.1) < 0.035); |
||
| 118 | // |
||
| 119 | // $start = microtime(true); |
||
| 120 | // $bound(0.15, function () { |
||
| 121 | // sleep(0.2); |
||
| 122 | // }); |
||
| 123 | // $bound(0.1); |
||
| 124 | // $timePassed = microtime(true) - $start; |
||
| 125 | // $this->assertTrue(abs($timePassed - 0.25) < 0.035); |
||
| 126 | // |
||
| 127 | // $start = microtime(true); |
||
| 128 | // $bound(0.15, function () { |
||
| 129 | // sleep(0.2); |
||
| 130 | // }); |
||
| 131 | // $bound(0.3); |
||
| 132 | // $timePassed = microtime(true) - $start; |
||
| 133 | // $this->assertTrue(abs($timePassed - 0.45) < 0.035); |
||
| 134 | // |
||
| 135 | // $this->assertEquals(2, $bound(0, function () { |
||
| 136 | // return 2; |
||
| 137 | // })); |
||
| 138 | // $this->assertEquals(null, $bound(0)); |
||
| 139 | // } |
||
| 140 | |||
| 141 | // public function testGetInUrl() |
||
|
1 ignored issue
–
show
|
|||
| 142 | // { |
||
| 143 | // $abstract = $this->newInstance(); |
||
| 144 | // $getInUrlCaller = function () { |
||
| 145 | // $this->domain = 'domain'; |
||
| 146 | // return $this->getInUrl(); |
||
| 147 | // }; |
||
| 148 | // $abstract->setApiKey('123456'); |
||
| 149 | // $bound = $getInUrlCaller->bindTo($abstract, $abstract); |
||
| 150 | // $this->assertEquals('http://domain/in.php', $bound()); |
||
| 151 | // } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 155 | * @expectedExceptionCode 4 |
||
| 156 | */ |
||
| 157 | public function testIsError() |
||
| 166 | |||
| 167 | public function testIsErrorNot() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 179 | * @expectedExceptionCode 17 |
||
| 180 | * @expectedExceptionMessage Ошибка CURL: Could |
||
| 181 | */ |
||
| 182 | public function testGetCurlResponseError() |
||
| 191 | |||
| 192 | public function testGetCurlResponse() |
||
| 203 | } |
||
| 204 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.