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 |
||
36 | View Code Duplication | class WebServerListener6 implements TestListener |
|
|
|||
37 | { |
||
38 | /** @var WebServerListenerTrait */ |
||
39 | private $trait; |
||
40 | |||
41 | public function __construct() |
||
45 | |||
46 | /** |
||
47 | * Make sure the PHP built-in web server is running for tests with group |
||
48 | * 'webserver'. |
||
49 | */ |
||
50 | public function startTestSuite(TestSuite $suite) |
||
54 | |||
55 | /** |
||
56 | * We don't need these. |
||
57 | */ |
||
58 | public function endTestSuite(TestSuite $suite) |
||
61 | |||
62 | public function addError(Test $test, \Exception $e, $time) |
||
65 | |||
66 | public function addFailure(Test $test, AssertionFailedError $e, $time) |
||
69 | |||
70 | public function addIncompleteTest(Test $test, \Exception $e, $time) |
||
73 | |||
74 | public function addSkippedTest(Test $test, \Exception $e, $time) |
||
77 | |||
78 | public function startTest(Test $test) |
||
81 | |||
82 | public function endTest(Test $test, $time) |
||
85 | |||
86 | public function addRiskyTest(Test $test, \Exception $e, $time) |
||
89 | |||
90 | public function addWarning(Test $test, Warning $e, $time) |
||
93 | |||
94 | /** |
||
95 | * Get web server hostname. |
||
96 | * |
||
97 | * @throws \Exception |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | protected function getHostName() |
||
105 | |||
106 | /** |
||
107 | * Get web server port. |
||
108 | * |
||
109 | * @throws \Exception |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | protected function getPort() |
||
117 | |||
118 | /** |
||
119 | * Get web server port. |
||
120 | * |
||
121 | * @throws \Exception |
||
122 | * |
||
123 | * @return int |
||
124 | */ |
||
125 | protected function getDocRoot() |
||
129 | |||
130 | /** |
||
131 | * Start PHP built-in web server. |
||
132 | * |
||
133 | * @return int PID |
||
134 | */ |
||
135 | protected function startPhpWebServer() |
||
139 | |||
140 | /** |
||
141 | * Wait for caching proxy to be started up and reachable. |
||
142 | * |
||
143 | * @param string $ip |
||
144 | * @param int $port |
||
145 | * @param int $timeout Timeout in milliseconds |
||
146 | * |
||
147 | * @throws \RuntimeException If proxy is not reachable within timeout |
||
148 | */ |
||
149 | protected function waitFor($ip, $port, $timeout) |
||
153 | } |
||
154 |
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.