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 |
||
21 | class Server implements ServerInterface |
||
22 | { |
||
23 | /** |
||
24 | * a Checker instance. |
||
25 | * |
||
26 | * @var \WebHelper\Parser\Parser\Checker |
||
27 | */ |
||
28 | private $checker; |
||
29 | |||
30 | /** |
||
31 | * The filesystem path where the web server is installed. |
||
32 | * |
||
33 | * It has to be an absolute path. |
||
34 | * |
||
35 | * Apache httpd server does not accept a relative prefix path at compilation. |
||
36 | * Nginx does, but this is a very risky practice... |
||
37 | * So relative prefix path in nginx configuration will not be considered |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $prefix = ''; |
||
42 | |||
43 | /** |
||
44 | * The string to match as a starting multi-line directive. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $startMultiLine = ''; |
||
49 | |||
50 | /** |
||
51 | * The string to match as an ending multi-line directive. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $endMultiLine = ''; |
||
56 | |||
57 | /** |
||
58 | * The string to match a simple directive. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $simpleDirective = ''; |
||
63 | |||
64 | /** |
||
65 | * binaries that can be used to control the webserver. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | private $binaries = []; |
||
70 | |||
71 | /** |
||
72 | * the parameter string to use to detect version and config file. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $detectionParameter = ''; |
||
77 | |||
78 | /** |
||
79 | * The ordered list of methods to apply before convertion. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | private $beforeMethods = []; |
||
84 | |||
85 | /** |
||
86 | * The ordered list of methods to apply after convertion. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | private $afterMethods = []; |
||
91 | |||
92 | /** |
||
93 | * The simple directive syntax when dumped. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $dumperSimpleDirective = ''; |
||
98 | |||
99 | /** |
||
100 | * The starting block directive syntax when dumped. |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | private $dumperStartDirective = ''; |
||
105 | |||
106 | /** |
||
107 | * The ending block directive syntax when dumped. |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | private $dumperEndDirective = ''; |
||
112 | |||
113 | /** |
||
114 | * The known directives of the server |
||
115 | * |
||
116 | * @var array |
||
117 | */ |
||
118 | private $knownDirectives = []; |
||
119 | |||
120 | /** |
||
121 | * Sets the Checker instance. |
||
122 | * |
||
123 | * @param Checker $checker a Checker instance |
||
124 | */ |
||
125 | 30 | public function setChecker(Checker $checker) |
|
131 | |||
132 | /** |
||
133 | * Confirms if the server instance has valid parameters. |
||
134 | * |
||
135 | * @return bool true if all parameters are initialized, false otherwise |
||
136 | */ |
||
137 | 2 | public function isValid() |
|
146 | |||
147 | /** |
||
148 | * Getter for the prefix. |
||
149 | * |
||
150 | * @return string the filesystem path where the web server is installed |
||
151 | */ |
||
152 | 4 | public function getPrefix() |
|
156 | |||
157 | /** |
||
158 | * Sets the prefix of a server instance. |
||
159 | * |
||
160 | * |
||
161 | * @param string $prefix the filesystem path where the web server is installed |
||
162 | * |
||
163 | * @throws ServerException if the prefix is invalid |
||
164 | */ |
||
165 | 11 | public function setPrefix($prefix) |
|
182 | |||
183 | /** |
||
184 | * Gets the regexp that will match the starting block directives. |
||
185 | * |
||
186 | * @return string the regexp that will match the starting block directives |
||
187 | */ |
||
188 | 8 | public function getStartMultiLine() |
|
192 | |||
193 | /** |
||
194 | * Sets the regexp that will match the starting block directives. |
||
195 | * |
||
196 | * @param string $startMultiLine the regexp that will match the starting block directives |
||
197 | */ |
||
198 | 13 | public function setStartMultiLine($startMultiLine) |
|
211 | |||
212 | /** |
||
213 | * Gets the regexp that will match the ending block directives. |
||
214 | * |
||
215 | * @return string the regexp that will match the ending block directives |
||
216 | */ |
||
217 | 8 | public function getEndMultiLine() |
|
221 | |||
222 | /** |
||
223 | * Sets the regexp that will match the ending block directives. |
||
224 | * |
||
225 | * @param string $endMultiLine the regexp that will match the ending block directives |
||
226 | */ |
||
227 | 10 | public function setEndMultiLine($endMultiLine) |
|
237 | |||
238 | /** |
||
239 | * Gets the regexp that will match the simple directives. |
||
240 | * |
||
241 | * @return string the regexp that will match the simple directives |
||
242 | */ |
||
243 | 8 | public function getSimpleDirective() |
|
247 | |||
248 | /** |
||
249 | * Sets the regexp that will match the simple directives. |
||
250 | * |
||
251 | * @param string $simpleDirective the regexp that will match the simple directives |
||
252 | */ |
||
253 | 13 | public function setSimpleDirective($simpleDirective) |
|
266 | |||
267 | /** |
||
268 | * Sets the regular expression directive. |
||
269 | * |
||
270 | * @param string $directive the directive string |
||
271 | * @param string $message1 message exception if the matcher is not a string |
||
272 | * @param string $message2 message exception if the matcher is not a valid regex |
||
273 | * |
||
274 | * @throws ServerException if the directive matcher is invalid |
||
275 | * |
||
276 | * @return string the regular expression directive |
||
277 | */ |
||
278 | 10 | View Code Duplication | private function setRegexDirective($directive, $message1, $message2) |
296 | |||
297 | /** |
||
298 | * Confirms if a directive matcher is a valid regex. |
||
299 | * |
||
300 | * @param string $directive the directive matcher to check |
||
301 | * @param string $message1 message exception if the matcher is not a string |
||
302 | * @param string $message2 message exception if the matcher is not a valid regex |
||
303 | * |
||
304 | * @throws ServerException if the directive matcher is invalid |
||
305 | * |
||
306 | * @return bool true if the directive matcher is valid |
||
307 | */ |
||
308 | 19 | View Code Duplication | private function isValidDirective($directive, $message1, $message2) |
326 | |||
327 | /** |
||
328 | * Gets the list of binaries that can be run to analyze. |
||
329 | * |
||
330 | * @return array the list of binaries that can be run |
||
331 | */ |
||
332 | 1 | public function getBinaries() |
|
336 | |||
337 | /** |
||
338 | * Sets the list of binaries that can be run to analyze. |
||
339 | * |
||
340 | * @param array $binaries list of controlers |
||
341 | */ |
||
342 | 8 | public function setBinaries(array $binaries) |
|
348 | |||
349 | /** |
||
350 | * Gets the parameter string to use to detect version and config file. |
||
351 | * |
||
352 | * @return string parameter string |
||
353 | */ |
||
354 | 1 | public function getDetectionParameter() |
|
358 | |||
359 | /** |
||
360 | * Sets the parameter string to use to detect version and config file. |
||
361 | * |
||
362 | * @param string $parameter parameter string |
||
363 | */ |
||
364 | 8 | public function setDetectionParameter($parameter = '') |
|
370 | |||
371 | /** |
||
372 | * Gets the ordered list of methods to apply before the config file turns into an array. |
||
373 | * |
||
374 | * @return array the ordered list of methods to apply before convertion |
||
375 | */ |
||
376 | 14 | public function getBeforeMethods() |
|
380 | |||
381 | /** |
||
382 | * Sets the ordered list of methods to apply before the config file turns into an array. |
||
383 | * |
||
384 | * @param array $methods the ordered list of methods to apply before convertion |
||
385 | */ |
||
386 | 13 | public function setBeforeMethods(array $methods) |
|
392 | |||
393 | /** |
||
394 | * Gets the ordered list of methods to apply after the config file has turned into an array. |
||
395 | * |
||
396 | * @return array the ordered list of methods to apply after convertion |
||
397 | */ |
||
398 | 14 | public function getAfterMethods() |
|
402 | |||
403 | /** |
||
404 | * Sets the ordered list of methods to apply after the config file has turned into an array. |
||
405 | * |
||
406 | * @param array $methods the ordered list of methods to apply after convertion |
||
407 | */ |
||
408 | 13 | public function setAfterMethods(array $methods) |
|
414 | |||
415 | /** |
||
416 | * Gets the simple directive syntax when dumped. |
||
417 | * |
||
418 | * @return string the simple directive syntax when dumped |
||
419 | */ |
||
420 | 1 | public function getDumperSimpleDirective() |
|
424 | |||
425 | /** |
||
426 | * Gets the starting block directive syntax when dumped. |
||
427 | * |
||
428 | * @return string the starting block directive syntax when dumped |
||
429 | */ |
||
430 | 1 | public function getDumperStartDirective() |
|
434 | |||
435 | /** |
||
436 | * Gets the ending block directive syntax when dumped. |
||
437 | * |
||
438 | * @return string the ending block directive syntax when dumped |
||
439 | */ |
||
440 | 1 | public function getDumperEndDirective() |
|
444 | |||
445 | /** |
||
446 | * Sets the simple directive syntax when dumped. |
||
447 | * |
||
448 | * @param string $simpleDirective the simple directive syntax when dumped |
||
449 | */ |
||
450 | 8 | public function setDumperSimpleDirective($simpleDirective) |
|
456 | |||
457 | /** |
||
458 | * Sets the starting block directive syntax when dumped. |
||
459 | * |
||
460 | * @param string $startMultiLine the starting block directive syntax when dumped |
||
461 | */ |
||
462 | 8 | public function setDumperStartDirective($startMultiLine) |
|
468 | |||
469 | /** |
||
470 | * Sets the ending block directive syntax when dumped. |
||
471 | * |
||
472 | * @param string $endMultiLine the ending block directive syntax when dumped |
||
473 | */ |
||
474 | 8 | public function setDumperEndDirective($endMultiLine) |
|
480 | |||
481 | /** |
||
482 | * Gets the known directives of the server. |
||
483 | * |
||
484 | * @return array the known directives of the server |
||
485 | */ |
||
486 | 9 | public function getKnownDirectives() |
|
490 | |||
491 | /** |
||
492 | * Sets the known directives of the server. |
||
493 | * |
||
494 | * @param array $knownDirectives the known directives of the server |
||
495 | */ |
||
496 | 7 | public function setKnownDirectives(array $knownDirectives) |
|
502 | } |
||
503 |
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.