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 |
||
7 | { |
||
8 | /** |
||
9 | * @return PHPUnit_Framework_MockObject_MockObject|\jumper423\decaptcha\core\DeCaptchaAbstract |
||
10 | */ |
||
11 | public function newInstance() |
||
18 | |||
19 | public function testGetBaseUrl() |
||
40 | |||
41 | // public function testGetActionUrl() |
||
42 | // { |
||
43 | // $abstract = $this->newInstance(); |
||
44 | // $getBaseUrlGetCodeCaller = function () { |
||
45 | // $this->captchaId = 123; |
||
46 | // |
||
47 | // return $this->getActionUrl('get_code'); |
||
48 | // }; |
||
49 | // $getBaseUrlGetBalanceCaller = function ($action, $key, $id) { |
||
50 | // $this->domain = 'domain'; |
||
51 | // $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY, $key); |
||
52 | // $this->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_CAPTCHA, $id); |
||
53 | // return $this->getActionUrl('get_balance'); |
||
54 | // }; |
||
55 | // $abstract->setParamSpec(\jumper423\decaptcha\core\DeCaptchaAbstract::PARAM_SPEC_KEY '123456'); |
||
56 | // $bound = $getBaseUrlGetCodeCaller->bindTo($abstract, $abstract); |
||
57 | // $this->assertEquals('http://domain/res.php?key=123456&action=get_code&id=123', $bound()); |
||
58 | // $bound = $getBaseUrlGetBalanceCaller->bindTo($abstract, $abstract); |
||
59 | // $this->assertEquals('http://domain/res.php?key=123456&action=get_balance&id=234', $bound()); |
||
60 | // } |
||
61 | |||
62 | public function testGetFilePath() |
||
75 | |||
76 | /** |
||
77 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
78 | * @expectedExceptionCode 16 |
||
79 | */ |
||
80 | View Code Duplication | public function testGetFilePathErrorFileNotFound() |
|
89 | |||
90 | /** |
||
91 | * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
92 | * @expectedExceptionMessage Файл не загрузился: https://upload.wikimedia.org/wikipedia/commons/6/69/Captcha46.jpg123 |
||
93 | * @expectedExceptionCode 15 |
||
94 | */ |
||
95 | View Code Duplication | public function testGetFilePathErrorFileIsNotLoaded() |
|
104 | |||
105 | // public function testGetResponse() |
||
106 | // { |
||
107 | // $abstract = $this->newInstance(); |
||
108 | // $getResponseCaller = function ($val) { |
||
109 | // $this->domain = 'echo.jsontest.com/aaa/bbb'; |
||
110 | // return $this->getResponse($val); |
||
111 | // }; |
||
112 | // $bound = $getResponseCaller->bindTo($abstract, $abstract); |
||
113 | // $res = $bound(''); |
||
114 | // $this->assertEquals('{"res.php":"","aaa":"bbb"}', str_replace("\n", '', str_replace(' ', '', $res))); |
||
115 | // } |
||
116 | |||
117 | public function testExecutionDelayed() |
||
151 | |||
152 | // public function testGetInUrl() |
||
153 | // { |
||
154 | // $abstract = $this->newInstance(); |
||
155 | // $getInUrlCaller = function () { |
||
156 | // $this->domain = 'domain'; |
||
157 | // return $this->getInUrl(); |
||
158 | // }; |
||
159 | // $abstract->setApiKey('123456'); |
||
160 | // $bound = $getInUrlCaller->bindTo($abstract, $abstract); |
||
161 | // $this->assertEquals('http://domain/in.php', $bound()); |
||
162 | // } |
||
163 | |||
164 | // /** |
||
165 | // * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
166 | // * @expectedExceptionCode 4 |
||
167 | // */ |
||
168 | // public function testIsError() |
||
169 | // { |
||
170 | // $abstract = $this->newInstance(); |
||
171 | // $isErrorCaller = function ($val) { |
||
172 | // return $this->isError($val); |
||
173 | // }; |
||
174 | // $bound = $isErrorCaller->bindTo($abstract, $abstract); |
||
175 | // $bound('ERROR_IP_NOT_ALLOWED'); |
||
176 | // } |
||
177 | |||
178 | // public function testIsErrorNot() |
||
179 | // { |
||
180 | // $abstract = $this->newInstance(); |
||
181 | // $isErrorCaller = function ($val) { |
||
182 | // return $this->isError($val); |
||
183 | // }; |
||
184 | // $bound = $isErrorCaller->bindTo($abstract, $abstract); |
||
185 | // $this->assertNull($bound('BALANCE:56')); |
||
186 | // } |
||
187 | |||
188 | // /** |
||
189 | // * @expectedException \jumper423\decaptcha\core\DeCaptchaErrors |
||
190 | // * @expectedExceptionCode 17 |
||
191 | // * @expectedExceptionMessage Ошибка CURL: Could |
||
192 | // */ |
||
193 | // public function testGetCurlResponseError() |
||
194 | // { |
||
195 | // $abstract = $this->newInstance(); |
||
196 | // $getCurlResponseCaller = function ($url, $val) { |
||
197 | // return $this->getCurlResponse($url, $val); |
||
198 | // }; |
||
199 | // $bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
||
200 | // $bound('http://domain', ['protected' => 'value']); |
||
201 | // } |
||
202 | // |
||
203 | // public function testGetCurlResponse() |
||
204 | // { |
||
205 | // $abstract = $this->newInstance(); |
||
206 | // $getCurlResponseCaller = function ($url, $val) { |
||
207 | // return $this->getCurlResponse($url, $val); |
||
208 | // }; |
||
209 | // $bound = $getCurlResponseCaller->bindTo($abstract, $abstract); |
||
210 | // $data = $bound('http://httpbin.org/post', ['protected' => 'value']); |
||
211 | // $data = json_decode($data, true); |
||
212 | // $this->assertEquals(['protected' => 'value'], $data['form']); |
||
213 | // } |
||
214 | } |
||
215 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.