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 |
||
27 | class QueryProcessor { |
||
28 | |||
29 | /** |
||
30 | * The current user account. |
||
31 | * |
||
32 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
33 | */ |
||
34 | protected $currentUser; |
||
35 | |||
36 | /** |
||
37 | * The schema plugin manager. |
||
38 | * |
||
39 | * @var \Drupal\graphql\Plugin\SchemaPluginManager |
||
40 | */ |
||
41 | protected $pluginManager; |
||
42 | |||
43 | /** |
||
44 | * The query provider service. |
||
45 | * |
||
46 | * @var \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface |
||
47 | */ |
||
48 | protected $queryProvider; |
||
49 | |||
50 | /** |
||
51 | * Processor constructor. |
||
52 | * |
||
53 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
54 | * The current user. |
||
55 | * @param \Drupal\graphql\Plugin\SchemaPluginManager $pluginManager |
||
56 | * The schema plugin manager. |
||
57 | * @param \Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface $queryProvider |
||
58 | * The query provider service. |
||
59 | */ |
||
60 | public function __construct( |
||
69 | |||
70 | /** |
||
71 | * Processes one or multiple graphql operations. |
||
72 | * |
||
73 | * @param string $schema |
||
74 | * The plugin id of the schema to use. |
||
75 | * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $params |
||
76 | * The graphql operation(s) to execute. |
||
77 | * @param mixed $context |
||
78 | * The query context. |
||
79 | * @param bool $debug |
||
80 | * Whether to run this query in debugging mode. |
||
81 | * |
||
82 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult|\Drupal\graphql\GraphQL\Execution\QueryResult[] |
||
83 | * The query result. |
||
84 | */ |
||
85 | public function processQuery($schema, $params, $context = NULL, $debug = FALSE) { |
||
110 | |||
111 | /** |
||
112 | * @param \GraphQL\Server\ServerConfig $config |
||
113 | * @param \GraphQL\Server\OperationParams $params |
||
114 | * |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function executeSingle(ServerConfig $config, OperationParams $params) { |
||
122 | |||
123 | /** |
||
124 | * @param \GraphQL\Server\ServerConfig $config |
||
125 | * @param array $params |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | public function executeBatch(ServerConfig $config, array $params) { |
||
138 | |||
139 | /** |
||
140 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
141 | * @param \GraphQL\Server\ServerConfig $config |
||
142 | * @param \GraphQL\Server\OperationParams $params |
||
143 | * @param bool $batching |
||
144 | * |
||
145 | * @return \GraphQL\Executor\Promise\Promise |
||
146 | */ |
||
147 | protected function promiseToExecuteOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) { |
||
212 | |||
213 | /** |
||
214 | * @param \GraphQL\Executor\Promise\PromiseAdapter $adapter |
||
215 | * @param \GraphQL\Type\Schema $schema |
||
216 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
217 | * @param null $root |
||
218 | * @param null $context |
||
219 | * @param null $variables |
||
220 | * @param null $operation |
||
221 | * @param callable|NULL $resolver |
||
222 | * @param array|NULL $rules |
||
223 | * |
||
224 | * @return \GraphQL\Executor\Promise\Promise |
||
225 | */ |
||
226 | protected function promiseToExecute( |
||
266 | |||
267 | /** |
||
268 | * @param \GraphQL\Server\ServerConfig $config |
||
269 | * @param \GraphQL\Server\OperationParams $params |
||
270 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
271 | * @param $operation |
||
272 | * |
||
273 | * @return callable|mixed |
||
274 | */ |
||
275 | View Code Duplication | protected function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
283 | |||
284 | /** |
||
285 | * @param \GraphQL\Server\ServerConfig $config |
||
286 | * @param \GraphQL\Server\OperationParams $params |
||
287 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
288 | * @param $operation |
||
289 | * |
||
290 | * @return callable|mixed |
||
291 | */ |
||
292 | View Code Duplication | protected function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
|
300 | |||
301 | /** |
||
302 | * @param \GraphQL\Server\ServerConfig $config |
||
303 | * @param \GraphQL\Server\OperationParams $params |
||
304 | * @param \GraphQL\Language\AST\DocumentNode $document |
||
305 | * @param $operation |
||
306 | * |
||
307 | * @return array|callable |
||
308 | */ |
||
309 | protected function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $document, $operation) { |
||
321 | |||
322 | /** |
||
323 | * @param \GraphQL\Server\ServerConfig $config |
||
324 | * @param \GraphQL\Server\OperationParams $params |
||
325 | * |
||
326 | * @return mixed |
||
327 | * @throws \GraphQL\Server\RequestError |
||
328 | */ |
||
329 | protected function loadPersistedQuery(ServerConfig $config, OperationParams $params) { |
||
341 | |||
342 | } |
||
343 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.