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 BasicObjectTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | /** |
||
11 | * @covers \Kint\Object\BasicObject::__construct |
||
12 | * @covers \Kint\Object\BasicObject::getRepresentations |
||
13 | * @covers \Kint\Object\BasicObject::addRepresentation |
||
14 | */ |
||
15 | public function testAddRepresentation() |
||
16 | { |
||
17 | $o = new BasicObject(); |
||
|
|||
18 | |||
19 | $this->assertSame(array(), $o->getRepresentations()); |
||
20 | $this->assertSame(null, $o->value); |
||
21 | |||
22 | $this->assertTrue($o->addRepresentation($r1 = new Representation('Rep 1'))); |
||
23 | $this->assertSame( |
||
24 | array( |
||
25 | 'rep_1' => $r1, |
||
26 | ), |
||
27 | $o->getRepresentations() |
||
28 | ); |
||
29 | $this->assertSame(null, $o->value); |
||
30 | |||
31 | $this->assertFalse($o->addRepresentation($r1)); |
||
32 | $this->assertSame( |
||
33 | array( |
||
34 | 'rep_1' => $r1, |
||
35 | ), |
||
36 | $o->getRepresentations() |
||
37 | ); |
||
38 | |||
39 | $this->assertTrue($o->addRepresentation($r2 = new Representation('Rep 2'))); |
||
40 | $this->assertSame( |
||
41 | array( |
||
42 | 'rep_1' => $r1, |
||
43 | 'rep_2' => $r2, |
||
44 | ), |
||
45 | $o->getRepresentations() |
||
46 | ); |
||
47 | |||
48 | $this->assertTrue($o->addRepresentation($r3 = new Representation('Rep 3'), 0)); |
||
49 | $this->assertSame( |
||
50 | array( |
||
51 | 'rep_3' => $r3, |
||
52 | 'rep_1' => $r1, |
||
53 | 'rep_2' => $r2, |
||
54 | ), |
||
55 | $o->getRepresentations() |
||
56 | ); |
||
57 | |||
58 | $this->assertTrue($o->addRepresentation($r4 = new Representation('Rep 4'), 1)); |
||
59 | $this->assertSame( |
||
60 | array( |
||
61 | 'rep_3' => $r3, |
||
62 | 'rep_4' => $r4, |
||
63 | 'rep_1' => $r1, |
||
64 | 'rep_2' => $r2, |
||
65 | ), |
||
66 | $o->getRepresentations() |
||
67 | ); |
||
68 | |||
69 | $this->assertTrue($o->addRepresentation($r5 = new Representation('Rep 5'), 100)); |
||
70 | $this->assertSame( |
||
71 | array( |
||
72 | 'rep_3' => $r3, |
||
73 | 'rep_4' => $r4, |
||
74 | 'rep_1' => $r1, |
||
75 | 'rep_2' => $r2, |
||
76 | 'rep_5' => $r5, |
||
77 | ), |
||
78 | $o->getRepresentations() |
||
79 | ); |
||
80 | |||
81 | $this->assertTrue($o->addRepresentation($r6 = new Representation('Rep 6'), -100)); |
||
82 | $this->assertSame( |
||
83 | array( |
||
84 | 'rep_6' => $r6, |
||
85 | 'rep_3' => $r3, |
||
86 | 'rep_4' => $r4, |
||
87 | 'rep_1' => $r1, |
||
88 | 'rep_2' => $r2, |
||
89 | 'rep_5' => $r5, |
||
90 | ), |
||
91 | $o->getRepresentations() |
||
92 | ); |
||
93 | |||
94 | $this->assertSame(null, $o->value); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @covers \Kint\Object\BasicObject::replaceRepresentation |
||
99 | */ |
||
100 | public function testReplaceRepresentation() |
||
149 | |||
150 | /** |
||
151 | * @covers \Kint\Object\BasicObject::removeRepresentation |
||
152 | */ |
||
153 | public function testRemoveRepresentation() |
||
182 | |||
183 | /** |
||
184 | * @covers \Kint\Object\BasicObject::getRepresentation |
||
185 | */ |
||
186 | public function testGetRepresentation() |
||
198 | |||
199 | /** |
||
200 | * @covers \Kint\Object\BasicObject::getRepresentations |
||
201 | */ |
||
202 | public function testGetRepresentations() |
||
218 | |||
219 | /** |
||
220 | * @covers \Kint\Object\BasicObject::clearRepresentations |
||
221 | */ |
||
222 | public function testClearRepresentations() |
||
223 | { |
||
224 | $o = new BasicObject(); |
||
225 | $o->addRepresentation($r1 = new Representation('Rep 1')); |
||
226 | $o->addRepresentation(new Representation('Rep 2')); |
||
227 | $o->addRepresentation(new Representation('Rep 3')); |
||
228 | $o->value = $r1; |
||
229 | |||
230 | $o->clearRepresentations(); |
||
231 | |||
232 | $this->assertSame(array(), $o->getRepresentations()); |
||
233 | $this->assertSame($r1, $o->value); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @covers \Kint\Object\BasicObject::getType |
||
238 | */ |
||
239 | public function testGetType() |
||
245 | |||
246 | public function modifierProvider() |
||
247 | { |
||
305 | |||
306 | /** |
||
307 | * @dataProvider modifierProvider |
||
308 | * @covers \Kint\Object\BasicObject::getModifiers |
||
309 | */ |
||
310 | public function testGetModifiers($const, $static, $access, $expect) |
||
318 | |||
319 | /** |
||
320 | * @covers \Kint\Object\BasicObject::getAccess |
||
321 | */ |
||
322 | public function testGetAccess() |
||
333 | |||
334 | /** |
||
335 | * @covers \Kint\Object\BasicObject::getName |
||
336 | */ |
||
337 | View Code Duplication | public function testGetName() |
|
347 | |||
348 | /** |
||
349 | * @covers \Kint\Object\BasicObject::getOperator |
||
350 | */ |
||
351 | public function testGetOperator() |
||
364 | |||
365 | /** |
||
366 | * @covers \Kint\Object\BasicObject::getSize |
||
367 | */ |
||
368 | View Code Duplication | public function testGetSize() |
|
377 | |||
378 | /** |
||
379 | * @covers \Kint\Object\BasicObject::getValueShort |
||
380 | */ |
||
381 | public function testGetValueShort() |
||
407 | |||
408 | /** |
||
409 | * @covers \Kint\Object\BasicObject::getAccessPath |
||
410 | */ |
||
411 | public function testGetAccessPath() |
||
418 | |||
419 | /** |
||
420 | * @covers \Kint\Object\BasicObject::blank |
||
421 | */ |
||
422 | public function testBlank() |
||
440 | |||
441 | /** |
||
442 | * @covers \Kint\Object\BasicObject::transplant |
||
443 | */ |
||
444 | public function testTransplant() |
||
480 | |||
481 | /** |
||
482 | * @covers \Kint\Object\BasicObject::sortByAccess |
||
483 | */ |
||
484 | public function testSortByAccess() |
||
507 | |||
508 | /** |
||
509 | * @covers \Kint\Object\BasicObject::sortByName |
||
510 | */ |
||
511 | public function testSortByName() |
||
540 | } |
||
541 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.