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 |
||
| 3 | class DeCaptchaAbstractTest extends PHPUnit_Framework_TestCase |
||
|
1 ignored issue
–
show
|
|||
| 4 | { |
||
| 5 | View Code Duplication | public function testGetBaseUrl() |
|
| 6 | { |
||
| 7 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 8 | // $foo->expects($this->any()) |
||
| 9 | // ->method("baz") |
||
| 10 | // ->will($this->returnValue("You called baz!")); |
||
| 11 | $getBaseUrlCaller = function () { |
||
| 12 | return $this->getBaseUrl(); |
||
|
1 ignored issue
–
show
|
|||
| 13 | }; |
||
| 14 | $abstract->domain = 'domain'; |
||
| 15 | $bound = $getBaseUrlCaller->bindTo($abstract, $abstract); |
||
| 16 | $this->assertEquals('http://domain/', $bound()); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function testSetApiKey() |
||
| 38 | |||
| 39 | public function testGetActionUrl() |
||
| 57 | |||
| 58 | public function testGetFilePath() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 74 | * @expectedExceptionCode 16 |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function testGetFilePathErrorFileNotFound() |
|
|
1 ignored issue
–
show
|
|||
| 77 | { |
||
| 78 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 79 | $abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
||
| 80 | $getFilePathCaller = function ($val) { |
||
| 81 | return $this->getFilePath($val); |
||
|
1 ignored issue
–
show
|
|||
| 82 | }; |
||
| 83 | $bound = $getFilePathCaller->bindTo($abstract, $abstract); |
||
| 84 | $bound(__DIR__ . '/data/Captcha1.jpg'); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 89 | * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
||
| 90 | * @expectedExceptionCode 15 |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function testGetFilePathErrorFileIsNotLoaded() |
|
|
1 ignored issue
–
show
|
|||
| 93 | { |
||
| 94 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 95 | $abstract->errorLang = \jumper423\decaptcha\core\DeCaptchaErrors::LANG_RU; |
||
| 96 | $getFilePathCaller = function ($val) { |
||
| 97 | return $this->getFilePath($val); |
||
|
1 ignored issue
–
show
|
|||
| 98 | }; |
||
| 99 | $bound = $getFilePathCaller->bindTo($abstract, $abstract); |
||
| 100 | $bound('https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123'); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testGetResponse() |
||
| 114 | |||
| 115 | public function testExecutionDelayed() |
||
| 149 | |||
| 150 | View Code Duplication | public function testGetInUrl() |
|
| 151 | { |
||
| 152 | $abstract = $this->getMockForAbstractClass(\jumper423\decaptcha\core\DeCaptchaAbstract::class); |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
| 164 | * @expectedExceptionCode 4 |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function testIsError() |
|
| 176 | |||
| 177 | View Code Duplication | public function testIsErrorNot() |
|
| 187 | } |
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.