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:
Complex classes like SchemaPluginBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaPluginBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, SchemaBuilderInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface { |
||
27 | |||
28 | /** |
||
29 | * The field plugin manager. |
||
30 | * |
||
31 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
32 | */ |
||
33 | protected $fieldManager; |
||
34 | |||
35 | /** |
||
36 | * The mutation plugin manager. |
||
37 | * |
||
38 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
39 | */ |
||
40 | protected $mutationManager; |
||
41 | |||
42 | /** |
||
43 | * The subscription plugin manager. |
||
44 | * |
||
45 | * @var \Drupal\graphql\Plugin\SubscriptionPluginManager |
||
46 | */ |
||
47 | protected $subscriptionManager; |
||
48 | |||
49 | /** |
||
50 | * The type manager aggregator service. |
||
51 | * |
||
52 | * @var \Drupal\graphql\Plugin\TypePluginManagerAggregator |
||
53 | */ |
||
54 | protected $typeManagers; |
||
55 | |||
56 | /** |
||
57 | * Static cache of field definitions. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $fields = []; |
||
62 | |||
63 | /** |
||
64 | * Static cache of mutation definitions. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $mutations = []; |
||
69 | |||
70 | /** |
||
71 | * Static cache of subscription definitions. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $subscriptions = []; |
||
76 | |||
77 | /** |
||
78 | * Static cache of type instances. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $types = []; |
||
83 | |||
84 | /** |
||
85 | * The service parameters |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $parameters; |
||
90 | |||
91 | /** |
||
92 | * The query provider service. |
||
93 | * |
||
94 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
95 | */ |
||
96 | protected $queryProvider; |
||
97 | |||
98 | /** |
||
99 | * The current user. |
||
100 | * |
||
101 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
102 | */ |
||
103 | protected $currentUser; |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
122 | |||
123 | /** |
||
124 | * SchemaPluginBase constructor. |
||
125 | * |
||
126 | * @param array $configuration |
||
127 | * The plugin configuration array. |
||
128 | * @param string $pluginId |
||
129 | * The plugin id. |
||
130 | * @param array $pluginDefinition |
||
131 | * The plugin definition array. |
||
132 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
133 | * The field plugin manager. |
||
134 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
135 | * The mutation plugin manager. |
||
136 | * @param \Drupal\graphql\Plugin\SubscriptionPluginManager $subscriptionManager |
||
137 | * The subscription plugin manager. |
||
138 | * @param \Drupal\graphql\Plugin\TypePluginManagerAggregator $typeManagers |
||
139 | * The type manager aggregator service. |
||
140 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
141 | * The query provider service. |
||
142 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
143 | * The current user. |
||
144 | * @param array $parameters |
||
145 | */ |
||
146 | public function __construct( |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function allowsQueryBatching() { |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function inDebug() { |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function getSchema() { |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function getRootValue() { |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getContext() { |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function getFieldResolver() { |
||
262 | |||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function getValidationRules() { |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function getPersistedQueryLoader() { |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | public function hasFields($type) { |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | public function hasMutations() { |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function hasSubscriptions() { |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function hasType($name) { |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function getFields($type) { |
||
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | public function getMutations() { |
||
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | public function getSubscriptions() { |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | public function getTypes() { |
||
353 | |||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | */ |
||
357 | public function getSubTypes($name) { |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function resolveType($name, $value, ResolveContext $context, ResolveInfo $info) { |
||
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | public function getType($name) { |
||
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | */ |
||
406 | public function processMutations($mutations) { |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function processSubscriptions($subscriptions) { |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | public function processFields($fields) { |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function processArguments($args) { |
||
434 | |||
435 | /** |
||
436 | * {@inheritdoc} |
||
437 | */ |
||
438 | public function processType($type) { |
||
445 | |||
446 | /** |
||
447 | * Retrieves the type instance for the given reference. |
||
448 | * |
||
449 | * @param array $type |
||
450 | * The type reference. |
||
451 | * |
||
452 | * @return \GraphQL\Type\Definition\Type |
||
453 | * The type instance. |
||
454 | */ |
||
455 | protected function buildType($type) { |
||
464 | |||
465 | /** |
||
466 | * Retrieves the field definition for a given field reference. |
||
467 | * |
||
468 | * @param array $field |
||
469 | * The type reference. |
||
470 | * |
||
471 | * @return array |
||
472 | * The field definition. |
||
473 | */ |
||
474 | View Code Duplication | protected function buildField($field) { |
|
482 | |||
483 | /** |
||
484 | * Retrieves the mutation definition for a given field reference. |
||
485 | * |
||
486 | * @param array $mutation |
||
487 | * The mutation reference. |
||
488 | * |
||
489 | * @return array |
||
490 | * The mutation definition. |
||
491 | */ |
||
492 | View Code Duplication | protected function buildMutation($mutation) { |
|
500 | |||
501 | /** |
||
502 | * Retrieves the subscription definition for a given field reference. |
||
503 | * |
||
504 | * @param array $mutation |
||
505 | * The subscription reference. |
||
506 | * |
||
507 | * @return array |
||
508 | * The subscription definition. |
||
509 | */ |
||
510 | View Code Duplication | protected function buildSubscription($subscription) { |
|
518 | |||
519 | /** |
||
520 | * {@inheritdoc} |
||
521 | */ |
||
522 | public function getCacheContexts() { |
||
525 | |||
526 | /** |
||
527 | * {@inheritdoc} |
||
528 | */ |
||
529 | public function getCacheTags() { |
||
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | public function getCacheMaxAge() { |
||
539 | } |
||
540 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.