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 |
||
| 41 | class EntityQuery extends FieldPluginBase implements ContainerFactoryPluginInterface { |
||
|
|
|||
| 42 | use DependencySerializationTrait; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The entity type manager. |
||
| 46 | * |
||
| 47 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
| 48 | */ |
||
| 49 | protected $entityTypeManager; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | protected function getCacheDependencies(array $result, $value, array $args, ResolveInfo $info) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | public function resolveValues($value, array $args, ResolveInfo $info) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Retrieve the target entity type of this plugin. |
||
| 94 | * |
||
| 95 | * @param mixed $value |
||
| 96 | * The parent entity type. |
||
| 97 | * @param array $args |
||
| 98 | * The field arguments array. |
||
| 99 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 100 | * The resolve info object. |
||
| 101 | * |
||
| 102 | * @return string|null |
||
| 103 | * The entity type object or NULL if none could be derived. |
||
| 104 | */ |
||
| 105 | protected function getEntityType($value, array $args, ResolveInfo $info) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Create the full entity query for the plugin's entity type. |
||
| 120 | * |
||
| 121 | * @param mixed $value |
||
| 122 | * The parent entity type. |
||
| 123 | * @param array $args |
||
| 124 | * The field arguments array. |
||
| 125 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 126 | * The resolve info object. |
||
| 127 | * |
||
| 128 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 129 | * The entity query object. |
||
| 130 | */ |
||
| 131 | protected function getQuery($value, array $args, ResolveInfo $info) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Create the basic entity query for the plugin's entity type. |
||
| 152 | * |
||
| 153 | * @param mixed $value |
||
| 154 | * The parent entity type. |
||
| 155 | * @param array $args |
||
| 156 | * The field arguments array. |
||
| 157 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
| 158 | * The resolve info object. |
||
| 159 | * |
||
| 160 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 161 | * The entity query object. |
||
| 162 | */ |
||
| 163 | protected function getBaseQuery($value, array $args, ResolveInfo $info) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Apply the specified revision filtering mode to the query. |
||
| 175 | * |
||
| 176 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 177 | * The entity query object. |
||
| 178 | * @param mixed $mode |
||
| 179 | * The revision query mode. |
||
| 180 | * |
||
| 181 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 182 | * The entity query object. |
||
| 183 | */ |
||
| 184 | protected function applyRevisionsMode(QueryInterface $query, $mode) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Apply the specified sort directives to the query. |
||
| 196 | * |
||
| 197 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 198 | * The entity query object. |
||
| 199 | * @param mixed $sort |
||
| 200 | * The sort definitions from the field arguments. |
||
| 201 | * |
||
| 202 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 203 | * The entity query object. |
||
| 204 | */ |
||
| 205 | protected function applySort(QueryInterface $query, $sort) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Apply the specified filter conditions to the query. |
||
| 218 | * |
||
| 219 | * Recursively picks up all filters and aggregates them into condition groups |
||
| 220 | * according to the nested structure of the filter argument. |
||
| 221 | * |
||
| 222 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 223 | * The entity query object. |
||
| 224 | * @param mixed $filter |
||
| 225 | * The filter definitions from the field arguments. |
||
| 226 | * |
||
| 227 | * @return \Drupal\Core\Entity\Query\QueryInterface |
||
| 228 | * The entity query object. |
||
| 229 | */ |
||
| 230 | protected function applyFilter(QueryInterface $query, $filter) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Recursively builds the filter condition groups. |
||
| 240 | * |
||
| 241 | * @param \Drupal\Core\Entity\Query\QueryInterface $query |
||
| 242 | * The entity query object. |
||
| 243 | * @param array $filter |
||
| 244 | * The filter definitions from the field arguments. |
||
| 245 | * |
||
| 246 | * @return \Drupal\Core\Entity\Query\ConditionInterface |
||
| 247 | * The generated condition group according to the given filter definitions. |
||
| 248 | * |
||
| 249 | * @throws \Youshido\GraphQL\Exception\ResolveException |
||
| 250 | * If the given operator and value for a filter are invalid. |
||
| 251 | */ |
||
| 252 | protected function buildFilterConditions(QueryInterface $query, array $filter) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks if an operator is a unary operator. |
||
| 313 | * |
||
| 314 | * @param string $operator |
||
| 315 | * The query operator to check against. |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | * TRUE if the given operator is unary, FALSE otherwise. |
||
| 319 | */ |
||
| 320 | protected function isUnaryOperator($operator) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Checks if an operator is a null operator. |
||
| 327 | * |
||
| 328 | * @param string $operator |
||
| 329 | * The query operator to check against. |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | * TRUE if the given operator is a null operator, FALSE otherwise. |
||
| 333 | */ |
||
| 334 | protected function isNullOperator($operator) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Checks if an operator is a range operator. |
||
| 341 | * |
||
| 342 | * @param string $operator |
||
| 343 | * The query operator to check against. |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | * TRUE if the given operator is a range operator, FALSE otherwise. |
||
| 347 | */ |
||
| 348 | protected function isRangeOperator($operator) { |
||
| 352 | |||
| 353 | } |
||
| 354 |