Conditions | 16 |
Paths | 197 |
Total Lines | 79 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
100 | public function processQuery($id, $query, array $variables = [], $useCache = TRUE, $maxComplexity = NULL, $bypassSecurity = FALSE) { |
||
101 | if (!$schema = $this->schemaLoader->getSchema($id)) { |
||
102 | throw new \InvalidArgumentException(sprintf('Could not load schema %s', [$id])); |
||
103 | } |
||
104 | |||
105 | // The processor isolates the parsing and execution of the query. |
||
106 | $processor = new Processor($schema, $query, $variables); |
||
107 | $context = $processor->getExecutionContext(); |
||
108 | $container = $context->getContainer(); |
||
109 | |||
110 | // Set up some parameters in the container. |
||
111 | $secure = $bypassSecurity || $this->currentUser->hasPermission('bypass graphql field security'); |
||
112 | $metadata = new CacheableMetadata(); |
||
113 | $container->set('secure', $secure); |
||
114 | $container->set('metadata', $metadata); |
||
115 | |||
116 | $visitor = new CacheMetadataVisitor(); |
||
117 | /** @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata */ |
||
118 | $metadata = $processor->reduceRequest($visitor, function ($result) { |
||
119 | $metadata = new CacheableMetadata(); |
||
120 | $metadata->addCacheableDependency($result); |
||
121 | // TODO: Use a cache context that includes the AST instead of the raw values. |
||
122 | $metadata->addCacheContexts(['gql']); |
||
123 | $metadata->addCacheTags(['graphql_response']); |
||
124 | |||
125 | return $metadata; |
||
126 | }) ?: new CacheableMetadata(); |
||
127 | |||
128 | // The cache identifier will later be re-used for writing the cache entry. |
||
129 | $cid = $this->getCacheIdentifier($metadata); |
||
130 | if (!empty($useCache) && $metadata->getCacheMaxAge() !== 0) { |
||
131 | if (($cache = $this->cacheBackend->get($cid)) && $result = $cache->data) { |
||
132 | return $result; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | if (!empty($maxComplexity)) { |
||
137 | $visitor = new MaxComplexityVisitor(); |
||
138 | $processor->reduceRequest($visitor, function ($result) use ($maxComplexity) { |
||
139 | if ($result > $maxComplexity) { |
||
140 | throw new ResolveException('Maximum complexity exceeded.'); |
||
141 | } |
||
142 | }); |
||
143 | } |
||
144 | |||
145 | // Retrieve the result from the processor. |
||
146 | $data = $processor->resolveRequest(); |
||
147 | |||
148 | // Add collected cache metadata from the query processor. |
||
149 | if ($container->has('metadata') && ($collected = $container->get('metadata'))) { |
||
150 | if ($collected instanceof CacheableDependencyInterface) { |
||
|
|||
151 | $metadata->addCacheableDependency($collected); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | // If we encountered any errors, do not cache anything. |
||
156 | if ($context->hasErrors()) { |
||
157 | $metadata->setCacheMaxAge(0); |
||
158 | } |
||
159 | |||
160 | // Build the result object. The keys are only set if they contain data. |
||
161 | $result = new QueryResult(array_filter([ |
||
162 | 'data' => $data, |
||
163 | 'errors' => $processor->getExecutionContext()->getErrorsArray(), |
||
164 | ]), $metadata); |
||
165 | |||
166 | // Write the query result into the cache if the cache metadata permits. |
||
167 | if (!empty($useCache) && $metadata->getCacheMaxAge() !== 0) { |
||
168 | $tags = $metadata->getCacheTags(); |
||
169 | $expire = $this->maxAgeToExpire($metadata->getCacheMaxAge()); |
||
170 | |||
171 | // The cache identifier for the cache entry is built based on the |
||
172 | // previously extracted cache contexts from the query visitor. That |
||
173 | // means, that dynamically returned cache contexts have no effect. |
||
174 | $this->cacheBackend->set($cid, $result, $expire, $tags); |
||
175 | } |
||
176 | |||
177 | return $result; |
||
178 | } |
||
179 | |||
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.