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) { |
||
315 | |||
316 | /** |
||
317 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
318 | * @param \GraphQL\Server\ServerConfig $config |
||
319 | * @param \GraphQL\Server\OperationParams $params |
||
320 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
321 | * |
||
322 | * @return \GraphQL\Executor\Promise\Promise |
||
323 | */ |
||
324 | protected function executeUncachableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
327 | |||
328 | /** |
||
329 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
330 | * @param \GraphQL\Server\ServerConfig $config |
||
331 | * @param \GraphQL\Server\OperationParams $params |
||
332 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
333 | * |
||
334 | * @return \GraphQL\Executor\Promise\Promise |
||
335 | */ |
||
336 | protected function doExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
363 | |||
364 | /** |
||
365 | * @param \GraphQL\Server\OperationParams $params |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | protected function validateOperationParams(OperationParams $params) { |
||
375 | |||
376 | /** |
||
377 | * @param \GraphQL\Server\ServerConfig $config |
||
378 | * @param \GraphQL\Server\OperationParams $params |
||
379 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
380 | * |
||
381 | * @return \GraphQL\Error\Error[] |
||
382 | */ |
||
383 | protected function validateOperation(ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
404 | |||
405 | /** |
||
406 | * @param \GraphQL\Server\ServerConfig $config |
||
407 | * @param \GraphQL\Server\OperationParams $params |
||
408 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
409 | * @param $operation |
||
410 | * |
||
411 | * @return mixed |
||
412 | */ |
||
413 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
421 | |||
422 | /** |
||
423 | * @param \GraphQL\Server\ServerConfig $config |
||
424 | * @param \GraphQL\Server\OperationParams $params |
||
425 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
426 | * @param $operation |
||
427 | * |
||
428 | * @return mixed |
||
429 | */ |
||
430 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
438 | |||
439 | /** |
||
440 | * @param \GraphQL\Server\ServerConfig $config |
||
441 | * @param \GraphQL\Server\OperationParams $params |
||
442 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
443 | * @param $operation |
||
444 | * |
||
445 | * @return array |
||
446 | */ |
||
447 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
459 | |||
460 | /** |
||
461 | * @param \GraphQL\Server\ServerConfig $config |
||
462 | * @param \GraphQL\Server\OperationParams $params |
||
463 | * |
||
464 | * @return mixed |
||
465 | * @throws \GraphQL\Server\RequestError |
||
466 | */ |
||
467 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
479 | |||
480 | /** |
||
481 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | protected function serializeDocument(DocumentNode $document) { |
||
488 | |||
489 | /** |
||
490 | * @param array $item |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | protected function sanitizeRecursive(array $item) { |
||
505 | |||
506 | /** |
||
507 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
508 | * @param \Drupal\Core\Cache\CacheableMetadata $metadata |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | protected function cacheIdentifier(DocumentNode $document, CacheableMetadata $metadata) { |
||
519 | } |
||
520 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.