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 |
||
| 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) { |
||
| 174 | $conjunction = !empty($args['conjunction']) ? $args['conjunction'] : 'AND'; |
||
| 175 | $group = $conjunction === 'AND' ? $query->andConditionGroup() : $query->orConditionGroup(); |
||
| 176 | |||
| 177 | // Apply filter conditions. |
||
| 178 | $conditions = !empty($filter['conditions']) ? $filter['conditions'] : []; |
||
| 179 | foreach ($conditions as $condition) { |
||
| 180 | $field = $condition['field']; |
||
| 181 | $value = !empty($condition['value']) ? $condition['value'] : NULL; |
||
| 182 | $operator = !empty($condition['operator']) ? $condition['operator'] : NULL; |
||
| 183 | $language = !empty($condition['language']) ? $condition['language'] : NULL; |
||
| 184 | |||
| 185 | // We need at least a value or an operator. |
||
| 186 | if (empty($operator) && empty($value)) { |
||
| 187 | throw new ResolveException(sprintf("Missing value and operator in filter for '%s'.", $field)); |
||
| 188 | } |
||
| 189 | // Unary operators need a single value. |
||
| 190 | else if (!empty($operator) && $this->isUnaryOperator($operator)) { |
||
| 191 | View Code Duplication | if (empty($value) || count($value) > 1) { |
|
| 192 | throw new ResolveException(sprintf("Unary operators must be associated with a single value (field '%s').", $field)); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Pick the first item from the values. |
||
| 196 | $value = reset($value); |
||
| 197 | } |
||
| 198 | // Range operators need exactly two values. |
||
| 199 | else if (!empty($operator) && $this->isRangeOperator($operator)) { |
||
| 200 | View Code Duplication | if (empty($value) || count($value) !== 2) { |
|
| 201 | throw new ResolveException(sprintf("Range operators must require exactly two values (field '%s').", $field)); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | // Null operators can't have a value set. |
||
| 205 | else if (!empty($operator) && $this->isNullOperator($operator)) { |
||
| 206 | if (!empty($value)) { |
||
| 207 | throw new ResolveException(sprintf("Null operators must not be associated with a filter value (field '%s').", $field)); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | // If no operator is set, however, we default to EQUALS or IN, depending |
||
| 212 | // on whether the given value is an array with one or more than one items. |
||
| 213 | if (empty($operator)) { |
||
| 214 | $value = count($value) === 1 ? reset($value) : $value; |
||
| 215 | $operator = is_array($value) ? 'IN' : '='; |
||
| 216 | } |
||
| 217 | |||
| 218 | // Add the condition for the current field. |
||
| 219 | $group->condition($field, $value, $operator, $language); |
||
| 220 | } |
||
| 221 | |||
| 222 | // Apply nested filter group conditions. |
||
| 223 | $groups = !empty($filter['groups']) ? $filter['groups'] : []; |
||
| 224 | foreach ($groups as $args) { |
||
| 225 | // By default, we use AND condition groups. |
||
| 226 | $group->condition($this->buildFilterConditions($query, $args)); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $group; |
||
| 230 | } |
||
| 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 |