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 |
||
19 | class AbstractEntryPointTest extends \PHPUnit_Framework_TestCase { |
||
20 | |||
21 | public static function setUpBeforeClass() |
||
24 | |||
25 | public static function tearDownAfterClass() |
||
28 | |||
29 | protected $Stub; |
||
30 | protected $url = 'http://localhost/rest/v10/'; |
||
31 | protected $options = array('foo'); |
||
32 | protected $data = array( |
||
33 | 'foo' => 'bar' |
||
34 | ); |
||
35 | |||
36 | public function setUp() |
||
40 | |||
41 | public function tearDown() |
||
45 | |||
46 | /** |
||
47 | * @return EntryPointStub $Stub |
||
48 | * @covers ::__construct |
||
49 | * @group abstractAP |
||
50 | */ |
||
51 | public function testConstructor(){ |
||
69 | |||
70 | /** |
||
71 | * @param EntryPointStub $Stub |
||
72 | * @return EntryPointStub $Stub |
||
73 | * @depends testConstructor |
||
74 | * @covers ::setOptions |
||
75 | * @covers ::getOptions |
||
76 | * @covers ::getUrl |
||
77 | * @covers ::configureUrl |
||
78 | * @group abstractEP |
||
79 | */ |
||
80 | public function testSetOptions($Stub){ |
||
90 | |||
91 | /** |
||
92 | * @param EntryPointStub $Stub |
||
93 | * @return EntryPointStub $Stub |
||
94 | * @depends testSetOptions |
||
95 | * @covers ::setData |
||
96 | * @covers ::getData |
||
97 | * @group abstractEP |
||
98 | */ |
||
99 | public function testSetData($Stub){ |
||
112 | |||
113 | /** |
||
114 | * @param EntryPointStub $Stub |
||
115 | * @return EntryPointStub $Stub |
||
116 | * @depends testSetData |
||
117 | * @covers ::setRequest |
||
118 | * @covers ::getRequest |
||
119 | * @group abstractEP |
||
120 | */ |
||
121 | public function testSetRequest($Stub){ |
||
147 | |||
148 | /** |
||
149 | * @param EntryPointStub $Stub |
||
150 | * @return EntryPointStub $Stub |
||
151 | * @depends testSetRequest |
||
152 | * @covers ::setAuth |
||
153 | * @covers ::authRequired |
||
154 | * @group abstractEP |
||
155 | */ |
||
156 | public function testSetAuth($Stub){ |
||
161 | |||
162 | /** |
||
163 | * @param EntryPointStub $Stub |
||
164 | * @return EntryPointStub $Stub |
||
165 | * @depends testSetAuth |
||
166 | * @covers ::execute |
||
167 | * @covers ::configureRequest |
||
168 | * @covers ::verifyUrl |
||
169 | * @covers ::verifyData |
||
170 | * @covers ::verifyRequiredData |
||
171 | * @covers ::verifyDataType |
||
172 | * @covers ::verifyOptions |
||
173 | * @covers ::configureData |
||
174 | * @covers ::configureDefaultData |
||
175 | * @covers ::configureUrl |
||
176 | * @covers ::configureAuth |
||
177 | * @group abstractEP |
||
178 | */ |
||
179 | public function testExecute($Stub){ |
||
188 | |||
189 | /** |
||
190 | * @covers ::verifyDataType |
||
191 | * @expectedException SugarAPI\SDK\Exception\EntryPoint\RequiredDataException |
||
192 | * @expectedExceptionMessageRegExp /Valid DataType is array/ |
||
193 | * @group abstractEP |
||
194 | */ |
||
195 | public function testInvalidDataType(){ |
||
204 | |||
205 | /** |
||
206 | * @covers ::verifyRequiredData |
||
207 | * @expectedException SugarAPI\SDK\Exception\EntryPoint\RequiredDataException |
||
208 | * @expectedExceptionMessageRegExp /Missing data for/ |
||
209 | * @group abstractEP |
||
210 | */ |
||
211 | View Code Duplication | public function testInvalidData(){ |
|
218 | |||
219 | /** |
||
220 | * @covers ::execute |
||
221 | * @expectedException SugarAPI\SDK\Exception\EntryPoint\InvalidRequestException |
||
222 | * @expectedExceptionMessageRegExp /Request property not configured/ |
||
223 | * @group abstractEP |
||
224 | */ |
||
225 | View Code Duplication | public function testInvalidRequest(){ |
|
231 | |||
232 | |||
233 | |||
234 | } |
||
235 |
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.