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 |
||
29 | class BaseMetadataFactory implements MetadataFactoryInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $aliasMap = []; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $classMap = []; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $phpcrTypeMap = []; |
||
45 | |||
46 | /** |
||
47 | * @var EventDispatcherInterface |
||
48 | */ |
||
49 | private $dispatcher; |
||
50 | |||
51 | /** |
||
52 | * @var Metadata[] |
||
53 | */ |
||
54 | private $metadata = []; |
||
55 | |||
56 | /** |
||
57 | * @param array $mapping |
||
58 | */ |
||
59 | public function __construct(EventDispatcherInterface $dispatcher, array $mapping) |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | View Code Duplication | public function getMetadataForAlias($alias) |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | View Code Duplication | public function getMetadataForPhpcrType($phpcrType) |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function hasMetadataForPhpcrType($phpcrType) |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function hasMetadataForClass($class) |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | View Code Duplication | public function getMetadataForClass($class) |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function hasAlias($alias) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function getAliases() |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function getMetadataForPhpcrNode(NodeInterface $node) |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function getPhpcrTypeMap() |
||
174 | |||
175 | /** |
||
176 | * @param array $mapping |
||
177 | * |
||
178 | * @return Metadata |
||
179 | */ |
||
180 | private function loadMetadata($mapping) |
||
213 | } |
||
214 |
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.