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 |
||
33 | class QueryProcessor { |
||
34 | |||
35 | /** |
||
36 | * The current user account. |
||
37 | * |
||
38 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
39 | */ |
||
40 | protected $currentUser; |
||
41 | |||
42 | /** |
||
43 | * The schema plugin manager. |
||
44 | * |
||
45 | * @var \Drupal\graphql\Plugin\SchemaPluginManager |
||
46 | */ |
||
47 | protected $pluginManager; |
||
48 | |||
49 | /** |
||
50 | * The query provider service. |
||
51 | * |
||
52 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
53 | */ |
||
54 | protected $queryProvider; |
||
55 | |||
56 | /** |
||
57 | * The cache backend for caching query results. |
||
58 | * |
||
59 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
60 | */ |
||
61 | protected $cacheBackend; |
||
62 | |||
63 | /** |
||
64 | * The cache contexts manager service. |
||
65 | * |
||
66 | * @var \Drupal\Core\Cache\Context\CacheContextsManager |
||
67 | */ |
||
68 | protected $contextsManager; |
||
69 | |||
70 | /** |
||
71 | * The configuration service parameter. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $config; |
||
76 | |||
77 | /** |
||
78 | * Processor constructor. |
||
79 | * |
||
80 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
81 | * The current user. |
||
82 | * @param \Drupal\Core\Cache\Context\CacheContextsManager $contextsManager |
||
83 | * The cache contexts manager service. |
||
84 | * @param \Drupal\graphql\Plugin\SchemaPluginManager $pluginManager |
||
85 | * The schema plugin manager. |
||
86 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
87 | * The query provider service. |
||
88 | * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
||
89 | * The cache backend for caching query results. |
||
90 | * @param array $config |
||
91 | * The configuration service parameter. |
||
92 | */ |
||
93 | public function __construct( |
||
108 | |||
109 | /** |
||
110 | * Processes one or multiple graphql operations. |
||
111 | * |
||
112 | * @param string $schema |
||
113 | * The plugin id of the schema to use. |
||
114 | * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $params |
||
115 | * The graphql operation(s) to execute. |
||
116 | * @param array $globals |
||
117 | * The query context. |
||
118 | * |
||
119 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult|\Drupal\graphql\GraphQL\Execution\QueryResult[] |
||
120 | * The query result. |
||
121 | * |
||
122 | */ |
||
123 | public function processQuery($schema, $params, array $globals = []) { |
||
175 | |||
176 | /** |
||
177 | * @param \GraphQL\Server\ServerConfig $config |
||
178 | * @param \GraphQL\Server\OperationParams $params |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function executeSingle(ServerConfig $config, OperationParams $params) { |
||
187 | |||
188 | /** |
||
189 | * @param \GraphQL\Server\ServerConfig $config |
||
190 | * @param array $params |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | public function executeBatch(ServerConfig $config, array $params) { |
||
203 | |||
204 | /** |
||
205 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
206 | * @param \GraphQL\Server\ServerConfig $config |
||
207 | * @param \GraphQL\Server\OperationParams $params |
||
208 | * @param bool $batching |
||
209 | * |
||
210 | * @return \GraphQL\Executor\Promise\Promise |
||
211 | */ |
||
212 | protected function executeOperationWithReporting(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
228 | |||
229 | /** |
||
230 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
231 | * @param \GraphQL\Server\ServerConfig $config |
||
232 | * @param \GraphQL\Server\OperationParams $params |
||
233 | * @param bool $batching |
||
234 | * |
||
235 | * @return \GraphQL\Executor\Promise\Promise |
||
236 | */ |
||
237 | protected function executeOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
285 | |||
286 | /** |
||
287 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
288 | * @param \GraphQL\Server\ServerConfig $config |
||
289 | * @param \GraphQL\Server\OperationParams $params |
||
290 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
291 | * |
||
292 | * @return \GraphQL\Executor\Promise\Promise|mixed |
||
293 | */ |
||
294 | protected function executeCacheableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
316 | |||
317 | /** |
||
318 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
319 | * @param \GraphQL\Server\ServerConfig $config |
||
320 | * @param \GraphQL\Server\OperationParams $params |
||
321 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
322 | * |
||
323 | * @return \GraphQL\Executor\Promise\Promise |
||
324 | */ |
||
325 | protected function executeUncachableOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
333 | |||
334 | /** |
||
335 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
336 | * @param \GraphQL\Server\ServerConfig $config |
||
337 | * @param \GraphQL\Server\OperationParams $params |
||
338 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
339 | * |
||
340 | * @return \GraphQL\Executor\Promise\Promise |
||
341 | */ |
||
342 | protected function doExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
376 | |||
377 | /** |
||
378 | * @param \GraphQL\Server\OperationParams $params |
||
379 | * |
||
380 | * @return array |
||
381 | */ |
||
382 | protected function validateOperationParams(OperationParams $params) { |
||
388 | |||
389 | /** |
||
390 | * @param \GraphQL\Server\ServerConfig $config |
||
391 | * @param \GraphQL\Server\OperationParams $params |
||
392 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
393 | * |
||
394 | * @return \GraphQL\Error\Error[] |
||
395 | */ |
||
396 | protected function validateOperation(ServerConfig $config, OperationParams $params, DocumentNode $document) { |
||
417 | |||
418 | /** |
||
419 | * @param \GraphQL\Server\ServerConfig $config |
||
420 | * @param \GraphQL\Server\OperationParams $params |
||
421 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
422 | * @param $operation |
||
423 | * |
||
424 | * @return mixed |
||
425 | */ |
||
426 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
434 | |||
435 | /** |
||
436 | * @param \GraphQL\Server\ServerConfig $config |
||
437 | * @param \GraphQL\Server\OperationParams $params |
||
438 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
439 | * @param $operation |
||
440 | * |
||
441 | * @return mixed |
||
442 | */ |
||
443 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
451 | |||
452 | /** |
||
453 | * @param \GraphQL\Server\ServerConfig $config |
||
454 | * @param \GraphQL\Server\OperationParams $params |
||
455 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
456 | * @param $operation |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
472 | |||
473 | /** |
||
474 | * @param \GraphQL\Server\ServerConfig $config |
||
475 | * @param \GraphQL\Server\OperationParams $params |
||
476 | * |
||
477 | * @return mixed |
||
478 | * @throws \GraphQL\Server\RequestError |
||
479 | */ |
||
480 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
492 | |||
493 | /** |
||
494 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | protected function serializeDocument(DocumentNode $document) { |
||
501 | |||
502 | /** |
||
503 | * @param array $item |
||
504 | * |
||
505 | * @return array |
||
506 | */ |
||
507 | protected function sanitizeRecursive(array $item) { |
||
518 | |||
519 | /** |
||
520 | * @param \GraphQL\Server\OperationParams $params |
||
521 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
522 | * @param \Drupal\Core\Cache\CacheableMetadata $metadata |
||
523 | * |
||
524 | * @return string |
||
525 | */ |
||
526 | protected function cacheIdentifier(OperationParams $params, DocumentNode $document, CacheableMetadata $metadata) { |
||
543 | |||
544 | /** |
||
545 | * Filter unused contexts. |
||
546 | * |
||
547 | * Removes the language contexts from a list of context ids. |
||
548 | * |
||
549 | * @param string[] $contexts |
||
550 | * The list of context id's. |
||
551 | * |
||
552 | * @return string[] |
||
553 | * The filtered list of context id's. |
||
554 | */ |
||
555 | protected function filterCacheContexts(array $contexts) { |
||
560 | } |
||
561 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.