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 ComplexFieldInterface, 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 | * {@inheritdoc} |
||
54 | */ |
||
55 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
63 | |||
64 | /** |
||
65 | * Calculates the complexity cost of this field. |
||
66 | * |
||
67 | * @param array $args |
||
68 | * The field arguments array. |
||
69 | * @param \Youshido\GraphQL\Config\Field\FieldConfig $fieldConfig |
||
70 | * The field config object. |
||
71 | * @param int $childScore |
||
72 | * The child score. |
||
73 | * |
||
74 | * @return int |
||
75 | * The complexity cost of this field. |
||
76 | */ |
||
77 | public static function calculateCost(array $args, FieldConfig $fieldConfig, $childScore = 0) { |
||
81 | |||
82 | /** |
||
83 | * EntityQuery constructor. |
||
84 | * |
||
85 | * @param array $configuration |
||
86 | * The plugin configuration array. |
||
87 | * @param string $pluginId |
||
88 | * The plugin id. |
||
89 | * @param mixed $pluginDefinition |
||
90 | * The plugin definition. |
||
91 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
92 | * The entity type manager service. |
||
93 | */ |
||
94 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | protected function getCacheDependencies(array $result, $value, array $args, ResolveInfo $info) { |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function resolveValues($value, array $args, ResolveInfo $info) { |
||
119 | |||
120 | /** |
||
121 | * Retrieve the target entity type of this plugin. |
||
122 | * |
||
123 | * @param mixed $value |
||
124 | * The parent value. |
||
125 | * @param array $args |
||
126 | * The field arguments array. |
||
127 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
128 | * The resolve info object. |
||
129 | * |
||
130 | * @return string|null |
||
131 | * The entity type object or NULL if none could be derived. |
||
132 | */ |
||
133 | protected function getEntityType($value, array $args, ResolveInfo $info) { |
||
145 | |||
146 | /** |
||
147 | * Create the full entity query for the plugin's entity type. |
||
148 | * |
||
149 | * @param mixed $value |
||
150 | * The parent entity type. |
||
151 | * @param array $args |
||
152 | * The field arguments array. |
||
153 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
154 | * The resolve info object. |
||
155 | * |
||
156 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
157 | * The entity query object. |
||
158 | */ |
||
159 | protected function getQuery($value, array $args, ResolveInfo $info) { |
||
177 | |||
178 | /** |
||
179 | * Create the basic entity query for the plugin's entity type. |
||
180 | * |
||
181 | * @param mixed $value |
||
182 | * The parent entity type. |
||
183 | * @param array $args |
||
184 | * The field arguments array. |
||
185 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
186 | * The resolve info object. |
||
187 | * |
||
188 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
189 | * The entity query object. |
||
190 | */ |
||
191 | protected function getBaseQuery($value, array $args, ResolveInfo $info) { |
||
202 | |||
203 | /** |
||
204 | * Retrieves an arbitrary value to write into the query metadata. |
||
205 | * |
||
206 | * @param mixed $value |
||
207 | * The parent value. |
||
208 | * @param array $args |
||
209 | * The field arguments array. |
||
210 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
211 | * The resolve info object. |
||
212 | * |
||
213 | * @return mixed |
||
214 | * The query context. |
||
215 | */ |
||
216 | protected function getQueryContext($value, array $args, ResolveInfo $info) { |
||
224 | |||
225 | /** |
||
226 | * Apply the specified revision filtering mode to the query. |
||
227 | * |
||
228 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
229 | * The entity query object. |
||
230 | * @param mixed $mode |
||
231 | * The revision query mode. |
||
232 | * |
||
233 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
234 | * The entity query object. |
||
235 | */ |
||
236 | protected function applyRevisionsMode(QueryInterface $query, $mode) { |
||
245 | |||
246 | /** |
||
247 | * Apply the specified sort directives to the query. |
||
248 | * |
||
249 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
250 | * The entity query object. |
||
251 | * @param mixed $sort |
||
252 | * The sort definitions from the field arguments. |
||
253 | * |
||
254 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
255 | * The entity query object. |
||
256 | */ |
||
257 | protected function applySort(QueryInterface $query, $sort) { |
||
267 | |||
268 | /** |
||
269 | * Apply the specified filter conditions to the query. |
||
270 | * |
||
271 | * Recursively picks up all filters and aggregates them into condition groups |
||
272 | * according to the nested structure of the filter argument. |
||
273 | * |
||
274 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
275 | * The entity query object. |
||
276 | * @param mixed $filter |
||
277 | * The filter definitions from the field arguments. |
||
278 | * |
||
279 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
280 | * The entity query object. |
||
281 | */ |
||
282 | protected function applyFilter(QueryInterface $query, $filter) { |
||
289 | |||
290 | /** |
||
291 | * Recursively builds the filter condition groups. |
||
292 | * |
||
293 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
294 | * The entity query object. |
||
295 | * @param array $filter |
||
296 | * The filter definitions from the field arguments. |
||
297 | * |
||
298 | * @return \Drupal\Core\Entity\Query\ConditionInterface |
||
299 | * The generated condition group according to the given filter definitions. |
||
300 | * |
||
301 | * @throws \Youshido\GraphQL\Exception\ResolveException |
||
302 | * If the given operator and value for a filter are invalid. |
||
303 | */ |
||
304 | protected function buildFilterConditions(QueryInterface $query, array $filter) { |
||
362 | |||
363 | /** |
||
364 | * Checks if an operator is a unary operator. |
||
365 | * |
||
366 | * @param string $operator |
||
367 | * The query operator to check against. |
||
368 | * |
||
369 | * @return bool |
||
370 | * TRUE if the given operator is unary, FALSE otherwise. |
||
371 | */ |
||
372 | protected function isUnaryOperator($operator) { |
||
376 | |||
377 | /** |
||
378 | * Checks if an operator is a null operator. |
||
379 | * |
||
380 | * @param string $operator |
||
381 | * The query operator to check against. |
||
382 | * |
||
383 | * @return bool |
||
384 | * TRUE if the given operator is a null operator, FALSE otherwise. |
||
385 | */ |
||
386 | protected function isNullOperator($operator) { |
||
390 | |||
391 | /** |
||
392 | * Checks if an operator is a range operator. |
||
393 | * |
||
394 | * @param string $operator |
||
395 | * The query operator to check against. |
||
396 | * |
||
397 | * @return bool |
||
398 | * TRUE if the given operator is a range operator, FALSE otherwise. |
||
399 | */ |
||
400 | protected function isRangeOperator($operator) { |
||
404 | } |
||
405 |