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 | * Processor constructor. |
||
57 | * |
||
58 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
59 | * The current user. |
||
60 | * @param \Drupal\graphql\Plugin\SchemaPluginManager $pluginManager |
||
61 | * The schema plugin manager. |
||
62 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
63 | * The query provider service. |
||
64 | */ |
||
65 | public function __construct( |
||
74 | |||
75 | /** |
||
76 | * Processes one or multiple graphql operations. |
||
77 | * |
||
78 | * @param string $schema |
||
79 | * The plugin id of the schema to use. |
||
80 | * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $params |
||
81 | * The graphql operation(s) to execute. |
||
82 | * @param array $globals |
||
83 | * The query context. |
||
84 | * |
||
85 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult|\Drupal\graphql\GraphQL\Execution\QueryResult[] |
||
86 | * The query result. |
||
87 | * |
||
88 | */ |
||
89 | public function processQuery($schema, $params, array $globals = []) { |
||
136 | |||
137 | /** |
||
138 | * @param \GraphQL\Server\ServerConfig $config |
||
139 | * @param \GraphQL\Server\OperationParams $params |
||
140 | * |
||
141 | * @return mixed |
||
142 | */ |
||
143 | public function executeSingle(ServerConfig $config, OperationParams $params) { |
||
148 | |||
149 | /** |
||
150 | * @param \GraphQL\Server\ServerConfig $config |
||
151 | * @param array $params |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public function executeBatch(ServerConfig $config, array $params) { |
||
164 | |||
165 | /** |
||
166 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
167 | * @param \GraphQL\Server\ServerConfig $config |
||
168 | * @param \GraphQL\Server\OperationParams $params |
||
169 | * @param bool $batching |
||
170 | * |
||
171 | * @return \GraphQL\Executor\Promise\Promise |
||
172 | */ |
||
173 | protected function executeOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
280 | |||
281 | /** |
||
282 | * @param \GraphQL\Server\ServerConfig $config |
||
283 | * @param \GraphQL\Server\OperationParams $params |
||
284 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
285 | * @param $operation |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
297 | |||
298 | /** |
||
299 | * @param \GraphQL\Server\ServerConfig $config |
||
300 | * @param \GraphQL\Server\OperationParams $params |
||
301 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
302 | * @param $operation |
||
303 | * |
||
304 | * @return mixed |
||
305 | */ |
||
306 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
314 | |||
315 | /** |
||
316 | * @param \GraphQL\Server\ServerConfig $config |
||
317 | * @param \GraphQL\Server\OperationParams $params |
||
318 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
319 | * @param $operation |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
335 | |||
336 | /** |
||
337 | * @param \GraphQL\Server\ServerConfig $config |
||
338 | * @param \GraphQL\Server\OperationParams $params |
||
339 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
340 | * @param $operation |
||
341 | * |
||
342 | * @return int|null |
||
343 | */ |
||
344 | protected function resolveAllowedComplexity(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
347 | |||
348 | /** |
||
349 | * @param \GraphQL\Server\ServerConfig $config |
||
350 | * @param \GraphQL\Server\OperationParams $params |
||
351 | * |
||
352 | * @return mixed |
||
353 | * @throws \GraphQL\Server\RequestError |
||
354 | */ |
||
355 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
367 | |||
368 | /** |
||
369 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | protected function serializeDocument(DocumentNode $document) { |
||
376 | |||
377 | /** |
||
378 | * @param array $item |
||
379 | * |
||
380 | * @return array |
||
381 | */ |
||
382 | protected function sanitizeRecursive(array $item) { |
||
393 | |||
394 | } |
||
395 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.