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 |
||
| 27 | abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, SchemaBuilderInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The field plugin manager. |
||
| 31 | * |
||
| 32 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
| 33 | */ |
||
| 34 | protected $fieldManager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The mutation plugin manager. |
||
| 38 | * |
||
| 39 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
| 40 | */ |
||
| 41 | protected $mutationManager; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The subscription plugin manager. |
||
| 45 | * |
||
| 46 | * @var \Drupal\graphql\Plugin\SubscriptionPluginManager |
||
| 47 | */ |
||
| 48 | protected $subscriptionManager; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The type manager aggregator service. |
||
| 52 | * |
||
| 53 | * @var \Drupal\graphql\Plugin\TypePluginManagerAggregator |
||
| 54 | */ |
||
| 55 | protected $typeManagers; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Static cache of field definitions. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $fields = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Static cache of mutation definitions. |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $mutations = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Static cache of subscription definitions. |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $subscriptions = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Static cache of type instances. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $types = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The service parameters |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $parameters; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The query provider service. |
||
| 94 | * |
||
| 95 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
| 96 | */ |
||
| 97 | protected $queryProvider; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The current user. |
||
| 101 | * |
||
| 102 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
| 103 | */ |
||
| 104 | protected $currentUser; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * SchemaPluginBase constructor. |
||
| 126 | * |
||
| 127 | * @param array $configuration |
||
| 128 | * The plugin configuration array. |
||
| 129 | * @param string $pluginId |
||
| 130 | * The plugin id. |
||
| 131 | * @param array $pluginDefinition |
||
| 132 | * The plugin definition array. |
||
| 133 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
| 134 | * The field plugin manager. |
||
| 135 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
| 136 | * The mutation plugin manager. |
||
| 137 | * @param \Drupal\graphql\Plugin\SubscriptionPluginManager $subscriptionManager |
||
| 138 | * The subscription plugin manager. |
||
| 139 | * @param \Drupal\graphql\Plugin\TypePluginManagerAggregator $typeManagers |
||
| 140 | * The type manager aggregator service. |
||
| 141 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
| 142 | * The query provider service. |
||
| 143 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
| 144 | * The current user. |
||
| 145 | * @param array $parameters |
||
| 146 | */ |
||
| 147 | public function __construct( |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function getSchema() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | public function getServer() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function hasFields($type) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function hasMutations() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function hasSubscriptions() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function hasType($name) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function getFields($type) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function getMutations() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function getSubscriptions() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function getTypes() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function getSubTypes($name) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | */ |
||
| 336 | public function resolveType($name, $value, ResolveContext $context, ResolveInfo $info) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | public function getType($name) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * {@inheritdoc} |
||
| 376 | */ |
||
| 377 | public function processMutations($mutations) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function processSubscriptions($subscriptions) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * {@inheritdoc} |
||
| 390 | */ |
||
| 391 | public function processFields($fields) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | public function processArguments($args) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | public function processType($type) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Retrieves the type instance for the given reference. |
||
| 419 | * |
||
| 420 | * @param array $type |
||
| 421 | * The type reference. |
||
| 422 | * |
||
| 423 | * @return \GraphQL\Type\Definition\Type |
||
| 424 | * The type instance. |
||
| 425 | */ |
||
| 426 | protected function buildType($type) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Retrieves the field definition for a given field reference. |
||
| 438 | * |
||
| 439 | * @param array $field |
||
| 440 | * The type reference. |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | * The field definition. |
||
| 444 | */ |
||
| 445 | View Code Duplication | protected function buildField($field) { |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Retrieves the mutation definition for a given field reference. |
||
| 456 | * |
||
| 457 | * @param array $mutation |
||
| 458 | * The mutation reference. |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | * The mutation definition. |
||
| 462 | */ |
||
| 463 | View Code Duplication | protected function buildMutation($mutation) { |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Retrieves the subscription definition for a given field reference. |
||
| 474 | * |
||
| 475 | * @param array $mutation |
||
| 476 | * The subscription reference. |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | * The subscription definition. |
||
| 480 | */ |
||
| 481 | View Code Duplication | protected function buildSubscription($subscription) { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * {@inheritdoc} |
||
| 492 | */ |
||
| 493 | public function getCacheContexts() { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | public function getCacheTags() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | public function getCacheMaxAge() { |
||
| 510 | } |
||
| 511 |
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.