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) |
||
54 | |||
55 | /** |
||
56 | * @param SchemaContainer $schemaContainer |
||
57 | * @param array $mapping |
||
58 | */ |
||
59 | View Code Duplication | private function mapQuery(SchemaContainer $schemaContainer, array $mapping) |
|
69 | |||
70 | /** |
||
71 | * @param SchemaContainer $schemaContainer |
||
72 | * @param array $mapping |
||
73 | */ |
||
74 | View Code Duplication | private function mapMutation(SchemaContainer $schemaContainer, array $mapping) |
|
84 | |||
85 | /** |
||
86 | * @param SchemaContainer $schemaContainer |
||
87 | * @param array $mapping |
||
88 | */ |
||
89 | private function mapTypes(SchemaContainer $schemaContainer, array $mapping) |
||
96 | |||
97 | /** |
||
98 | * @param SchemaContainer $schemaContainer |
||
99 | * @param array $mapping |
||
100 | */ |
||
101 | private function mapInterfaces(SchemaContainer $schemaContainer, array $mapping) |
||
108 | |||
109 | /** |
||
110 | * @param string $name |
||
111 | * @param array $mapping |
||
112 | * @return Type |
||
113 | */ |
||
114 | private function createType($name, array $mapping) |
||
134 | |||
135 | /** |
||
136 | * @param AbstractType $type |
||
137 | * @param array $mapping |
||
138 | */ |
||
139 | private function populateType(AbstractType $type, array $mapping) |
||
145 | |||
146 | /** |
||
147 | * @param FieldContainer $type |
||
148 | * @param array $mapping |
||
149 | */ |
||
150 | private function populateFieldContainer(FieldContainer $type, array $mapping) |
||
164 | |||
165 | /** |
||
166 | * @param string $name |
||
167 | * @param array $mapping |
||
168 | * @return InterfaceType |
||
169 | */ |
||
170 | private function createInterface($name, array $mapping) |
||
178 | |||
179 | /** |
||
180 | * @param string $name |
||
181 | * @param array $mapping |
||
182 | * @return Field |
||
183 | */ |
||
184 | private function createField($name, array $mapping) |
||
205 | } |
||
206 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.