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 |
||
17 | class QueryProcessor { |
||
18 | |||
19 | /** |
||
20 | * The current user account. |
||
21 | * |
||
22 | * @var \Drupal\Core\Session\AccountProxyInterface |
||
23 | */ |
||
24 | protected $currentUser; |
||
25 | |||
26 | /** |
||
27 | * The schema loader service. |
||
28 | * |
||
29 | * @var \Drupal\graphql\GraphQL\Schema\SchemaLoader |
||
30 | */ |
||
31 | protected $schemaLoader; |
||
32 | |||
33 | /** |
||
34 | * The cache contexts manager service. |
||
35 | * |
||
36 | * @var \Drupal\Core\Cache\Context\CacheContextsManager |
||
37 | */ |
||
38 | protected $contextsManager; |
||
39 | |||
40 | /** |
||
41 | * The cache backend for caching responses. |
||
42 | * |
||
43 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
44 | */ |
||
45 | protected $cacheBackend; |
||
46 | |||
47 | /** |
||
48 | * The request stack. |
||
49 | * |
||
50 | * @var \Symfony\Component\HttpFoundation\RequestStack |
||
51 | */ |
||
52 | protected $requestStack; |
||
53 | |||
54 | /** |
||
55 | * QueryProcessor constructor. |
||
56 | * |
||
57 | * @param \Drupal\graphql\GraphQL\Schema\SchemaLoader $schemaLoader |
||
58 | * The schema loader service. |
||
59 | * @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
||
60 | * The current user. |
||
61 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
||
62 | * @param \Drupal\Core\Cache\Context\CacheContextsManager $contextsManager |
||
63 | * The cache contexts manager service. |
||
64 | * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
||
65 | * The cache backend for caching response. |
||
66 | */ |
||
67 | public function __construct( |
||
80 | |||
81 | /** |
||
82 | * Processes a graphql query. |
||
83 | * |
||
84 | * @param string $id |
||
85 | * The name of the schema to process the query against. |
||
86 | * @param string $query |
||
87 | * The GraphQL query. |
||
88 | * @param array $variables |
||
89 | * The query variables. |
||
90 | * @param bool $useCache |
||
91 | * Whether to use caching. |
||
92 | * @param int|null $maxComplexity |
||
93 | * The maximum complexity of the query or NULL if any complexity is allowed. |
||
94 | * @param bool $bypassSecurity |
||
95 | * Bypass field security |
||
96 | * |
||
97 | * @return \Drupal\graphql\GraphQL\Execution\QueryResult . |
||
98 | * The query result. |
||
99 | */ |
||
100 | public function processQuery($id, $query, array $variables = [], $useCache = TRUE, $maxComplexity = NULL, $bypassSecurity = FALSE) { |
||
179 | |||
180 | /** |
||
181 | * Maps a max age value to an "expire" value for the Cache API. |
||
182 | * |
||
183 | * @param int $maxAge |
||
184 | * A max age value. |
||
185 | * |
||
186 | * @return int |
||
187 | * A corresponding "expire" value. |
||
188 | * |
||
189 | * @see \Drupal\Core\Cache\CacheBackendInterface::set() |
||
190 | */ |
||
191 | View Code Duplication | protected function maxAgeToExpire($maxAge) { |
|
198 | |||
199 | /** |
||
200 | * Generates a cache identifier for the passed cache contexts. |
||
201 | * |
||
202 | * @param \Drupal\Core\Cache\CacheableDependencyInterface $metadata |
||
203 | * Optional array of cache context tokens. |
||
204 | * |
||
205 | * @return string The generated cache identifier. |
||
206 | * The generated cache identifier. |
||
207 | */ |
||
208 | protected function getCacheIdentifier(CacheableDependencyInterface $metadata) { |
||
213 | |||
214 | } |
||
215 |
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.