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 |
||
13 | class ValidatorTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | |||
16 | use BasicConfigurationTrait; |
||
17 | |||
18 | /** |
||
19 | * @expectedException \Jalle19\StatusManager\Exception\InvalidConfigurationException |
||
20 | * @expectedExceptionMessageRegExp *Mandatory* |
||
21 | */ |
||
22 | public function testMandatoryValues() |
||
31 | |||
32 | |||
33 | /** |
||
34 | * @expectedException \Jalle19\StatusManager\Exception\InvalidConfigurationException |
||
35 | * @expectedExceptionMessageRegExp *The database path* |
||
36 | */ |
||
37 | View Code Duplication | public function testDatabasePath() |
|
45 | |||
46 | |||
47 | /** |
||
48 | * @expectedException \Jalle19\StatusManager\Exception\InvalidConfigurationException |
||
49 | * @expectedExceptionMessageRegExp *The log path* |
||
50 | */ |
||
51 | View Code Duplication | public function testLogPath() |
|
59 | |||
60 | |||
61 | /** |
||
62 | * @dataProvider updateIntervalProvider |
||
63 | * |
||
64 | * @param mixed $updateInterval |
||
65 | * |
||
66 | * @expectedException \Jalle19\StatusManager\Exception\InvalidConfigurationException |
||
67 | * @expectedExceptionMessageRegExp *Update interval cannot* |
||
68 | */ |
||
69 | public function testUpdateInterval($updateInterval) |
||
77 | |||
78 | |||
79 | /** |
||
80 | * @dataProvider listenPortProvider |
||
81 | * |
||
82 | * @param int $listenPort |
||
83 | * |
||
84 | * @expectedException \Jalle19\StatusManager\Exception\InvalidConfigurationException |
||
85 | * @expectedExceptionMessageRegExp *Listen port must be between* |
||
86 | */ |
||
87 | public function testListenPort($listenPort) |
||
95 | |||
96 | |||
97 | /** |
||
98 | * @return array |
||
99 | */ |
||
100 | public function updateIntervalProvider() |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function listenPortProvider() |
||
122 | |||
123 | } |
||
124 |
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.