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 |
||
13 | class YamlDriver extends FileDriver |
||
14 | { |
||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | public function load(SchemaContainer $schema) |
||
26 | |||
27 | /** |
||
28 | * @param string $path |
||
29 | * @param SchemaContainer $schemaContainer |
||
30 | */ |
||
31 | private function loadFile($path, SchemaContainer $schemaContainer) |
||
59 | |||
60 | /** |
||
61 | * @param SchemaContainer $schemaContainer |
||
62 | * @param array $mapping |
||
63 | */ |
||
64 | View Code Duplication | private function mapQuery(SchemaContainer $schemaContainer, array $mapping) |
|
74 | |||
75 | /** |
||
76 | * @param SchemaContainer $schemaContainer |
||
77 | * @param array $mapping |
||
78 | */ |
||
79 | View Code Duplication | private function mapMutation(SchemaContainer $schemaContainer, array $mapping) |
|
89 | |||
90 | /** |
||
91 | * @param SchemaContainer $schemaContainer |
||
92 | * @param array $mapping |
||
93 | */ |
||
94 | private function mapTypes(SchemaContainer $schemaContainer, array $mapping) |
||
101 | |||
102 | /** |
||
103 | * @param SchemaContainer $schemaContainer |
||
104 | * @param array $mapping |
||
105 | */ |
||
106 | private function mapInterfaces(SchemaContainer $schemaContainer, array $mapping) |
||
113 | |||
114 | /** |
||
115 | * @param string $name |
||
116 | * @param array $mapping |
||
117 | * @return Type |
||
118 | */ |
||
119 | private function createType($name, array $mapping) |
||
139 | |||
140 | /** |
||
141 | * @param AbstractType $type |
||
142 | * @param array $mapping |
||
143 | */ |
||
144 | private function populateType(AbstractType $type, array $mapping) |
||
150 | |||
151 | /** |
||
152 | * @param FieldContainer $type |
||
153 | * @param array $mapping |
||
154 | */ |
||
155 | private function populateFieldContainer(FieldContainer $type, array $mapping) |
||
169 | |||
170 | /** |
||
171 | * @param string $name |
||
172 | * @param array $mapping |
||
173 | * @return InterfaceType |
||
174 | */ |
||
175 | private function createInterface($name, array $mapping) |
||
183 | |||
184 | /** |
||
185 | * @param string $name |
||
186 | * @param array $mapping |
||
187 | * @return Field |
||
188 | */ |
||
189 | private function createField($name, array $mapping) |
||
210 | } |
||
211 |
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.