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() |
||
| 30 | |||
| 31 | // public function testGetActionUrl() |
||
|
1 ignored issue
–
show
|
|||
| 32 | // { |
||
| 33 | // $abstract = $this->newInstance(); |
||
| 34 | // $getBaseUrlGetCodeCaller = function () { |
||
| 35 | // $this->captchaId = 123; |
||
| 36 | // |
||
| 37 | // return $this->getActionUrl('get_code'); |
||
| 38 | // }; |
||
| 39 | // $getBaseUrlGetBalanceCaller = function ($action, $key, $id) { |
||
| 40 | // $this->domain = 'domain'; |
||
| 41 | // $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY, $key); |
||
| 42 | // $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_CAPTCHA, $id); |
||
| 43 | // return $this->getActionUrl('get_balance'); |
||
| 44 | // }; |
||
| 45 | // $abstract->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY '123456'); |
||
| 46 | // $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract); |
||
| 47 | // $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound()); |
||
| 48 | // $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract); |
||
| 49 | // $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound()); |
||
| 50 | // } |
||
| 51 | |||
| 52 | public function testGetFilePath() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 68 | * @expectedExceptionCode 16 |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function testGetFilePathErrorFileNotFound() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 82 | * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
||
| 83 | * @expectedExceptionCode 15 |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function testGetFilePathErrorFileIsNotLoaded() |
|
| 94 | |||
| 95 | // public function testGetResponse() |
||
|
1 ignored issue
–
show
|
|||
| 96 | // { |
||
| 97 | // $abstract = $this->newInstance(); |
||
| 98 | // $getResponseCaller = function ($val) { |
||
| 99 | // $this->domain = 'echo.jsontest.com/aaa/bbb'; |
||
| 100 | // return $this->getResponse($val); |
||
| 101 | // }; |
||
| 102 | // $bound = $getResponseCaller->bindTo($abstract, $abstract); |
||
| 103 | // $res = $bound(''); |
||
| 104 | // $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res))); |
||
| 105 | // } |
||
| 106 | |||
| 107 | public function testExecutionDelayed() |
||
| 141 | |||
| 142 | // public function testGetInUrl() |
||
|
1 ignored issue
–
show
|
|||
| 143 | // { |
||
| 144 | // $abstract = $this->newInstance(); |
||
| 145 | // $getInUrlCaller = function () { |
||
| 146 | // $this->domain = 'domain'; |
||
| 147 | // return $this->getInUrl(); |
||
| 148 | // }; |
||
| 149 | // $abstract->setApiKey('123456'); |
||
| 150 | // $bound = $getInUrlCaller->bindTo($abstract, $abstract); |
||
| 151 | // $this->assertEquals('http://domain/in.php', $bound()); |
||
| 152 | // } |
||
| 153 | |||
| 154 | // /** |
||
|
1 ignored issue
–
show
|
|||
| 155 | // * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 156 | // * @expectedExceptionCode 4 |
||
| 157 | // */ |
||
| 158 | // public function testIsError() |
||
| 159 | // { |
||
| 160 | // $abstract = $this->newInstance(); |
||
| 161 | // $isErrorCaller = function ($val) { |
||
| 162 | // return $this->isError($val); |
||
| 163 | // }; |
||
| 164 | // $bound = $isErrorCaller->bindTo($abstract, $abstract); |
||
| 165 | // $bound('ERROR_IP_NOT_ALLOWED'); |
||
| 166 | // } |
||
| 167 | |||
| 168 | // public function testIsErrorNot() |
||
|
1 ignored issue
–
show
|
|||
| 169 | // { |
||
| 170 | // $abstract = $this->newInstance(); |
||
| 171 | // $isErrorCaller = function ($val) { |
||
| 172 | // return $this->isError($val); |
||
| 173 | // }; |
||
| 174 | // $bound = $isErrorCaller->bindTo($abstract, $abstract); |
||
| 175 | // $this->assertNull($bound('BALANCE:56')); |
||
| 176 | // } |
||
| 177 | |||
| 178 | // /** |
||
|
1 ignored issue
–
show
|
|||
| 179 | // * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 180 | // * @expectedExceptionCode 17 |
||
| 181 | // * @expectedExceptionMessage Ошибка CURL: Could |
||
| 182 | // */ |
||
| 183 | // public function testGetCurlResponseError() |
||
| 184 | // { |
||
| 185 | // $abstract = $this->newInstance(); |
||
| 186 | // $getCurlResponseCaller = function ($url, $val) { |
||
| 187 | // return $this->getCurlResponse($url, $val); |
||
| 188 | // }; |
||
| 189 | // $bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
||
| 190 | // $bound('http://domain', ['protected' => 'value']); |
||
| 191 | // } |
||
| 192 | // |
||
| 193 | // public function testGetCurlResponse() |
||
| 194 | // { |
||
| 195 | // $abstract = $this->newInstance(); |
||
| 196 | // $getCurlResponseCaller = function ($url, $val) { |
||
| 197 | // return $this->getCurlResponse($url, $val); |
||
| 198 | // }; |
||
| 199 | // $bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
||
| 200 | // $data = $bound('http://httpbin.org/post', ['protected' => 'value']); |
||
| 201 | // $data = json_decode($data, true); |
||
| 202 | // $this->assertEquals(['protected' => 'value'], $data['form']); |
||
| 203 | // } |
||
| 204 | } |
||
| 205 |
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.