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() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | public function hasFields($type) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function hasMutations() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function hasSubscriptions() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritdoc} |
||
| 313 | */ |
||
| 314 | public function hasType($name) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | public function getFields($type) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function getMutations() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritdoc} |
||
| 343 | */ |
||
| 344 | public function getSubscriptions() { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function getTypes() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritdoc} |
||
| 359 | */ |
||
| 360 | public function getSubTypes($name) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritdoc} |
||
| 367 | */ |
||
| 368 | public function resolveType($name, $value, ResolveContext $context, ResolveInfo $info) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * {@inheritdoc} |
||
| 389 | */ |
||
| 390 | public function getType($name) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | public function processMutations(array $mutations) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritdoc} |
||
| 415 | */ |
||
| 416 | public function processSubscriptions(array $subscriptions) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | */ |
||
| 423 | public function processFields(array $fields) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * {@inheritdoc} |
||
| 429 | */ |
||
| 430 | public function processArguments(array $args) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * {@inheritdoc} |
||
| 440 | */ |
||
| 441 | public function processType(array $type) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Retrieves the type instance for the given reference. |
||
| 451 | * |
||
| 452 | * @param array $type |
||
| 453 | * The type reference. |
||
| 454 | * |
||
| 455 | * @return \GraphQL\Type\Definition\Type |
||
| 456 | * The type instance. |
||
| 457 | */ |
||
| 458 | protected function buildType($type) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Retrieves the field definition for a given field reference. |
||
| 470 | * |
||
| 471 | * @param array $field |
||
| 472 | * The type reference. |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | * The field definition. |
||
| 476 | */ |
||
| 477 | View Code Duplication | protected function buildField($field) { |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Retrieves the mutation definition for a given field reference. |
||
| 488 | * |
||
| 489 | * @param array $mutation |
||
| 490 | * The mutation reference. |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | * The mutation definition. |
||
| 494 | */ |
||
| 495 | View Code Duplication | protected function buildMutation($mutation) { |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Retrieves the subscription definition for a given field reference. |
||
| 506 | * |
||
| 507 | * @param array $mutation |
||
| 508 | * The subscription reference. |
||
| 509 | * |
||
| 510 | * @return array |
||
| 511 | * The subscription definition. |
||
| 512 | */ |
||
| 513 | View Code Duplication | protected function buildSubscription($subscription) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * {@inheritdoc} |
||
| 524 | */ |
||
| 525 | public function getCacheContexts() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritdoc} |
||
| 531 | */ |
||
| 532 | public function getCacheTags() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * {@inheritdoc} |
||
| 538 | */ |
||
| 539 | public function getCacheMaxAge() { |
||
| 542 | } |
||
| 543 |
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.