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 QueryProcessor 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 QueryProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class QueryProcessor { |
||
33 | |||
34 | /** |
||
35 | * The current user account. |
||
36 | * |
||
37 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
38 | */ |
||
39 | protected $currentUser; |
||
40 | |||
41 | /** |
||
42 | * The schema plugin manager. |
||
43 | * |
||
44 | * @var \Drupal\graphql\Plugin\SchemaPluginManager |
||
45 | */ |
||
46 | protected $pluginManager; |
||
47 | |||
48 | /** |
||
49 | * The query provider service. |
||
50 | * |
||
51 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
52 | */ |
||
53 | protected $queryProvider; |
||
54 | |||
55 | /** |
||
56 | * The cache backend for caching query results. |
||
57 | * |
||
58 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
59 | */ |
||
60 | protected $cacheBackend; |
||
61 | |||
62 | /** |
||
63 | * The cache contexts manager service. |
||
64 | * |
||
65 | * @var \Drupal\Core\Cache\Context\CacheContextsManager |
||
66 | */ |
||
67 | protected $contextsManager; |
||
68 | |||
69 | /** |
||
70 | * Processor constructor. |
||
71 | * |
||
72 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
73 | * The current user. |
||
74 | * @param \Drupal\Core\Cache\Context\CacheContextsManager $contextsManager |
||
75 | * The cache contexts manager service. |
||
76 | * @param \Drupal\graphql\Plugin\SchemaPluginManager $pluginManager |
||
77 | * The schema plugin manager. |
||
78 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
79 | * The query provider service. |
||
80 | * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
||
81 | * The cache backend for caching query results. |
||
82 | */ |
||
83 | public function __construct( |
||
96 | |||
97 | /** |
||
98 | * Processes one or multiple graphql operations. |
||
99 | * |
||
100 | * @param string $schema |
||
101 | * The plugin id of the schema to use. |
||
102 | * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $params |
||
103 | * The graphql operation(s) to execute. |
||
104 | * @param array $globals |
||
105 | * The query context. |
||
106 | * |
||
107 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult|\Drupal\graphql\GraphQL\Execution\QueryResult[] |
||
108 | * The query result. |
||
109 | * |
||
110 | */ |
||
111 | public function processQuery($schema, $params, array $globals = []) { |
||
158 | |||
159 | /** |
||
160 | * @param \GraphQL\Server\ServerConfig $config |
||
161 | * @param \GraphQL\Server\OperationParams $params |
||
162 | * |
||
163 | * @return mixed |
||
164 | */ |
||
165 | public function executeSingle(ServerConfig $config, OperationParams $params) { |
||
170 | |||
171 | /** |
||
172 | * @param \GraphQL\Server\ServerConfig $config |
||
173 | * @param array $params |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public function executeBatch(ServerConfig $config, array $params) { |
||
186 | |||
187 | /** |
||
188 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
189 | * @param \GraphQL\Server\ServerConfig $config |
||
190 | * @param \GraphQL\Server\OperationParams $params |
||
191 | * @param bool $batching |
||
192 | * |
||
193 | * @return \GraphQL\Executor\Promise\Promise |
||
194 | */ |
||
195 | protected function executeOperationWithReporting(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
211 | |||
212 | /** |
||
213 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
214 | * @param \GraphQL\Server\ServerConfig $config |
||
215 | * @param \GraphQL\Server\OperationParams $params |
||
216 | * @param bool $batching |
||
217 | * |
||
218 | * @return \GraphQL\Executor\Promise\Promise |
||
219 | */ |
||
220 | protected function executeOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
268 | |||
269 | /** |
||
270 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
271 | * @param \GraphQL\Server\ServerConfig $config |
||
272 | * @param \GraphQL\Server\OperationParams $params |
||
273 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
274 | * |
||
275 | * @return \GraphQL\Executor\Promise\Promise|mixed |
||
276 | */ |
||
277 | protected function executeCacheableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
312 | |||
313 | /** |
||
314 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
315 | * @param \GraphQL\Server\ServerConfig $config |
||
316 | * @param \GraphQL\Server\OperationParams $params |
||
317 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
318 | * |
||
319 | * @return \GraphQL\Executor\Promise\Promise |
||
320 | */ |
||
321 | protected function executeUncachableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
329 | |||
330 | /** |
||
331 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
332 | * @param \GraphQL\Server\ServerConfig $config |
||
333 | * @param \GraphQL\Server\OperationParams $params |
||
334 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
335 | * |
||
336 | * @return \GraphQL\Executor\Promise\Promise |
||
337 | */ |
||
338 | protected function doExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
369 | |||
370 | /** |
||
371 | * @param \GraphQL\Server\OperationParams $params |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | protected function validateOperationParams(OperationParams $params) { |
||
381 | |||
382 | /** |
||
383 | * @param \GraphQL\Server\ServerConfig $config |
||
384 | * @param \GraphQL\Server\OperationParams $params |
||
385 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
386 | * |
||
387 | * @return \GraphQL\Error\Error[] |
||
388 | */ |
||
389 | protected function validateOperation(ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
410 | |||
411 | /** |
||
412 | * @param \GraphQL\Server\ServerConfig $config |
||
413 | * @param \GraphQL\Server\OperationParams $params |
||
414 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
415 | * @param $operation |
||
416 | * |
||
417 | * @return mixed |
||
418 | */ |
||
419 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
427 | |||
428 | /** |
||
429 | * @param \GraphQL\Server\ServerConfig $config |
||
430 | * @param \GraphQL\Server\OperationParams $params |
||
431 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
432 | * @param $operation |
||
433 | * |
||
434 | * @return mixed |
||
435 | */ |
||
436 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
444 | |||
445 | /** |
||
446 | * @param \GraphQL\Server\ServerConfig $config |
||
447 | * @param \GraphQL\Server\OperationParams $params |
||
448 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
449 | * @param $operation |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
465 | |||
466 | /** |
||
467 | * @param \GraphQL\Server\ServerConfig $config |
||
468 | * @param \GraphQL\Server\OperationParams $params |
||
469 | * |
||
470 | * @return mixed |
||
471 | * @throws \GraphQL\Server\RequestError |
||
472 | */ |
||
473 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
485 | |||
486 | /** |
||
487 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
488 | * |
||
489 | * @return array |
||
490 | */ |
||
491 | protected function serializeDocument(DocumentNode $document) { |
||
494 | |||
495 | /** |
||
496 | * @param array $item |
||
497 | * |
||
498 | * @return array |
||
499 | */ |
||
500 | protected function sanitizeRecursive(array $item) { |
||
511 | |||
512 | /** |
||
513 | * @param \GraphQL\Server\OperationParams $params |
||
514 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
515 | * @param \Drupal\Core\Cache\CacheableMetadata $metadata |
||
516 | * |
||
517 | * @return string |
||
518 | */ |
||
519 | protected function cacheIdentifier(OperationParams $params, DocumentNode $document, CacheableMetadata $metadata) { |
||
535 | } |
||
536 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.