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 |
||
| 28 | abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, SchemaBuilderInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The field plugin manager. |
||
| 32 | * |
||
| 33 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
| 34 | */ |
||
| 35 | protected $fieldManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The mutation plugin manager. |
||
| 39 | * |
||
| 40 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
| 41 | */ |
||
| 42 | protected $mutationManager; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The subscription plugin manager. |
||
| 46 | * |
||
| 47 | * @var \Drupal\graphql\Plugin\SubscriptionPluginManager |
||
| 48 | */ |
||
| 49 | protected $subscriptionManager; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The type manager aggregator service. |
||
| 53 | * |
||
| 54 | * @var \Drupal\graphql\Plugin\TypePluginManagerAggregator |
||
| 55 | */ |
||
| 56 | protected $typeManagers; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Static cache of field definitions. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $fields = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Static cache of mutation definitions. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $mutations = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Static cache of subscription definitions. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $subscriptions = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Static cache of type instances. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $types = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The service parameters |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $parameters; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The query provider service. |
||
| 95 | * |
||
| 96 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
| 97 | */ |
||
| 98 | protected $queryProvider; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The current user. |
||
| 102 | * |
||
| 103 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
| 104 | */ |
||
| 105 | protected $currentUser; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The logger service. |
||
| 109 | * |
||
| 110 | * @var \Psr\Log\LoggerInterface |
||
| 111 | */ |
||
| 112 | protected $logger; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * SchemaPluginBase constructor. |
||
| 135 | * |
||
| 136 | * @param array $configuration |
||
| 137 | * The plugin configuration array. |
||
| 138 | * @param string $pluginId |
||
| 139 | * The plugin id. |
||
| 140 | * @param array $pluginDefinition |
||
| 141 | * The plugin definition array. |
||
| 142 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
| 143 | * The field plugin manager. |
||
| 144 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
| 145 | * The mutation plugin manager. |
||
| 146 | * @param \Drupal\graphql\Plugin\SubscriptionPluginManager $subscriptionManager |
||
| 147 | * The subscription plugin manager. |
||
| 148 | * @param \Drupal\graphql\Plugin\TypePluginManagerAggregator $typeManagers |
||
| 149 | * The type manager aggregator service. |
||
| 150 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
| 151 | * The query provider service. |
||
| 152 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
| 153 | * The current user. |
||
| 154 | * @param \Psr\Log\LoggerInterface $logger |
||
| 155 | * The logger service. |
||
| 156 | * @param array $parameters |
||
| 157 | * The service parameters. |
||
| 158 | */ |
||
| 159 | public function __construct( |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function getSchema() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function validateSchema() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function getServer() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function hasFields($type) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function hasMutations() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | public function hasSubscriptions() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | public function hasType($name) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | public function getFields($type) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | */ |
||
| 336 | public function getMutations() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | public function getSubscriptions() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritdoc} |
||
| 349 | */ |
||
| 350 | public function getTypes() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * {@inheritdoc} |
||
| 358 | */ |
||
| 359 | public function getSubTypes($name) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | public function resolveType($name, $value, ResolveContext $context, ResolveInfo $info) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * {@inheritdoc} |
||
| 388 | */ |
||
| 389 | public function getType($name) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritdoc} |
||
| 407 | */ |
||
| 408 | public function processMutations(array $mutations) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 415 | public function processSubscriptions(array $subscriptions) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | public function processFields(array $fields) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | public function processArguments(array $args) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | public function processType(array $type) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Retrieves the type instance for the given reference. |
||
| 450 | * |
||
| 451 | * @param array $type |
||
| 452 | * The type reference. |
||
| 453 | * |
||
| 454 | * @return \GraphQL\Type\Definition\Type |
||
| 455 | * The type instance. |
||
| 456 | */ |
||
| 457 | protected function buildType($type) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Retrieves the field definition for a given field reference. |
||
| 469 | * |
||
| 470 | * @param array $field |
||
| 471 | * The type reference. |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | * The field definition. |
||
| 475 | */ |
||
| 476 | View Code Duplication | protected function buildField($field) { |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Retrieves the mutation definition for a given field reference. |
||
| 487 | * |
||
| 488 | * @param array $mutation |
||
| 489 | * The mutation reference. |
||
| 490 | * |
||
| 491 | * @return array |
||
| 492 | * The mutation definition. |
||
| 493 | */ |
||
| 494 | View Code Duplication | protected function buildMutation($mutation) { |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Retrieves the subscription definition for a given field reference. |
||
| 505 | * |
||
| 506 | * @param array $mutation |
||
| 507 | * The subscription reference. |
||
| 508 | * |
||
| 509 | * @return array |
||
| 510 | * The subscription definition. |
||
| 511 | */ |
||
| 512 | View Code Duplication | protected function buildSubscription($subscription) { |
|
| 520 | |||
| 521 | /** |
||
| 522 | * {@inheritdoc} |
||
| 523 | */ |
||
| 524 | public function getCacheContexts() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * {@inheritdoc} |
||
| 530 | */ |
||
| 531 | public function getCacheTags() { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * {@inheritdoc} |
||
| 537 | */ |
||
| 538 | public function getCacheMaxAge() { |
||
| 541 | } |
||
| 542 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.