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 |
||
17 | abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, SchemaBuilderInterface, ContainerFactoryPluginInterface { |
||
18 | |||
19 | /** |
||
20 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
21 | */ |
||
22 | protected $fieldManager; |
||
23 | |||
24 | /** |
||
25 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
26 | */ |
||
27 | protected $mutationManager; |
||
28 | |||
29 | /** |
||
30 | * @var \Drupal\graphql\Plugin\TypePluginManagerAggregator |
||
31 | */ |
||
32 | protected $typeManagers; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $fields = []; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $mutations = []; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $types = []; |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
62 | |||
63 | /** |
||
64 | * SchemaPluginBase constructor. |
||
65 | * |
||
66 | * @param array $configuration |
||
67 | * The plugin configuration array. |
||
68 | * @param string $pluginId |
||
69 | * The plugin id. |
||
70 | * @param array $pluginDefinition |
||
71 | * The plugin definition array. |
||
72 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
73 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
74 | * @param \Drupal\graphql\Plugin\TypePluginManagerAggregator $typeManagers |
||
75 | */ |
||
76 | View Code Duplication | public function __construct( |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function getSchema() { |
||
122 | |||
123 | /** |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function hasFields($type) { |
||
129 | |||
130 | /** |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function hasMutations() { |
||
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function hasType($name) { |
||
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getFields($type) { |
||
159 | |||
160 | /** |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getMutations() { |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function getTypes() { |
||
175 | |||
176 | /** |
||
177 | * Retrieve the list of derivatives associated with a composite type. |
||
178 | * |
||
179 | * @return string[] |
||
180 | * The list of possible sub typenames. |
||
181 | */ |
||
182 | public function getSubTypes($name) { |
||
186 | |||
187 | /** |
||
188 | * Resolve the matching type. |
||
189 | */ |
||
190 | public function resolveType($name, $value, $context, $info) { |
||
208 | |||
209 | /** |
||
210 | * @param $name |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | public function getType($name) { |
||
229 | |||
230 | /** |
||
231 | * @param $mutations |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | public function processMutations($mutations) { |
||
238 | |||
239 | /** |
||
240 | * @param $fields |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function processFields($fields) { |
||
247 | |||
248 | /** |
||
249 | * @param $args |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | public function processArguments($args) { |
||
260 | |||
261 | /** |
||
262 | * @param $type |
||
263 | * |
||
264 | * @return mixed |
||
265 | */ |
||
266 | public function processType($type) { |
||
273 | |||
274 | /** |
||
275 | * @param $type |
||
276 | * |
||
277 | * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase |
||
278 | */ |
||
279 | protected function buildType($type) { |
||
288 | |||
289 | /** |
||
290 | * @param $field |
||
291 | * |
||
292 | * @return mixed |
||
293 | */ |
||
294 | View Code Duplication | protected function buildField($field) { |
|
302 | |||
303 | /** |
||
304 | * @param $mutation |
||
305 | * |
||
306 | * @return mixed |
||
307 | */ |
||
308 | View Code Duplication | protected function buildMutation($mutation) { |
|
316 | } |
||
317 |
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.