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 validateSchema() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | public function getServer() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function hasFields($type) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function hasMutations() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function hasSubscriptions() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function hasType($name) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function getFields($type) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function getMutations() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function getSubscriptions() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | */ |
||
| 326 | public function getTypes() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | public function getSubTypes($name) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | public function resolveType($name, $value, ResolveContext $context, ResolveInfo $info) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * {@inheritdoc} |
||
| 364 | */ |
||
| 365 | public function getType($name) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function processMutations($mutations) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * {@inheritdoc} |
||
| 390 | */ |
||
| 391 | public function processSubscriptions($subscriptions) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | public function processFields($fields) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritdoc} |
||
| 404 | */ |
||
| 405 | public function processArguments($args) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritdoc} |
||
| 415 | */ |
||
| 416 | public function processType($type) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Retrieves the type instance for the given reference. |
||
| 426 | * |
||
| 427 | * @param array $type |
||
| 428 | * The type reference. |
||
| 429 | * |
||
| 430 | * @return \GraphQL\Type\Definition\Type |
||
| 431 | * The type instance. |
||
| 432 | */ |
||
| 433 | protected function buildType($type) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Retrieves the field definition for a given field reference. |
||
| 445 | * |
||
| 446 | * @param array $field |
||
| 447 | * The type reference. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | * The field definition. |
||
| 451 | */ |
||
| 452 | View Code Duplication | protected function buildField($field) { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Retrieves the mutation definition for a given field reference. |
||
| 463 | * |
||
| 464 | * @param array $mutation |
||
| 465 | * The mutation reference. |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | * The mutation definition. |
||
| 469 | */ |
||
| 470 | View Code Duplication | protected function buildMutation($mutation) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Retrieves the subscription definition for a given field reference. |
||
| 481 | * |
||
| 482 | * @param array $mutation |
||
| 483 | * The subscription reference. |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | * The subscription definition. |
||
| 487 | */ |
||
| 488 | View Code Duplication | protected function buildSubscription($subscription) { |
|
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | public function getCacheContexts() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | public function getCacheTags() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * {@inheritdoc} |
||
| 513 | */ |
||
| 514 | public function getCacheMaxAge() { |
||
| 517 | } |
||
| 518 |
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.