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 Introspection 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 Introspection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Introspection |
||
24 | { |
||
25 | const TYPEKIND_SCALAR = 0; |
||
26 | const TYPEKIND_OBJECT = 1; |
||
27 | const TYPEKIND_INTERFACE = 2; |
||
28 | const TYPEKIND_UNION = 3; |
||
29 | const TYPEKIND_ENUM = 4; |
||
30 | const TYPEKIND_INPUT_OBJECT = 5; |
||
31 | const TYPEKIND_LIST = 6; |
||
32 | const TYPEKIND_NON_NULL = 7; |
||
33 | |||
34 | /** |
||
35 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
36 | */ |
||
37 | protected static $schema; |
||
38 | |||
39 | /** |
||
40 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
41 | */ |
||
42 | protected static $directive; |
||
43 | |||
44 | /** |
||
45 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
46 | */ |
||
47 | protected static $type; |
||
48 | |||
49 | /** |
||
50 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
51 | */ |
||
52 | protected static $field; |
||
53 | |||
54 | /** |
||
55 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
56 | */ |
||
57 | protected static $inputValue; |
||
58 | |||
59 | /** |
||
60 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
61 | */ |
||
62 | protected static $typeKind; |
||
63 | |||
64 | /** |
||
65 | * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
66 | */ |
||
67 | protected static $enumValue; |
||
68 | |||
69 | /** |
||
70 | * @var \Fubhy\GraphQL\Type\Definition\FieldDefinition |
||
71 | */ |
||
72 | protected static $typeNameMetaFieldDefinition; |
||
73 | |||
74 | /** |
||
75 | * @var \Fubhy\GraphQL\Type\Definition\FieldDefinition |
||
76 | */ |
||
77 | protected static $schemaMetaFieldDefinition; |
||
78 | |||
79 | /** |
||
80 | * @var \Fubhy\GraphQL\Type\Definition\FieldDefinition |
||
81 | */ |
||
82 | protected static $typeMetaFieldDefinition; |
||
83 | |||
84 | /** |
||
85 | * @param $source |
||
86 | * @return array|null |
||
87 | 159 | */ |
|
88 | public static function resolveSchemaTypes($source) { |
||
95 | 9 | ||
96 | 9 | /** |
|
97 | * @param $source |
||
98 | 3 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType|null |
|
99 | 3 | */ |
|
100 | public static function resolveSchemaQueryType($source) { |
||
107 | 3 | ||
108 | 3 | /** |
|
109 | * @param $source |
||
110 | 3 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType|null |
|
111 | 3 | */ |
|
112 | public static function resolveSchemaMutationType($source) { |
||
119 | 3 | ||
120 | 3 | /** |
|
121 | * @param $source |
||
122 | 3 | * @return \Fubhy\GraphQL\Type\Directives\DirectiveInterface[]|null |
|
123 | 3 | */ |
|
124 | public static function resolveSchemaDirectives($source) { |
||
131 | |||
132 | /** |
||
133 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
134 | */ |
||
135 | public static function schema() |
||
164 | |||
165 | 3 | /** |
|
166 | * @param $source |
||
167 | 3 | * @return string |
|
168 | 3 | */ |
|
169 | public static function resolveDirectiveName($source) { |
||
176 | 3 | ||
177 | |||
178 | /** |
||
179 | 3 | * @param $source |
|
180 | * @return string |
||
181 | 3 | */ |
|
182 | public static function resolveDirectiveDescription($source) { |
||
189 | 3 | ||
190 | /** |
||
191 | 153 | * @param $source |
|
192 | * @return string |
||
193 | */ |
||
194 | public static function resolveDirectiveArguments($source) { |
||
201 | |||
202 | 3 | /** |
|
203 | * @param $source |
||
204 | 24 | * @return bool|null |
|
205 | 15 | */ |
|
206 | public static function resolveDirectiveOnOperation($source) { |
||
213 | 12 | ||
214 | /** |
||
215 | * @param $source |
||
216 | 21 | * @return bool|null |
|
217 | 3 | */ |
|
218 | public static function resolveDirectiveOnFragment($source) { |
||
225 | 3 | ||
226 | /** |
||
227 | * @param $source |
||
228 | 15 | * @return bool|null |
|
229 | 12 | */ |
|
230 | public static function resolveDirectiveOnField($source) { |
||
237 | 3 | ||
238 | 3 | /** |
|
239 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
240 | 3 | */ |
|
241 | public static function directive() |
||
274 | |||
275 | 9 | /** |
|
276 | 3 | * @param $source |
|
277 | * @return int |
||
278 | 3 | */ |
|
279 | public static function resolveTypeKind($source) { |
||
314 | 3 | ||
315 | /** |
||
316 | 3 | * @param $source |
|
317 | * @return null|string |
||
318 | 9 | */ |
|
319 | 3 | public static function resolveTypeName($source) { |
|
326 | 12 | ||
327 | 12 | /** |
|
328 | * @param $source |
||
329 | 15 | * @return null|string |
|
330 | 3 | */ |
|
331 | 3 | public static function resolveTypeDescription($source) { |
|
338 | |||
339 | /** |
||
340 | 156 | * @param $source |
|
341 | * @param array $args |
||
342 | 156 | * @return null|string |
|
343 | 3 | */ |
|
344 | View Code Duplication | public static function resolveTypeFields($source, array $args) { |
|
359 | 3 | ||
360 | /** |
||
361 | 3 | * @param $source |
|
362 | * @return null|string |
||
363 | 6 | */ |
|
364 | 6 | public static function resolveTypeInterfaces($source) { |
|
371 | 9 | ||
372 | 9 | /** |
|
373 | * @param $source |
||
374 | 3 | * @return null|string |
|
375 | 3 | */ |
|
376 | public static function resolveTypePossibleTypes($source) { |
||
383 | 3 | ||
384 | /** |
||
385 | 3 | * @param $source |
|
386 | * @param array $args |
||
387 | 6 | * @return array |
|
388 | 6 | */ |
|
389 | View Code Duplication | public static function resolveTypeEnumValues($source, array $args) { |
|
404 | 3 | ||
405 | /** |
||
406 | 3 | * @param $source |
|
407 | * @return array|null |
||
408 | 9 | */ |
|
409 | 9 | public static function resolveTypeInputFields($source) { |
|
410 | if ($source instanceof InputObjectType) { |
||
411 | return array_values($source->getFields()); |
||
412 | 3 | } |
|
413 | |||
414 | 3 | return NULL; |
|
415 | } |
||
416 | 3 | ||
417 | 3 | /** |
|
418 | * @param $source |
||
419 | * @return \Fubhy\GraphQL\Type\Definition\Types\TypeInterface |
||
420 | 3 | */ |
|
421 | public static function resolveTypeOfType($source) { |
||
426 | |||
427 | /** |
||
428 | 3 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
|
429 | */ |
||
430 | 3 | public static function type() |
|
487 | |||
488 | /** |
||
489 | * @param $source |
||
490 | * @return null|string |
||
491 | */ |
||
492 | 156 | public static function resolveFieldName($source) { |
|
499 | 3 | ||
500 | /** |
||
501 | 3 | * @param $source |
|
502 | 3 | * @return null|string |
|
503 | 3 | */ |
|
504 | public static function resolveFieldDescription($source) { |
||
511 | 3 | ||
512 | /** |
||
513 | 3 | * @param $source |
|
514 | 3 | * @return null|string |
|
515 | 3 | */ |
|
516 | public static function resolveFieldArguments($source) { |
||
523 | 3 | ||
524 | /** |
||
525 | 3 | * @param $source |
|
526 | 3 | * @return null|string |
|
527 | 3 | */ |
|
528 | 3 | public static function resolveFieldType($source) { |
|
535 | |||
536 | /** |
||
537 | 279 | * @param $source |
|
538 | * @return bool|null |
||
539 | 279 | */ |
|
540 | 3 | public static function resolveFieldIsDeprecated($source) { |
|
547 | |||
548 | 3 | /** |
|
549 | 3 | * @param $source |
|
550 | * @return null|string |
||
551 | 279 | */ |
|
552 | public static function resolveFieldDeprecationReason($source) { |
||
559 | 279 | ||
560 | 3 | /** |
|
561 | 3 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
|
562 | 3 | */ |
|
563 | 3 | public static function field() |
|
596 | |||
597 | /** |
||
598 | * @param $source |
||
599 | * @return null|string |
||
600 | */ |
||
601 | public static function resolveInputValueName($source) { |
||
608 | |||
609 | /** |
||
610 | * @param $source |
||
611 | * @return null|string |
||
612 | */ |
||
613 | public static function resolveInputValueDescription($source) { |
||
620 | |||
621 | /** |
||
622 | * @param $source |
||
623 | * @return \Fubhy\GraphQL\Type\Definition\Types\InputTypeInterface |
||
624 | */ |
||
625 | public static function resolveInputValueType($source) { |
||
632 | |||
633 | /** |
||
634 | * @param $source |
||
635 | * @return \Fubhy\GraphQL\Type\Definition\Types\InputTypeInterface |
||
636 | */ |
||
637 | public static function resolveInputValueDefaultValue($source) { |
||
645 | |||
646 | /** |
||
647 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
648 | */ |
||
649 | public static function inputValue() |
||
674 | |||
675 | /** |
||
676 | * @param $source |
||
677 | * @return string |
||
678 | */ |
||
679 | public static function resolveEnumValueName($source) { |
||
686 | |||
687 | /** |
||
688 | * @param $source |
||
689 | * @return string |
||
690 | */ |
||
691 | public static function resolveEnumValueDescription($source) { |
||
698 | |||
699 | /** |
||
700 | * @param $source |
||
701 | * @return string |
||
702 | */ |
||
703 | public static function resolveEnumValueIsDeprecated($source) { |
||
710 | |||
711 | /** |
||
712 | * @param $source |
||
713 | * @return string |
||
714 | */ |
||
715 | public static function resolveEnumValueDeprecationReason($source) { |
||
722 | |||
723 | /** |
||
724 | * @return \Fubhy\GraphQL\Type\Definition\Types\ObjectType |
||
725 | */ |
||
726 | public static function enumValue() |
||
751 | |||
752 | /** |
||
753 | * @return \Fubhy\GraphQL\Type\Definition\Types\EnumType |
||
754 | */ |
||
755 | public static function typeKind() |
||
796 | |||
797 | /** |
||
798 | * @param $a |
||
799 | * @param $b |
||
800 | * @param $c |
||
801 | * @param $d |
||
802 | * @param $e |
||
803 | * @param $f |
||
804 | * @param $schema |
||
805 | * @return mixed |
||
806 | */ |
||
807 | public static function resolveSchemaMetaField($a, $b, $c, $d, $e, $f, $schema) { |
||
810 | |||
811 | /** |
||
812 | * @return \Fubhy\GraphQL\Type\Definition\FieldDefinition |
||
813 | */ |
||
814 | public static function schemaMetaFieldDefinition() |
||
828 | |||
829 | /** |
||
830 | * @param $a |
||
831 | * @param array $args |
||
832 | * @param $b |
||
833 | * @param $c |
||
834 | * @param $d |
||
835 | * @param $e |
||
836 | * @param \Fubhy\GraphQL\Schema $schema |
||
837 | * @return \Fubhy\GraphQL\Type\Definition\Types\TypeInterface |
||
838 | */ |
||
839 | public static function resolveTypeMetaField($a, array $args, $b, $c, $d, $e, Schema $schema) { |
||
842 | |||
843 | /** |
||
844 | * @return \Fubhy\GraphQL\Type\Definition\FieldDefinition |
||
845 | */ |
||
846 | public static function typeMetaFieldDefinition() |
||
862 | |||
863 | /** |
||
864 | * @param $a |
||
865 | * @param $b |
||
866 | * @param $c |
||
867 | * @param $d |
||
868 | * @param $e |
||
869 | * @param \Fubhy\GraphQL\Type\Definition\Types\TypeInterface $parentType |
||
870 | * @return string |
||
871 | */ |
||
872 | public static function resolveTypeNameMetaField($a, $b, $c, $d, $e, TypeInterface $parentType) { |
||
875 | |||
876 | /** |
||
877 | * @return \Fubhy\GraphQL\Type\Definition\FieldDefinition |
||
878 | */ |
||
879 | public static function typeNameMetaFieldDefinition() |
||
893 | } |
||
894 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.