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 |
||
8 | class AbstractDataMapperTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | |||
11 | |||
12 | /** |
||
13 | * @var AbstractDataMapper |
||
14 | */ |
||
15 | protected $object; |
||
16 | |||
17 | /** |
||
18 | * Sets up the fixture, for example, opens a network connection. |
||
19 | * This method is called before a test is executed. |
||
20 | */ |
||
21 | protected function setUp() |
||
22 | { |
||
23 | $this->object = $this->getMockBuilder('SimpleORM\AbstractDataMapper') |
||
24 | //->disableOriginalConstructor()//можно отключать |
||
|
|||
25 | // ->setConstructorArgs([ |
||
26 | // $DI, |
||
27 | // $adapter |
||
28 | // ])//в конструктор |
||
29 | ->disableOriginalConstructor() |
||
30 | //->setMethods(null) |
||
31 | ->setMethods(['setMappingFields','createEntity']) |
||
32 | //->setMethods(null) //не использовать заглушки методов иначе буде возвращать NULL |
||
33 | ->getMock(); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Tears down the fixture, for example, closes a network connection. |
||
38 | * This method is called after a test is executed. |
||
39 | */ |
||
40 | protected function tearDown() |
||
44 | |||
45 | /** |
||
46 | * @covers SimpleORM\AbstractDataMapper::AddMappingField |
||
47 | */ |
||
48 | View Code Duplication | public function testAddMappingField_SimpleField(){ |
|
49 | |||
50 | \TestHelper::callMethod($this->object,'addMappingField',['myfield']); |
||
51 | |||
52 | $correct = [ |
||
53 | 'myfield' => [ |
||
54 | 'field' => 'myfield' |
||
55 | ] |
||
56 | ]; |
||
57 | |||
58 | //----------------- |
||
59 | $mapping_fields = \TestHelper::getProtectedAttribute($this->object,'mapping_fields'); |
||
60 | |||
61 | $this->assertEquals($mapping_fields, $correct); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @covers SimpleORM\AbstractDataMapper::AddMappingField |
||
66 | */ |
||
67 | View Code Duplication | public function testAddMappingField_FieldAndAlias(){ |
|
68 | |||
69 | \TestHelper::callMethod($this->object,'addMappingField',['myfield','tb_mayfield']); |
||
70 | |||
71 | $correct = [ |
||
72 | 'myfield' => [ |
||
73 | 'field' => 'tb_mayfield' |
||
74 | ] |
||
75 | ]; |
||
76 | |||
77 | //----------------- |
||
78 | $mapping_fields = \TestHelper::getProtectedAttribute($this->object,'mapping_fields'); |
||
79 | |||
80 | $this->assertEquals($mapping_fields, $correct); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @covers SimpleORM\AbstractDataMapper::AddMappingField |
||
85 | */ |
||
86 | View Code Duplication | public function testAddMappingField_ArrayField(){ |
|
87 | |||
88 | \TestHelper::callMethod($this->object,'addMappingField',['myfield',[ |
||
89 | 'field' => 'tb_mayfield' |
||
90 | ]]); |
||
91 | |||
92 | $correct = [ |
||
93 | 'myfield' => [ |
||
94 | 'field' => 'tb_mayfield' |
||
95 | ] |
||
96 | ]; |
||
97 | |||
98 | //----------------- |
||
99 | $mapping_fields = \TestHelper::getProtectedAttribute($this->object,'mapping_fields'); |
||
100 | |||
101 | $this->assertEquals($mapping_fields, $correct); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @covers SimpleORM\AbstractDataMapper::AddMappingField |
||
106 | */ |
||
107 | View Code Duplication | public function testAddMappingField_PrimaryKey(){ |
|
129 | |||
130 | /** |
||
131 | * @covers SimpleORM\AbstractDataMapper::AddMappingField |
||
132 | */ |
||
133 | View Code Duplication | public function testAddMappingField_SoftDelete(){ |
|
155 | |||
156 | /** |
||
157 | * @covers SimpleORM\AbstractDataMapper::getPrimaryKey |
||
158 | */ |
||
159 | public function testGetPrimaryKey() |
||
164 | |||
165 | /** |
||
166 | * @covers SimpleORM\AbstractDataMapper::setSoftDeleteKey |
||
167 | */ |
||
168 | public function testsetSoftDeleteKey() |
||
173 | |||
174 | /** |
||
175 | * @covers SimpleORM\AbstractDataMapper::getFieldAlias |
||
176 | */ |
||
177 | View Code Duplication | public function testGetFieldAlias(){ |
|
184 | |||
185 | |||
186 | /** |
||
187 | * @covers SimpleORM\AbstractDataMapper::getAdapter |
||
188 | * @todo Implement testGetAdapter(). |
||
189 | */ |
||
190 | public function testGetAdapter() |
||
197 | |||
198 | /** |
||
199 | * @covers SimpleORM\AbstractDataMapper::setAdapter |
||
200 | * @todo Implement testSetAdapter(). |
||
201 | */ |
||
202 | public function testSetAdapter() |
||
209 | |||
210 | /** |
||
211 | * @covers SimpleORM\AbstractDataMapper::findById |
||
212 | * @todo Implement testFindById(). |
||
213 | */ |
||
214 | public function testFindById() |
||
221 | |||
222 | /** |
||
223 | * @covers SimpleORM\AbstractDataMapper::save |
||
224 | * @todo Implement testSave(). |
||
225 | */ |
||
226 | public function testSave() |
||
233 | |||
234 | |||
235 | /** |
||
236 | * @covers SimpleORM\AbstractDataMapper::findBySpecification |
||
237 | * @todo Implement testFindBySpecification(). |
||
238 | */ |
||
239 | public function testFindBySpecification() |
||
246 | |||
247 | /** |
||
248 | * @covers SimpleORM\AbstractDataMapper::delete |
||
249 | * @todo Implement testDelete(). |
||
250 | */ |
||
251 | public function testDelete() |
||
258 | |||
259 | /** |
||
260 | * @covers SimpleORM\AbstractDataMapper::findAllBySpecification |
||
261 | * @todo Implement testFindAllBySpecification(). |
||
262 | */ |
||
263 | public function testFindAllBySpecification() |
||
270 | |||
271 | /** |
||
272 | * @covers SimpleORM\AbstractDataMapper::findAll |
||
273 | * @todo Implement testFindAll(). |
||
274 | */ |
||
275 | public function testFindAll() |
||
282 | |||
283 | /** |
||
284 | * @covers SimpleORM\AbstractDataMapper::useJoins |
||
285 | * @todo Implement testUseJoins(). |
||
286 | */ |
||
287 | public function testUseJoins() |
||
294 | |||
295 | /** |
||
296 | * @covers SimpleORM\AbstractDataMapper::withDelete |
||
297 | * @todo Implement testWithDelete(). |
||
298 | */ |
||
299 | public function testWithDelete() |
||
306 | } |
||
307 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.