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 |
||
23 | View Code Duplication | abstract class CommandBuilder implements ApplicationBuilder |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * Configuration array. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $configuration; |
||
31 | |||
32 | /** |
||
33 | * The container builder. |
||
34 | * |
||
35 | * @var ContainerBuilder |
||
36 | */ |
||
37 | protected $container; |
||
38 | |||
39 | /** |
||
40 | * The persistence driver. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $persistence; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | * @param ContainerBuilder $container The container builder |
||
50 | * @param string $persistence The persistence driver |
||
51 | * @param array $configuration The configuration tree |
||
52 | */ |
||
53 | public function __construct(ContainerBuilder $container, $persistence, array $configuration = []) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function build($file) |
||
74 | |||
75 | /** |
||
76 | * Gets the command definition name. |
||
77 | * |
||
78 | * @param string $file The file name |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | abstract protected function definitionName($file); |
||
83 | |||
84 | /** |
||
85 | * Gets the command definition name alias. |
||
86 | * |
||
87 | * @param string $file The file name |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | abstract protected function aliasDefinitionName($file); |
||
92 | |||
93 | /** |
||
94 | * Registers the command into container. |
||
95 | * |
||
96 | * @param string $file The file name |
||
97 | */ |
||
98 | abstract protected function register($file); |
||
99 | } |
||
100 |
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.