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 VariableTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | /** |
||
11 | * Test if variable value equals expected value |
||
12 | * |
||
13 | * @dataProvider variableProvider |
||
14 | */ |
||
15 | public function testGetValue($actual, $expected) |
||
21 | |||
22 | /** |
||
23 | * @expectedException \LogicException |
||
24 | * @expectedExceptionMessage Value is not set for variable "foo" |
||
25 | */ |
||
26 | public function testGetNullValueException() |
||
31 | |||
32 | View Code Duplication | public function testGetValueReturnsDefaultValueIfNoValueSet() |
|
|
|||
33 | { |
||
34 | $var = new Variable('foo', 'bar', false, false, true, new Location(1,1)); |
||
35 | $var->setDefaultValue('default-value'); |
||
36 | |||
37 | $this->assertEquals( |
||
38 | 'default-value', |
||
39 | $var->getValue() |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | View Code Duplication | public function testGetValueReturnsSetValueEvenWithDefaultValue() |
|
44 | { |
||
45 | $var = new Variable('foo', 'bar', false, false, true, new Location(1,1)); |
||
46 | $var->setValue('real-value'); |
||
47 | $var->setDefaultValue('default-value'); |
||
48 | |||
49 | $this->assertEquals( |
||
50 | 'real-value', |
||
51 | $var->getValue() |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | public function testIndicatesDefaultValuePresent() |
|
56 | { |
||
57 | $var = new Variable('foo', 'bar', false, false, true, new Location(1,1)); |
||
58 | $var->setDefaultValue('default-value'); |
||
59 | |||
60 | $this->assertTrue( |
||
61 | $var->hasDefaultValue() |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | public function testHasNoDefaultValue() |
||
66 | { |
||
67 | $var = new Variable('foo', 'bar', false, false, true, new Location(1,1)); |
||
68 | |||
69 | $this->assertFalse( |
||
70 | $var->hasDefaultValue() |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return array Array of <mixed: value to set, mixed: expected value> |
||
76 | */ |
||
77 | public static function variableProvider() |
||
86 | } |
||
87 |
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.