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 |
||
33 | class InclusionDirective extends BlockDirective implements DirectiveInterface |
||
34 | { |
||
35 | /** @var \WebHelper\Parser\Parser the parser instance */ |
||
36 | private $parser; |
||
37 | |||
38 | /** @var array file list pointed with that directive */ |
||
39 | private $files = []; |
||
40 | |||
41 | /** |
||
42 | * Specific constructor for inclusion directives. |
||
43 | * |
||
44 | * @param string $name the name of the key/context |
||
45 | * @param string $value the value of the key/context |
||
46 | * @param \WebHelper\Parser\Parser $parser the parser instance |
||
47 | */ |
||
48 | 4 | public function __construct($name, $value, Parser $parser) |
|
55 | |||
56 | /** |
||
57 | * Gets the detected files of the directive. |
||
58 | * |
||
59 | * @return array detected files |
||
60 | */ |
||
61 | 3 | public function getFiles() |
|
65 | |||
66 | /** |
||
67 | * Detects and memoizes the included files. |
||
68 | */ |
||
69 | 4 | public function setFiles() |
|
85 | |||
86 | /** |
||
87 | * Fills the block directives by compiling the memoized files. |
||
88 | */ |
||
89 | 4 | public function compileFiles() |
|
98 | |||
99 | /** |
||
100 | * Dumps the directive respecting a server syntax. |
||
101 | * |
||
102 | * @param ServerInterface $server a server instance |
||
103 | * @param int $spaces the indentation spaces |
||
104 | * |
||
105 | * @return string the dumped directive |
||
106 | */ |
||
107 | 1 | View Code Duplication | public function dump(ServerInterface $server, $spaces = 0) |
117 | } |
||
118 |
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.