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 |
||
16 | abstract class AbstractProxy implements ProxyInterface |
||
17 | { |
||
18 | protected $port; |
||
19 | protected $binary; |
||
20 | protected $configFile; |
||
21 | protected $ip = '127.0.0.1'; |
||
22 | |||
23 | /** |
||
24 | * Wait for caching proxy to be started up and reachable |
||
25 | * |
||
26 | * @param string $ip |
||
27 | * @param int $port |
||
28 | * @param int $timeout Timeout in milliseconds |
||
29 | * |
||
30 | * @throws \RuntimeException If proxy is not reachable within timeout |
||
31 | */ |
||
32 | 31 | View Code Duplication | protected function waitFor($ip, $port, $timeout) |
49 | |||
50 | /** |
||
51 | * Wait for caching proxy to be started up and reachable |
||
52 | * |
||
53 | * @param string $ip |
||
54 | * @param int $port |
||
55 | * @param int $timeout Timeout in milliseconds |
||
56 | * |
||
57 | * @throws \RuntimeException If proxy is not reachable within timeout |
||
58 | */ |
||
59 | 23 | View Code Duplication | protected function waitUntil($ip, $port, $timeout) |
77 | |||
78 | 29 | protected function wait($timeout, $callback) |
|
90 | |||
91 | /** |
||
92 | * Run a shell command |
||
93 | * |
||
94 | * @param string $command |
||
95 | * @param array $arguments |
||
96 | * |
||
97 | * @throws \RuntimeException If command execution fails |
||
98 | */ |
||
99 | 29 | protected function runCommand($command, array $arguments) |
|
111 | |||
112 | |||
113 | /** |
||
114 | * @param int $port |
||
115 | */ |
||
116 | 29 | public function setPort($port) |
|
120 | |||
121 | /** |
||
122 | * @return int |
||
123 | */ |
||
124 | 31 | public function getPort() |
|
128 | |||
129 | /** |
||
130 | * Set Varnish binary (defaults to varnishd) |
||
131 | * |
||
132 | * @param string $binary |
||
133 | */ |
||
134 | 1 | public function setBinary($binary) |
|
138 | |||
139 | /** |
||
140 | * Get Varnish binary |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 31 | public function getBinary() |
|
148 | |||
149 | |||
150 | /** |
||
151 | * Set IP address (defaults to 127.0.0.1) |
||
152 | * |
||
153 | * @param string $ip |
||
154 | */ |
||
155 | 1 | public function setIp($ip) |
|
159 | |||
160 | /** |
||
161 | * Get IP address |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 6 | public function getIp() |
|
169 | |||
170 | /** |
||
171 | * @param string $configFile |
||
172 | * |
||
173 | * @throws \InvalidArgumentException |
||
174 | */ |
||
175 | 30 | public function setConfigFile($configFile) |
|
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | 31 | public function getConfigFile() |
|
191 | } |
||
192 |
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.