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 EntityQuery 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 EntityQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class EntityQuery extends FieldPluginBase implements ContainerFactoryPluginInterface { |
||
43 | use DependencySerializationTrait; |
||
44 | |||
45 | /** |
||
46 | * The entity type manager. |
||
47 | * |
||
48 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
49 | */ |
||
50 | protected $entityTypeManager; |
||
51 | |||
52 | /** |
||
53 | * Service config.factory. |
||
54 | * |
||
55 | * @var \Drupal\Core\Config\ConfigFactoryInterface |
||
56 | */ |
||
57 | protected $config; |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
71 | |||
72 | /** |
||
73 | * EntityQuery constructor. |
||
74 | * |
||
75 | * @param array $configuration |
||
76 | * The plugin configuration array. |
||
77 | * @param string $pluginId |
||
78 | * The plugin id. |
||
79 | * @param mixed $pluginDefinition |
||
80 | * The plugin definition. |
||
81 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
82 | * The entity type manager service. |
||
83 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config |
||
84 | * Service config.factory. |
||
85 | */ |
||
86 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config) { |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | protected function getCacheDependencies(array $result, $value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
112 | |||
113 | /** |
||
114 | * Retrieve the target entity type of this plugin. |
||
115 | * |
||
116 | * @param mixed $value |
||
117 | * The parent value. |
||
118 | * @param array $args |
||
119 | * The field arguments array. |
||
120 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
121 | * The resolve context. |
||
122 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
123 | * The resolve info object. |
||
124 | * |
||
125 | * @return null|string |
||
126 | * The entity type object or NULL if none could be derived. |
||
127 | */ |
||
128 | protected function getEntityType($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
140 | |||
141 | /** |
||
142 | * Create the full entity query for the plugin's entity type. |
||
143 | * |
||
144 | * @param mixed $value |
||
145 | * The parent entity type. |
||
146 | * @param array $args |
||
147 | * The field arguments array. |
||
148 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
149 | * The resolve context. |
||
150 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
151 | * The resolve info object. |
||
152 | * |
||
153 | * @return \Drupal\Core\Entity\Query\QueryInterface|null |
||
154 | * The entity query object. |
||
155 | */ |
||
156 | protected function getQuery($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
185 | |||
186 | /** |
||
187 | * Create the basic entity query for the plugin's entity type. |
||
188 | * |
||
189 | * @param mixed $value |
||
190 | * The parent entity type. |
||
191 | * @param array $args |
||
192 | * The field arguments array. |
||
193 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
194 | * The resolve context. |
||
195 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
196 | * The resolve info object. |
||
197 | * |
||
198 | * @return \Drupal\Core\Entity\Query\QueryInterface|null |
||
199 | * The entity query object. |
||
200 | */ |
||
201 | protected function getBaseQuery($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
212 | |||
213 | /** |
||
214 | * Retrieves an arbitrary value to write into the query metadata. |
||
215 | * |
||
216 | * @param mixed $value |
||
217 | * The parent value. |
||
218 | * @param array $args |
||
219 | * The field arguments array. |
||
220 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
221 | * The resolve context. |
||
222 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
223 | * The resolve info object. |
||
224 | * |
||
225 | * @return mixed |
||
226 | * The query context. |
||
227 | */ |
||
228 | protected function getQueryContext($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
236 | |||
237 | /** |
||
238 | * Apply the specified revision filtering mode to the query. |
||
239 | * |
||
240 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
241 | * The entity query object. |
||
242 | * @param mixed $mode |
||
243 | * The revision query mode. |
||
244 | * |
||
245 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
246 | * The entity query object. |
||
247 | */ |
||
248 | protected function applyRevisionsMode(QueryInterface $query, $mode) { |
||
262 | |||
263 | /** |
||
264 | * Apply the specified sort directives to the query. |
||
265 | * |
||
266 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
267 | * The entity query object. |
||
268 | * @param mixed $sort |
||
269 | * The sort definitions from the field arguments. |
||
270 | * |
||
271 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
272 | * The entity query object. |
||
273 | */ |
||
274 | protected function applySort(QueryInterface $query, $sort) { |
||
285 | |||
286 | /** |
||
287 | * Apply the specified filter conditions to the query. |
||
288 | * |
||
289 | * Recursively picks up all filters and aggregates them into condition groups |
||
290 | * according to the nested structure of the filter argument. |
||
291 | * |
||
292 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
293 | * The entity query object. |
||
294 | * @param mixed $filter |
||
295 | * The filter definitions from the field arguments. |
||
296 | * |
||
297 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
298 | * The entity query object. |
||
299 | */ |
||
300 | protected function applyFilter(QueryInterface $query, $filter) { |
||
311 | |||
312 | /** |
||
313 | * Recursively builds the filter condition groups. |
||
314 | * |
||
315 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
316 | * The entity query object. |
||
317 | * @param array $filter |
||
318 | * The filter definitions from the field arguments. |
||
319 | * |
||
320 | * @return \Drupal\Core\Entity\Query\ConditionInterface |
||
321 | * The generated condition group according to the given filter definitions. |
||
322 | * |
||
323 | * @throws \GraphQL\Error\Error |
||
324 | * If the given operator and value for a filter are invalid. |
||
325 | */ |
||
326 | protected function buildFilterConditions(QueryInterface $query, array $filter) { |
||
393 | |||
394 | /** |
||
395 | * Checks if an operator is a unary operator. |
||
396 | * |
||
397 | * @param string $operator |
||
398 | * The query operator to check against. |
||
399 | * |
||
400 | * @return bool |
||
401 | * TRUE if the given operator is unary, FALSE otherwise. |
||
402 | */ |
||
403 | protected function isUnaryOperator($operator) { |
||
407 | |||
408 | /** |
||
409 | * Checks if an operator is a null operator. |
||
410 | * |
||
411 | * @param string $operator |
||
412 | * The query operator to check against. |
||
413 | * |
||
414 | * @return bool |
||
415 | * TRUE if the given operator is a null operator, FALSE otherwise. |
||
416 | */ |
||
417 | protected function isNullOperator($operator) { |
||
421 | |||
422 | /** |
||
423 | * Checks if an operator is a range operator. |
||
424 | * |
||
425 | * @param string $operator |
||
426 | * The query operator to check against. |
||
427 | * |
||
428 | * @return bool |
||
429 | * TRUE if the given operator is a range operator, FALSE otherwise. |
||
430 | */ |
||
431 | protected function isRangeOperator($operator) { |
||
435 | |||
436 | } |
||
437 |
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.