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 |
||
29 | abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, SchemaBuilderInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface { |
||
30 | |||
31 | /** |
||
32 | * The field plugin manager. |
||
33 | * |
||
34 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
35 | */ |
||
36 | protected $fieldManager; |
||
37 | |||
38 | /** |
||
39 | * The mutation plugin manager. |
||
40 | * |
||
41 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
42 | */ |
||
43 | protected $mutationManager; |
||
44 | |||
45 | /** |
||
46 | * The subscription plugin manager. |
||
47 | * |
||
48 | * @var \Drupal\graphql\Plugin\SubscriptionPluginManager |
||
49 | */ |
||
50 | protected $subscriptionManager; |
||
51 | |||
52 | /** |
||
53 | * The type manager aggregator service. |
||
54 | * |
||
55 | * @var \Drupal\graphql\Plugin\TypePluginManagerAggregator |
||
56 | */ |
||
57 | protected $typeManagers; |
||
58 | |||
59 | /** |
||
60 | * Static cache of field definitions. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $fields = []; |
||
65 | |||
66 | /** |
||
67 | * Static cache of mutation definitions. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $mutations = []; |
||
72 | |||
73 | /** |
||
74 | * Static cache of subscription definitions. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $subscriptions = []; |
||
79 | |||
80 | /** |
||
81 | * Static cache of type instances. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $types = []; |
||
86 | |||
87 | /** |
||
88 | * The service parameters |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $parameters; |
||
93 | |||
94 | /** |
||
95 | * The query provider service. |
||
96 | * |
||
97 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
98 | */ |
||
99 | protected $queryProvider; |
||
100 | |||
101 | /** |
||
102 | * The current user. |
||
103 | * |
||
104 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
105 | */ |
||
106 | protected $currentUser; |
||
107 | |||
108 | /** |
||
109 | * The logger service. |
||
110 | * |
||
111 | * @var \Psr\Log\LoggerInterface |
||
112 | */ |
||
113 | protected $logger; |
||
114 | |||
115 | /** |
||
116 | * @var \Drupal\Core\Language\LanguageManagerInterface |
||
117 | */ |
||
118 | protected $languageManager; |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
139 | |||
140 | /** |
||
141 | * SchemaPluginBase constructor. |
||
142 | * |
||
143 | * @param array $configuration |
||
144 | * The plugin configuration array. |
||
145 | * @param string $pluginId |
||
146 | * The plugin id. |
||
147 | * @param array $pluginDefinition |
||
148 | * The plugin definition array. |
||
149 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
150 | * The field plugin manager. |
||
151 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
152 | * The mutation plugin manager. |
||
153 | * @param \Drupal\graphql\Plugin\SubscriptionPluginManager $subscriptionManager |
||
154 | * The subscription plugin manager. |
||
155 | * @param \Drupal\graphql\Plugin\TypePluginManagerAggregator $typeManagers |
||
156 | * The type manager aggregator service. |
||
157 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
158 | * The query provider service. |
||
159 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
160 | * The current user. |
||
161 | * @param \Psr\Log\LoggerInterface $logger |
||
162 | * The logger service. |
||
163 | * @param array $parameters |
||
164 | * The service parameters. |
||
165 | */ |
||
166 | public function __construct( |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function getSchema() { |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function validateSchema() { |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function getServer() { |
||
306 | |||
307 | /** |
||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function hasFields($type) { |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function hasMutations() { |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function hasSubscriptions() { |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function hasType($name) { |
||
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | public function getFields($type) { |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function getMutations() { |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function getSubscriptions() { |
||
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | public function getTypes() { |
||
374 | |||
375 | /** |
||
376 | * {@inheritdoc} |
||
377 | */ |
||
378 | public function getSubTypes($name) { |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | */ |
||
386 | public function resolveType($name, $value, ResolveContext $context, ResolveInfo $info) { |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function getType($name) { |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function processMutations(array $mutations) { |
||
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | */ |
||
434 | public function processSubscriptions(array $subscriptions) { |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | */ |
||
441 | public function processFields(array $fields) { |
||
444 | |||
445 | /** |
||
446 | * {@inheritdoc} |
||
447 | */ |
||
448 | public function processArguments(array $args) { |
||
455 | |||
456 | /** |
||
457 | * {@inheritdoc} |
||
458 | */ |
||
459 | public function processType(array $type) { |
||
466 | |||
467 | /** |
||
468 | * Retrieves the type instance for the given reference. |
||
469 | * |
||
470 | * @param array $type |
||
471 | * The type reference. |
||
472 | * |
||
473 | * @return \GraphQL\Type\Definition\Type |
||
474 | * The type instance. |
||
475 | */ |
||
476 | protected function buildType($type) { |
||
485 | |||
486 | /** |
||
487 | * Retrieves the field definition for a given field reference. |
||
488 | * |
||
489 | * @param array $field |
||
490 | * The type reference. |
||
491 | * |
||
492 | * @return array |
||
493 | * The field definition. |
||
494 | */ |
||
495 | View Code Duplication | protected function buildField($field) { |
|
503 | |||
504 | /** |
||
505 | * Retrieves the mutation definition for a given field reference. |
||
506 | * |
||
507 | * @param array $mutation |
||
508 | * The mutation reference. |
||
509 | * |
||
510 | * @return array |
||
511 | * The mutation definition. |
||
512 | */ |
||
513 | View Code Duplication | protected function buildMutation($mutation) { |
|
521 | |||
522 | /** |
||
523 | * Retrieves the subscription definition for a given field reference. |
||
524 | * |
||
525 | * @param array $mutation |
||
526 | * The subscription reference. |
||
527 | * |
||
528 | * @return array |
||
529 | * The subscription definition. |
||
530 | */ |
||
531 | View Code Duplication | protected function buildSubscription($subscription) { |
|
539 | |||
540 | /** |
||
541 | * {@inheritdoc} |
||
542 | */ |
||
543 | public function getCacheContexts() { |
||
546 | |||
547 | /** |
||
548 | * {@inheritdoc} |
||
549 | */ |
||
550 | public function getCacheTags() { |
||
553 | |||
554 | /** |
||
555 | * {@inheritdoc} |
||
556 | */ |
||
557 | public function getCacheMaxAge() { |
||
560 | } |
||
561 |
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.