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:
1 | <?php |
||
30 | class QueryProcessor { |
||
31 | |||
32 | /** |
||
33 | * The current user account. |
||
34 | * |
||
35 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
36 | */ |
||
37 | protected $currentUser; |
||
38 | |||
39 | /** |
||
40 | * The schema plugin manager. |
||
41 | * |
||
42 | * @var \Drupal\graphql\Plugin\SchemaPluginManager |
||
43 | */ |
||
44 | protected $pluginManager; |
||
45 | |||
46 | /** |
||
47 | * The query provider service. |
||
48 | * |
||
49 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
50 | */ |
||
51 | protected $queryProvider; |
||
52 | |||
53 | /** |
||
54 | * The renderer service. |
||
55 | * |
||
56 | * @var \Drupal\Core\Render\RendererInterface |
||
57 | */ |
||
58 | protected $renderer; |
||
59 | |||
60 | /** |
||
61 | * Processor constructor. |
||
62 | * |
||
63 | * @param \Drupal\Core\Render\RendererInterface $renderer |
||
64 | * The renderer service. |
||
65 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
66 | * The current user. |
||
67 | * @param \Drupal\graphql\Plugin\SchemaPluginManager $pluginManager |
||
68 | * The schema plugin manager. |
||
69 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
70 | * The query provider service. |
||
71 | */ |
||
72 | public function __construct( |
||
83 | |||
84 | /** |
||
85 | * Processes one or multiple graphql operations. |
||
86 | * |
||
87 | * @param string $schema |
||
88 | * The plugin id of the schema to use. |
||
89 | * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $params |
||
90 | * The graphql operation(s) to execute. |
||
91 | * @param mixed $context |
||
92 | * The query context. |
||
93 | * @param bool $debug |
||
94 | * Whether to run this query in debugging mode. |
||
95 | * |
||
96 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult |
||
97 | * The query result. |
||
98 | */ |
||
99 | public function processQuery($schema, $params, $context = NULL, $debug = FALSE) { |
||
120 | |||
121 | /** |
||
122 | * Executes one or multiple graphql operations. |
||
123 | * |
||
124 | * @param \GraphQL\Server\ServerConfig $config |
||
125 | * The server config. |
||
126 | * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $params |
||
127 | * The graphql operation(s) to execute. |
||
128 | * |
||
129 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult |
||
130 | * The result of executing the operations. |
||
131 | */ |
||
132 | protected function executeQuery(ServerConfig $config, $params) { |
||
154 | |||
155 | /** |
||
156 | * @param \GraphQL\Server\ServerConfig $config |
||
157 | * @param \GraphQL\Server\OperationParams $params |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | public function executeSingle(ServerConfig $config, OperationParams $params) { |
||
166 | |||
167 | /** |
||
168 | * @param \GraphQL\Server\ServerConfig $config |
||
169 | * @param array $params |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | public function executeBatch(ServerConfig $config, array $params) { |
||
182 | |||
183 | /** |
||
184 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
185 | * @param \GraphQL\Server\ServerConfig $config |
||
186 | * @param \GraphQL\Server\OperationParams $params |
||
187 | * @param bool $batching |
||
188 | * |
||
189 | * @return \GraphQL\Executor\Promise\Promise |
||
190 | */ |
||
191 | protected function promiseToExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
258 | |||
259 | /** |
||
260 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
261 | * @param \GraphQL\Type\Schema $schema |
||
262 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
263 | * @param null $root |
||
264 | * @param null $context |
||
265 | * @param null $variables |
||
266 | * @param null $operation |
||
267 | * @param callable|NULL $resolver |
||
268 | * @param array|NULL $rules |
||
269 | * |
||
270 | * @return \GraphQL\Executor\Promise\Promise |
||
271 | */ |
||
272 | protected function promiseToExecute( |
||
295 | |||
296 | /** |
||
297 | * @param \GraphQL\Server\ServerConfig $config |
||
298 | * @param \GraphQL\Server\OperationParams $params |
||
299 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
300 | * @param $operation |
||
301 | * |
||
302 | * @return callable|mixed |
||
303 | */ |
||
304 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
312 | |||
313 | /** |
||
314 | * @param \GraphQL\Server\ServerConfig $config |
||
315 | * @param \GraphQL\Server\OperationParams $params |
||
316 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
317 | * @param $operation |
||
318 | * |
||
319 | * @return callable|mixed |
||
320 | */ |
||
321 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
329 | |||
330 | /** |
||
331 | * @param \GraphQL\Server\ServerConfig $config |
||
332 | * @param \GraphQL\Server\OperationParams $params |
||
333 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
334 | * @param $operation |
||
335 | * |
||
336 | * @return array|callable |
||
337 | */ |
||
338 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
350 | |||
351 | /** |
||
352 | * @param \GraphQL\Server\ServerConfig $config |
||
353 | * @param \GraphQL\Server\OperationParams $params |
||
354 | * |
||
355 | * @return mixed |
||
356 | * @throws \GraphQL\Server\RequestError |
||
357 | */ |
||
358 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
370 | |||
371 | } |
||
372 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.