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 Executor 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 Executor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Executor |
||
45 | { |
||
46 | protected static $UNDEFINED; |
||
47 | |||
48 | /** |
||
49 | * @param \Fubhy\GraphQL\Schema $schema |
||
50 | * @param $root |
||
51 | * @param \Fubhy\GraphQL\Language\Node\Document $ast |
||
52 | * @param string|null $operation |
||
53 | * @param array|null $args |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | 300 | public static function execute(Schema $schema, $root, Document $ast, $operation = NULL, array $args = NULL) |
|
78 | |||
79 | /** |
||
80 | * Constructs a ExecutionContext object from the arguments passed to |
||
81 | * execute, which we will pass throughout the other execution methods. |
||
82 | * |
||
83 | * @param Schema $schema |
||
84 | * @param $root |
||
85 | * @param Document $ast |
||
86 | * @param string|null $operationName |
||
87 | * @param array $args |
||
88 | * @param $errors |
||
89 | * |
||
90 | * @return ExecutionContext |
||
91 | * |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | 300 | protected static function buildExecutionContext(Schema $schema, $root, Document $ast, $operationName = NULL, array $args = NULL, &$errors) |
|
126 | |||
127 | /** |
||
128 | * Implements the "Evaluating operations" section of the spec. |
||
129 | */ |
||
130 | 279 | protected static function executeOperation(ExecutionContext $context, $root, OperationDefinition $operation) |
|
140 | |||
141 | |||
142 | /** |
||
143 | * Extracts the root type of the operation from the schema. |
||
144 | * |
||
145 | * @param Schema $schema |
||
146 | * @param OperationDefinition $operation |
||
147 | * |
||
148 | * @return ObjectType |
||
149 | * |
||
150 | * @throws \Exception |
||
151 | */ |
||
152 | 279 | protected static function getOperationRootType(Schema $schema, OperationDefinition $operation) |
|
168 | |||
169 | /** |
||
170 | * Implements the "Evaluating selection sets" section of the spec for "write" mode. |
||
171 | */ |
||
172 | 279 | protected static function executeFieldsSerially(ExecutionContext $context, ObjectType $parent, $source, $fields) |
|
186 | |||
187 | /** |
||
188 | * Implements the "Evaluating selection sets" section of the spec for "read" mode. |
||
189 | * |
||
190 | * @param ExecutionContext $context |
||
191 | * @param ObjectType $parent |
||
192 | * @param $source |
||
193 | * @param $fields |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | 273 | protected static function executeFields(ExecutionContext $context, ObjectType $parent, $source, $fields) |
|
201 | |||
202 | /** |
||
203 | * Given a selectionSet, adds all of the fields in that selection to |
||
204 | * the passed in map of fields, and returns it at the end. |
||
205 | * |
||
206 | * @param ExecutionContext $context |
||
207 | * @param ObjectType $type |
||
208 | * @param SelectionSet $set |
||
209 | * @param $fields |
||
210 | * @param $visited |
||
211 | * |
||
212 | * @return \ArrayObject |
||
213 | */ |
||
214 | 279 | protected static function collectFields(ExecutionContext $context, ObjectType $type, SelectionSet $set, $fields, $visited) |
|
268 | |||
269 | /** |
||
270 | * Determines if a field should be included based on @if and @unless directives. |
||
271 | */ |
||
272 | 279 | protected static function shouldIncludeNode(ExecutionContext $exeContext, $directives) |
|
291 | |||
292 | /** |
||
293 | * Determines if a fragment is applicable to the given type. |
||
294 | * |
||
295 | * @param ExecutionContext $context |
||
296 | * @param $fragment |
||
297 | * @param ObjectType $type |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | 39 | protected static function doesFragmentConditionMatch(ExecutionContext $context, $fragment, ObjectType $type) |
|
319 | |||
320 | /** |
||
321 | * Implements the logic to compute the key of a given field's entry |
||
322 | * |
||
323 | * @param Field $node |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 279 | protected static function getFieldEntryKey(Field $node) |
|
331 | |||
332 | /** |
||
333 | * A wrapper function for resolving the field, that catches the error |
||
334 | * and adds it to the context's global if the error is not rethrowable. |
||
335 | * |
||
336 | * @param ExecutionContext $context |
||
337 | * @param ObjectType $parent |
||
338 | * @param $source |
||
339 | * @param $asts |
||
340 | * |
||
341 | * @return array|mixed|null|string |
||
342 | * |
||
343 | * @throws \Exception |
||
344 | */ |
||
345 | 279 | protected static function resolveField(ExecutionContext $context, ObjectType $parent, $source, $asts) |
|
368 | |||
369 | /** |
||
370 | * Resolves the field on the given source object. |
||
371 | * |
||
372 | * In particular, this figures out the object that the field returns using |
||
373 | * the resolve function, then calls completeField to coerce scalars or |
||
374 | * execute the sub selection set for objects. |
||
375 | * |
||
376 | * @param ExecutionContext $context |
||
377 | * @param ObjectType $parent |
||
378 | * @param $source |
||
379 | * @param $asts |
||
380 | * @param FieldDefinition $definition |
||
381 | * |
||
382 | * @return array|mixed|null|string |
||
383 | * |
||
384 | * @throws \Exception |
||
385 | */ |
||
386 | 276 | protected static function resolveFieldOrError(ExecutionContext $context, ObjectType $parent, $source, $asts, FieldDefinition $definition) |
|
403 | |||
404 | /** |
||
405 | * Implements the instructions for completeValue as defined in the |
||
406 | * "Field entries" section of the spec. |
||
407 | * |
||
408 | * If the field type is Non-Null, then this recursively completes the value |
||
409 | * for the inner type. It throws a field error if that completion returns null, |
||
410 | * as per the "Nullability" section of the spec. |
||
411 | * |
||
412 | * If the field type is a List, then this recursively completes the value |
||
413 | * for the inner type on each item in the list. |
||
414 | * |
||
415 | * If the field type is a Scalar or Enum, ensures the completed value is a legal |
||
416 | * value of the type by calling the `coerce` method of GraphQL type definition. |
||
417 | * |
||
418 | * Otherwise, the field type expects a sub-selection set, and will complete the |
||
419 | * value by evaluating all sub-selections. |
||
420 | * |
||
421 | * @param ExecutionContext $context |
||
422 | * @param TypeInterface $type |
||
423 | * @param $asts |
||
424 | * @param $result |
||
425 | * |
||
426 | * @return array|mixed|null|string |
||
427 | * |
||
428 | 270 | * @throws \Exception |
|
429 | */ |
||
430 | protected static function completeField(ExecutionContext $context, TypeInterface $type, $asts, &$result) |
||
497 | |||
498 | /** |
||
499 | * If a resolve function is not given, then a default resolve behavior is used |
||
500 | * which takes the property of the source object of the same name as the field |
||
501 | * and returns it as the result, or if it's a function, returns the result |
||
502 | * of calling that function. |
||
503 | * |
||
504 | * @param $source |
||
505 | * @param $args |
||
506 | * @param $root |
||
507 | * @param $ast |
||
508 | * |
||
509 | 147 | * @return mixed|null |
|
510 | */ |
||
511 | 147 | public static function defaultResolveFn($source, $args, $root, $ast) |
|
527 | |||
528 | /** |
||
529 | * This method looks up the field on the given type defintion. |
||
530 | * It has special casing for the two introspection fields, __schema |
||
531 | * and __typename. __typename is special because it can always be |
||
532 | * queried as a field, even in situations where no other fields |
||
533 | * are allowed, like on a Union. __schema could get automatically |
||
534 | * added to the query type, but that would require mutating type |
||
535 | * definitions, which would cause issues. |
||
536 | * |
||
537 | * @param Schema $schema |
||
538 | * @param ObjectType $parent |
||
539 | * @param Field $ast |
||
540 | * |
||
541 | 279 | * @return FieldDefinition |
|
542 | */ |
||
543 | 279 | protected static function getFieldDefinition(Schema $schema, ObjectType $parent, Field $ast) |
|
561 | } |
||
562 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: