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 |
||
| 40 | class EntityQuery extends FieldPluginBase implements ContainerFactoryPluginInterface { |
||
|
|
|||
| 41 | use DependencySerializationTrait; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The entity type manager. |
||
| 45 | * |
||
| 46 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
| 47 | */ |
||
| 48 | protected $entityTypeManager; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * EntityQuery constructor. |
||
| 64 | * |
||
| 65 | * @param array $configuration |
||
| 66 | * The plugin configuration array. |
||
| 67 | * @param string $pluginId |
||
| 68 | * The plugin id. |
||
| 69 | * @param mixed $pluginDefinition |
||
| 70 | * The plugin definition. |
||
| 71 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
| 72 | * The entity type manager service. |
||
| 73 | */ |
||
| 74 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | protected function getCacheDependencies(array $result, $value, array $args, ResolveInfo $info) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | public function resolveValues($value, array $args, ResolveInfo $info) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Retrieve the target entity type of this plugin. |
||
| 102 | * |
||
| 103 | * @param mixed $value |
||
| 104 | * The parent value. |
||
| 105 | * @param array $args |
||
| 106 | * The field arguments array. |
||
| 107 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 108 | * The resolve info object. |
||
| 109 | * |
||
| 110 | * @return string|null |
||
| 111 | * The entity type object or NULL if none could be derived. |
||
| 112 | */ |
||
| 113 | protected function getEntityType($value, array $args, ResolveInfo $info) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Create the full entity query for the plugin's entity type. |
||
| 128 | * |
||
| 129 | * @param mixed $value |
||
| 130 | * The parent entity type. |
||
| 131 | * @param array $args |
||
| 132 | * The field arguments array. |
||
| 133 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 134 | * The resolve info object. |
||
| 135 | * |
||
| 136 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 137 | * The entity query object. |
||
| 138 | */ |
||
| 139 | protected function getQuery($value, array $args, ResolveInfo $info) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Create the basic entity query for the plugin's entity type. |
||
| 160 | * |
||
| 161 | * @param mixed $value |
||
| 162 | * The parent entity type. |
||
| 163 | * @param array $args |
||
| 164 | * The field arguments array. |
||
| 165 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 166 | * The resolve info object. |
||
| 167 | * |
||
| 168 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 169 | * The entity query object. |
||
| 170 | */ |
||
| 171 | protected function getBaseQuery($value, array $args, ResolveInfo $info) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Retrieves an arbitrary value to write into the query metadata. |
||
| 185 | * |
||
| 186 | * @param mixed $value |
||
| 187 | * The parent value. |
||
| 188 | * @param array $args |
||
| 189 | * The field arguments array. |
||
| 190 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 191 | * The resolve info object. |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | * The query context. |
||
| 195 | */ |
||
| 196 | protected function getQueryContext($value, array $args, ResolveInfo $info) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Apply the specified revision filtering mode to the query. |
||
| 207 | * |
||
| 208 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 209 | * The entity query object. |
||
| 210 | * @param mixed $mode |
||
| 211 | * The revision query mode. |
||
| 212 | * |
||
| 213 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 214 | * The entity query object. |
||
| 215 | */ |
||
| 216 | protected function applyRevisionsMode(QueryInterface $query, $mode) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Apply the specified sort directives to the query. |
||
| 228 | * |
||
| 229 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 230 | * The entity query object. |
||
| 231 | * @param mixed $sort |
||
| 232 | * The sort definitions from the field arguments. |
||
| 233 | * |
||
| 234 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 235 | * The entity query object. |
||
| 236 | */ |
||
| 237 | protected function applySort(QueryInterface $query, $sort) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Apply the specified filter conditions to the query. |
||
| 250 | * |
||
| 251 | * Recursively picks up all filters and aggregates them into condition groups |
||
| 252 | * according to the nested structure of the filter argument. |
||
| 253 | * |
||
| 254 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 255 | * The entity query object. |
||
| 256 | * @param mixed $filter |
||
| 257 | * The filter definitions from the field arguments. |
||
| 258 | * |
||
| 259 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 260 | * The entity query object. |
||
| 261 | */ |
||
| 262 | protected function applyFilter(QueryInterface $query, $filter) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Recursively builds the filter condition groups. |
||
| 272 | * |
||
| 273 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 274 | * The entity query object. |
||
| 275 | * @param array $filter |
||
| 276 | * The filter definitions from the field arguments. |
||
| 277 | * |
||
| 278 | * @return \Drupal\Core\Entity\Query\ConditionInterface |
||
| 279 | * The generated condition group according to the given filter definitions. |
||
| 280 | * |
||
| 281 | * @throws \Youshido\GraphQL\Exception\ResolveException |
||
| 282 | * If the given operator and value for a filter are invalid. |
||
| 283 | */ |
||
| 284 | protected function buildFilterConditions(QueryInterface $query, array $filter) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Checks if an operator is a unary operator. |
||
| 345 | * |
||
| 346 | * @param string $operator |
||
| 347 | * The query operator to check against. |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | * TRUE if the given operator is unary, FALSE otherwise. |
||
| 351 | */ |
||
| 352 | protected function isUnaryOperator($operator) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Checks if an operator is a null operator. |
||
| 359 | * |
||
| 360 | * @param string $operator |
||
| 361 | * The query operator to check against. |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | * TRUE if the given operator is a null operator, FALSE otherwise. |
||
| 365 | */ |
||
| 366 | protected function isNullOperator($operator) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Checks if an operator is a range operator. |
||
| 373 | * |
||
| 374 | * @param string $operator |
||
| 375 | * The query operator to check against. |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | * TRUE if the given operator is a range operator, FALSE otherwise. |
||
| 379 | */ |
||
| 380 | protected function isRangeOperator($operator) { |
||
| 384 | |||
| 385 | } |
||
| 386 |