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 |
||
30 | class SelfTestCliArguments extends AbstractSelfTest |
||
31 | { |
||
32 | /** |
||
33 | * The output buffer to keep track of the detection. |
||
34 | * |
||
35 | * @var BufferedOutput |
||
36 | */ |
||
37 | private $log; |
||
38 | |||
39 | /** |
||
40 | * The interpreter to use. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | private $interpreter; |
||
45 | |||
46 | /** |
||
47 | * Check that we have a correct CLI executable of PHP. |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | public function doTest() |
||
75 | |||
76 | /** |
||
77 | * Check the needed parameters. |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | private function check() |
||
87 | |||
88 | /** |
||
89 | * Test if raising the memory limit is needed. |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | View Code Duplication | private function testMemoryLimit() |
|
105 | |||
106 | /** |
||
107 | * Test if raising the memory limit is needed. |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | View Code Duplication | private function testMaxExecutionTime() |
|
123 | |||
124 | /** |
||
125 | * Test if overriding a parameter works. |
||
126 | * |
||
127 | * @param string $script The script to run. |
||
128 | * |
||
129 | * @param string $definition The argument to pass. |
||
130 | * |
||
131 | * @param string $expectedValue The expected output value. |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | private function testOverride($script, $definition, $expectedValue) |
||
145 | |||
146 | /** |
||
147 | * Runs the passed test script through the php cli and returns the output. |
||
148 | * |
||
149 | * @param string $testScript The test script to run. |
||
150 | * |
||
151 | * @param string $definition Optional definition to override. |
||
152 | * |
||
153 | * @return null|string |
||
154 | */ |
||
155 | private function testCliRuntime($testScript, $definition = '') |
||
176 | } |
||
177 |
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.