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) { |
||
313 | |||
314 | /** |
||
315 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
316 | * @param \GraphQL\Server\ServerConfig $config |
||
317 | * @param \GraphQL\Server\OperationParams $params |
||
318 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
319 | * |
||
320 | * @return \GraphQL\Executor\Promise\Promise |
||
321 | */ |
||
322 | protected function executeUncachableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
330 | |||
331 | /** |
||
332 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
333 | * @param \GraphQL\Server\ServerConfig $config |
||
334 | * @param \GraphQL\Server\OperationParams $params |
||
335 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
336 | * |
||
337 | * @return \GraphQL\Executor\Promise\Promise |
||
338 | */ |
||
339 | protected function doExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
372 | |||
373 | /** |
||
374 | * @param \GraphQL\Server\OperationParams $params |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | protected function validateOperationParams(OperationParams $params) { |
||
384 | |||
385 | /** |
||
386 | * @param \GraphQL\Server\ServerConfig $config |
||
387 | * @param \GraphQL\Server\OperationParams $params |
||
388 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
389 | * |
||
390 | * @return \GraphQL\Error\Error[] |
||
391 | */ |
||
392 | protected function validateOperation(ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
413 | |||
414 | /** |
||
415 | * @param \GraphQL\Server\ServerConfig $config |
||
416 | * @param \GraphQL\Server\OperationParams $params |
||
417 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
418 | * @param $operation |
||
419 | * |
||
420 | * @return mixed |
||
421 | */ |
||
422 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
430 | |||
431 | /** |
||
432 | * @param \GraphQL\Server\ServerConfig $config |
||
433 | * @param \GraphQL\Server\OperationParams $params |
||
434 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
435 | * @param $operation |
||
436 | * |
||
437 | * @return mixed |
||
438 | */ |
||
439 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
447 | |||
448 | /** |
||
449 | * @param \GraphQL\Server\ServerConfig $config |
||
450 | * @param \GraphQL\Server\OperationParams $params |
||
451 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
452 | * @param $operation |
||
453 | * |
||
454 | * @return array |
||
455 | */ |
||
456 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
468 | |||
469 | /** |
||
470 | * @param \GraphQL\Server\ServerConfig $config |
||
471 | * @param \GraphQL\Server\OperationParams $params |
||
472 | * |
||
473 | * @return mixed |
||
474 | * @throws \GraphQL\Server\RequestError |
||
475 | */ |
||
476 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
488 | |||
489 | /** |
||
490 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | protected function serializeDocument(DocumentNode $document) { |
||
497 | |||
498 | /** |
||
499 | * @param array $item |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | protected function sanitizeRecursive(array $item) { |
||
514 | |||
515 | /** |
||
516 | * @param \GraphQL\Server\OperationParams $params |
||
517 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
518 | * @param \Drupal\Core\Cache\CacheableMetadata $metadata |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | protected function cacheIdentifier(OperationParams $params, DocumentNode $document, CacheableMetadata $metadata) { |
||
538 | } |
||
539 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.