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 executeOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
256 | |||
257 | /** |
||
258 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
259 | * @param \GraphQL\Server\ServerConfig $config |
||
260 | * @param \GraphQL\Server\OperationParams $params |
||
261 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
262 | * |
||
263 | * @return \GraphQL\Executor\Promise\Promise|mixed |
||
264 | */ |
||
265 | protected function executeCacheableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
300 | |||
301 | /** |
||
302 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
303 | * @param \GraphQL\Server\ServerConfig $config |
||
304 | * @param \GraphQL\Server\OperationParams $params |
||
305 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
306 | * |
||
307 | * @return \GraphQL\Executor\Promise\Promise |
||
308 | */ |
||
309 | protected function executeUncachableOperation(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 doExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
348 | |||
349 | /** |
||
350 | * @param \GraphQL\Server\OperationParams $params |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | protected function validateOperationParams(OperationParams $params) { |
||
360 | |||
361 | /** |
||
362 | * @param \GraphQL\Server\ServerConfig $config |
||
363 | * @param \GraphQL\Server\OperationParams $params |
||
364 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
365 | * |
||
366 | * @return \GraphQL\Error\Error[] |
||
367 | */ |
||
368 | protected function validateOperation(ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
389 | |||
390 | /** |
||
391 | * @param \GraphQL\Server\ServerConfig $config |
||
392 | * @param \GraphQL\Server\OperationParams $params |
||
393 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
394 | * @param $operation |
||
395 | * |
||
396 | * @return mixed |
||
397 | */ |
||
398 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
406 | |||
407 | /** |
||
408 | * @param \GraphQL\Server\ServerConfig $config |
||
409 | * @param \GraphQL\Server\OperationParams $params |
||
410 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
411 | * @param $operation |
||
412 | * |
||
413 | * @return mixed |
||
414 | */ |
||
415 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
423 | |||
424 | /** |
||
425 | * @param \GraphQL\Server\ServerConfig $config |
||
426 | * @param \GraphQL\Server\OperationParams $params |
||
427 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
428 | * @param $operation |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
444 | |||
445 | /** |
||
446 | * @param \GraphQL\Server\ServerConfig $config |
||
447 | * @param \GraphQL\Server\OperationParams $params |
||
448 | * |
||
449 | * @return mixed |
||
450 | * @throws \GraphQL\Server\RequestError |
||
451 | */ |
||
452 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
464 | |||
465 | /** |
||
466 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
467 | * |
||
468 | * @return array |
||
469 | */ |
||
470 | protected function serializeDocument(DocumentNode $document) { |
||
473 | |||
474 | /** |
||
475 | * @param array $item |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | protected function sanitizeRecursive(array $item) { |
||
490 | |||
491 | /** |
||
492 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
493 | * @param \Drupal\Core\Cache\CacheableMetadata $metadata |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | protected function cacheIdentifier(DocumentNode $document, CacheableMetadata $metadata) { |
||
504 | } |
||
505 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.