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 |
||
39 | class EntityQuery extends FieldPluginBase implements ContainerFactoryPluginInterface { |
||
|
|||
40 | use DependencySerializationTrait; |
||
41 | |||
42 | /** |
||
43 | * The entity type manager. |
||
44 | * |
||
45 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
46 | */ |
||
47 | protected $entityTypeManager; |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | protected function getCacheDependencies(array $result, $value, array $args, ResolveInfo $info) { |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function resolveValues($value, array $args, ResolveInfo $info) { |
||
89 | |||
90 | /** |
||
91 | * Create an entity query for the plugin's entity type. |
||
92 | * |
||
93 | * @param mixed $value |
||
94 | * The parent entity type. |
||
95 | * @param array $args |
||
96 | * The field arguments array. |
||
97 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
98 | * The resolve info object. |
||
99 | * |
||
100 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
101 | * The entity query object. |
||
102 | */ |
||
103 | protected function getQuery($value, array $args, ResolveInfo $info) { |
||
128 | |||
129 | /** |
||
130 | * Apply the specified sort directives to the query. |
||
131 | * |
||
132 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
133 | * The entity query object. |
||
134 | * @param array $sort |
||
135 | * The sort definitions from the field arguments. |
||
136 | */ |
||
137 | protected function applySort(QueryInterface $query, array $sort) { |
||
143 | |||
144 | /** |
||
145 | * Apply the specified filter conditions to the query. |
||
146 | * |
||
147 | * Recursively picks up all filters and aggregates them into condition groups |
||
148 | * according to the nested structure of the filter argument. |
||
149 | * |
||
150 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
151 | * The entity query object. |
||
152 | * @param array $filter |
||
153 | * The filter definitions from the field arguments. |
||
154 | */ |
||
155 | protected function applyFilter(QueryInterface $query, array $filter) { |
||
158 | |||
159 | /** |
||
160 | * Recursively builds the filter condition groups. |
||
161 | * |
||
162 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
163 | * The entity query object. |
||
164 | * @param array $filter |
||
165 | * The filter definitions from the field arguments. |
||
166 | * |
||
167 | * @return \Drupal\Core\Entity\Query\ConditionInterface |
||
168 | * The generated condition group according to the given filter definitions. |
||
169 | * |
||
170 | * @throws \Youshido\GraphQL\Exception\ResolveException |
||
171 | * If the given operator and value for a filter are invalid. |
||
172 | */ |
||
173 | protected function buildFilterConditions(QueryInterface $query, array $filter) { |
||
231 | |||
232 | /** |
||
233 | * Checks if an operator is a unary operator. |
||
234 | * |
||
235 | * @param string $operator |
||
236 | * The query operator to check against. |
||
237 | * |
||
238 | * @return bool |
||
239 | * TRUE if the given operator is unary, FALSE otherwise. |
||
240 | */ |
||
241 | protected function isUnaryOperator($operator) { |
||
245 | |||
246 | /** |
||
247 | * Checks if an operator is a null operator. |
||
248 | * |
||
249 | * @param string $operator |
||
250 | * The query operator to check against. |
||
251 | * |
||
252 | * @return bool |
||
253 | * TRUE if the given operator is a null operator, FALSE otherwise. |
||
254 | */ |
||
255 | protected function isNullOperator($operator) { |
||
259 | |||
260 | /** |
||
261 | * Checks if an operator is a range operator. |
||
262 | * |
||
263 | * @param string $operator |
||
264 | * The query operator to check against. |
||
265 | * |
||
266 | * @return bool |
||
267 | * TRUE if the given operator is a range operator, FALSE otherwise. |
||
268 | */ |
||
269 | protected function isRangeOperator($operator) { |
||
273 | |||
274 | } |
||
275 |