We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Complex classes like AnnotationParser 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 AnnotationParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class AnnotationParser implements PreParserInterface |
||
15 | { |
||
16 | public const CLASSESMAP_CONTAINER_PARAMETER = 'overblog_graphql_types.classes_map'; |
||
17 | |||
18 | private static $annotationReader = null; |
||
19 | private static $classesMap = []; |
||
20 | private static $providers = []; |
||
21 | private static $doctrineMapping = []; |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | * |
||
26 | * @throws \ReflectionException |
||
27 | * @throws InvalidArgumentException |
||
28 | */ |
||
29 | 9 | public static function preParse(\SplFileInfo $file, ContainerBuilder $container, array $configs = []): void |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | * |
||
37 | * @throws \ReflectionException |
||
38 | * @throws InvalidArgumentException |
||
39 | */ |
||
40 | 9 | public static function parse(\SplFileInfo $file, ContainerBuilder $container, array $configs = []): array |
|
44 | |||
45 | /** |
||
46 | * Clear the Annotation parser. |
||
47 | */ |
||
48 | 9 | public static function clear(): void |
|
54 | |||
55 | /** |
||
56 | * Process a file. |
||
57 | * |
||
58 | * @param \SplFileInfo $file |
||
59 | * @param ContainerBuilder $container |
||
60 | * @param bool $resolveClassMap |
||
61 | * |
||
62 | * @throws \ReflectionException |
||
63 | * @throws InvalidArgumentException |
||
64 | */ |
||
65 | 9 | public static function proccessFile(\SplFileInfo $file, ContainerBuilder $container, array $configs, bool $resolveClassMap = false): array |
|
66 | { |
||
67 | 9 | self::$doctrineMapping = $configs['doctrine']['types_mapping']; |
|
68 | |||
69 | 9 | $rootQueryType = $configs['definitions']['schema']['default']['query'] ?? false; |
|
70 | 9 | $rootMutationType = $configs['definitions']['schema']['default']['mutation'] ?? false; |
|
71 | |||
72 | 9 | $container->addResource(new FileResource($file->getRealPath())); |
|
73 | |||
74 | 9 | if (!$resolveClassMap) { |
|
75 | 9 | $container->setParameter(self::CLASSESMAP_CONTAINER_PARAMETER, self::$classesMap); |
|
76 | } |
||
77 | |||
78 | try { |
||
79 | 9 | $fileContent = \file_get_contents($file->getRealPath()); |
|
80 | |||
81 | 9 | $shortClassName = \substr($file->getFilename(), 0, -4); |
|
82 | 9 | if (\preg_match('#namespace (.+);#', $fileContent, $namespace)) { |
|
83 | 9 | $className = $namespace[1].'\\'.$shortClassName; |
|
84 | 9 | $namespace = $namespace[1]; |
|
85 | } else { |
||
86 | $className = $shortClassName; |
||
87 | } |
||
88 | |||
89 | 9 | $reflexionEntity = new \ReflectionClass($className); |
|
90 | |||
91 | 9 | $classAnnotations = self::getAnnotationReader()->getClassAnnotations($reflexionEntity); |
|
92 | |||
93 | 9 | $properties = []; |
|
94 | 9 | foreach ($reflexionEntity->getProperties() as $property) { |
|
95 | 9 | $properties[$property->getName()] = ['property' => $property, 'annotations' => self::getAnnotationReader()->getPropertyAnnotations($property)]; |
|
96 | } |
||
97 | |||
98 | 9 | $methods = []; |
|
99 | 9 | foreach ($reflexionEntity->getMethods() as $method) { |
|
100 | 9 | $methods[$method->getName()] = ['method' => $method, 'annotations' => self::getAnnotationReader()->getMethodAnnotations($method)]; |
|
|
|||
101 | } |
||
102 | |||
103 | 9 | $gqlTypes = []; |
|
104 | |||
105 | 9 | foreach ($classAnnotations as $classAnnotation) { |
|
106 | 9 | $gqlConfiguration = $gqlType = $gqlName = false; |
|
107 | |||
108 | switch (true) { |
||
109 | 9 | case $classAnnotation instanceof GQL\Type: |
|
110 | 9 | $gqlType = 'type'; |
|
111 | 9 | $gqlName = $classAnnotation->name ?: $shortClassName; |
|
112 | 9 | if (!$resolveClassMap) { |
|
113 | 9 | $isRootQuery = ($rootQueryType && $gqlName === $rootQueryType); |
|
114 | 9 | $isRootMutation = ($rootMutationType && $gqlName === $rootMutationType); |
|
115 | 9 | $currentValue = ($isRootQuery || $isRootMutation) ? \sprintf("service('%s')", self::formatNamespaceForExpression($className)) : 'value'; |
|
116 | |||
117 | 9 | $gqlConfiguration = self::getGraphqlType($classAnnotation, $classAnnotations, $properties, $methods, $namespace, $currentValue); |
|
118 | 9 | $providerFields = self::getGraphqlFieldsFromProviders($namespace, $className, $isRootMutation ? 'Mutation' : 'Query', $gqlName, ($isRootQuery || $isRootMutation)); |
|
119 | 9 | $gqlConfiguration['config']['fields'] = $providerFields + $gqlConfiguration['config']['fields']; |
|
120 | } |
||
121 | 9 | break; |
|
122 | 9 | case $classAnnotation instanceof GQL\Input: |
|
123 | 9 | $gqlType = 'input'; |
|
124 | 9 | $gqlName = $classAnnotation->name ?: self::suffixName($shortClassName, 'Input'); |
|
125 | 9 | if (!$resolveClassMap) { |
|
126 | 9 | $gqlConfiguration = self::getGraphqlInput($classAnnotation, $classAnnotations, $properties, $namespace); |
|
127 | } |
||
128 | 9 | break; |
|
129 | 9 | case $classAnnotation instanceof GQL\Scalar: |
|
130 | 9 | $gqlType = 'scalar'; |
|
131 | 9 | if (!$resolveClassMap) { |
|
132 | 9 | $gqlConfiguration = self::getGraphqlScalar($className, $classAnnotation, $classAnnotations); |
|
133 | } |
||
134 | 9 | break; |
|
135 | 9 | case $classAnnotation instanceof GQL\Enum: |
|
136 | 9 | $gqlType = 'enum'; |
|
137 | 9 | if (!$resolveClassMap) { |
|
138 | 9 | $gqlConfiguration = self::getGraphqlEnum($classAnnotation, $classAnnotations, $reflexionEntity->getConstants()); |
|
139 | } |
||
140 | 9 | break; |
|
141 | 9 | case $classAnnotation instanceof GQL\Union: |
|
142 | 9 | $gqlType = 'union'; |
|
143 | 9 | if (!$resolveClassMap) { |
|
144 | 9 | $gqlConfiguration = self::getGraphqlUnion($className, $classAnnotation, $classAnnotations, $methods); |
|
145 | } |
||
146 | 9 | break; |
|
147 | 9 | case $classAnnotation instanceof GQL\TypeInterface: |
|
148 | 9 | $gqlType = 'interface'; |
|
149 | 9 | if (!$resolveClassMap) { |
|
150 | 9 | $gqlConfiguration = self::getGraphqlInterface($classAnnotation, $classAnnotations, $properties, $methods, $namespace); |
|
151 | } |
||
152 | 9 | break; |
|
153 | 9 | case $classAnnotation instanceof GQL\Provider: |
|
154 | 9 | if ($resolveClassMap) { |
|
155 | 9 | self::$providers[$className] = ['annotation' => $classAnnotation, 'methods' => $methods]; |
|
156 | } |
||
157 | 9 | break; |
|
158 | default: |
||
159 | 9 | continue 2; |
|
160 | } |
||
161 | |||
162 | 9 | if ($gqlType) { |
|
163 | 9 | if (!$gqlName) { |
|
164 | 9 | $gqlName = $classAnnotation->name ?: $shortClassName; |
|
165 | } |
||
166 | |||
167 | 9 | if ($resolveClassMap) { |
|
168 | 9 | if (isset(self::$classesMap[$gqlName])) { |
|
169 | 1 | throw new InvalidArgumentException(\sprintf('The GraphQL type "%s" has already been registered in class "%s"', $gqlName, self::$classesMap[$gqlName]['class'])); |
|
170 | } |
||
171 | 9 | self::$classesMap[$gqlName] = ['type' => $gqlType, 'class' => $className]; |
|
172 | } else { |
||
173 | 9 | $gqlTypes = [$gqlName => $gqlConfiguration] + $gqlTypes; |
|
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | 9 | return $resolveClassMap ? self::$classesMap : $gqlTypes; |
|
179 | 1 | } catch (\InvalidArgumentException $e) { |
|
180 | 1 | throw new InvalidArgumentException(\sprintf('Failed to parse GraphQL annotations from file "%s".', $file), $e->getCode(), $e); |
|
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Retrieve annotation reader. |
||
186 | * |
||
187 | * @return AnnotationReader |
||
188 | */ |
||
189 | 9 | private static function getAnnotationReader() |
|
203 | |||
204 | /** |
||
205 | * Create a GraphQL Type configuration from annotations on class, properties and methods. |
||
206 | * |
||
207 | * @param GQL\Type $typeAnnotation |
||
208 | * @param array $classAnnotations |
||
209 | * @param array $properties |
||
210 | * @param array $methods |
||
211 | * @param string $namespace |
||
212 | * @param string $currentValue |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | 9 | private static function getGraphqlType(GQL\Type $typeAnnotation, array $classAnnotations, array $properties, array $methods, string $namespace, string $currentValue) |
|
246 | |||
247 | /** |
||
248 | * Create a GraphQL Interface type configuration from annotations on properties. |
||
249 | * |
||
250 | * @param string $shortClassName |
||
251 | * @param GQL\Interface $interfaceAnnotation |
||
252 | * @param array $properties |
||
253 | * @param array $methods |
||
254 | * @param string $namespace |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 9 | private static function getGraphqlInterface(GQL\TypeInterface $interfaceAnnotation, array $classAnnotations, array $properties, array $methods, string $namespace) |
|
272 | |||
273 | /** |
||
274 | * Create a GraphQL Input type configuration from annotations on properties. |
||
275 | * |
||
276 | * @param string $shortClassName |
||
277 | * @param GQL\Input $inputAnnotation |
||
278 | * @param array $properties |
||
279 | * @param string $namespace |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | 9 | private static function getGraphqlInput(GQL\Input $inputAnnotation, array $classAnnotations, array $properties, string $namespace) |
|
293 | |||
294 | /** |
||
295 | * Get a Graphql scalar configuration from given scalar annotation. |
||
296 | * |
||
297 | * @param string $shortClassName |
||
298 | * @param string $className |
||
299 | * @param GQL\Scalar $scalarAnnotation |
||
300 | * @param array $classAnnotations |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | 9 | private static function getGraphqlScalar(string $className, GQL\Scalar $scalarAnnotation, array $classAnnotations) |
|
322 | |||
323 | /** |
||
324 | * Get a Graphql Enum configuration from given enum annotation. |
||
325 | * |
||
326 | * @param string $shortClassName |
||
327 | * @param GQL\Enum $enumAnnotation |
||
328 | * @param array $classAnnotations |
||
329 | * @param array $constants |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | 9 | private static function getGraphqlEnum(GQL\Enum $enumAnnotation, array $classAnnotations, array $constants) |
|
362 | |||
363 | /** |
||
364 | * Get a Graphql Union configuration from given union annotation. |
||
365 | * |
||
366 | * @param string $className |
||
367 | * @param GQL\Union $unionAnnotation |
||
368 | * @param array $classAnnotations |
||
369 | * @param array $methods |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | 9 | private static function getGraphqlUnion(string $className, GQL\Union $unionAnnotation, array $classAnnotations, array $methods): array |
|
395 | |||
396 | /** |
||
397 | * Create Graphql fields configuration based on annotations. |
||
398 | * |
||
399 | * @param string $namespace |
||
400 | * @param array $propertiesOrMethods |
||
401 | * @param bool $isInput |
||
402 | * @param bool $isMethod |
||
403 | * @param string $currentValue |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | 9 | private static function getGraphqlFieldsFromAnnotations(string $namespace, array $propertiesOrMethods, bool $isInput = false, bool $isMethod = false, string $currentValue = 'value', string $fieldAnnotationName = 'Field'): array |
|
534 | |||
535 | /** |
||
536 | * Return fields config from Provider methods. |
||
537 | * |
||
538 | * @param string $className |
||
539 | * @param array $methods |
||
540 | * @param bool $isMutation |
||
541 | * |
||
542 | * @return array |
||
543 | */ |
||
544 | 9 | private static function getGraphqlFieldsFromProviders(string $namespace, string $className, string $annotationName, string $targetType, bool $isRoot = false) |
|
584 | |||
585 | /** |
||
586 | * Get the config for description & deprecation reason. |
||
587 | * |
||
588 | * @param array $annotations |
||
589 | * @param bool $withDeprecation |
||
590 | * |
||
591 | * @return array |
||
592 | */ |
||
593 | 9 | private static function getDescriptionConfiguration(array $annotations, bool $withDeprecation = false) |
|
610 | |||
611 | /** |
||
612 | * Get args config from an array of @Arg annotation or by auto-guessing if a method is provided. |
||
613 | * |
||
614 | * @param array $args |
||
615 | * @param \ReflectionMethod $method |
||
616 | * |
||
617 | * @return array |
||
618 | */ |
||
619 | 9 | private static function getArgs(array $args = null, \ReflectionMethod $method = null) |
|
632 | |||
633 | 9 | private static function formatArgsForExpression(array $args) |
|
642 | |||
643 | /** |
||
644 | * Format an array of args to a list of arguments in an expression. |
||
645 | * |
||
646 | * @param array $args |
||
647 | * |
||
648 | * @return string |
||
649 | */ |
||
650 | /* |
||
651 | private static function formatArgsForExpression(array $args) |
||
652 | { |
||
653 | $resolvedArgs = []; |
||
654 | foreach ($args as $name => $config) { |
||
655 | $cleanedType = \str_replace(['[', ']', '!'], '', $config['type']); |
||
656 | $definition = self::resolveClassFromType($cleanedType); |
||
657 | $defaultFormat = \sprintf("args['%s']", $name); |
||
658 | if (!$definition) { |
||
659 | $resolvedArgs[] = $defaultFormat; |
||
660 | } else { |
||
661 | switch ($definition['type']) { |
||
662 | case 'input': |
||
663 | case 'enum': |
||
664 | $resolvedArgs[] = \sprintf("input('%s', args['%s'], '%s')", $config['type'], $name, $name); |
||
665 | break; |
||
666 | default: |
||
667 | $resolvedArgs[] = $defaultFormat; |
||
668 | break; |
||
669 | } |
||
670 | } |
||
671 | } |
||
672 | |||
673 | return sprintf("inputs(%s)", \implode(', ', $resolvedArgs)); |
||
674 | } |
||
675 | */ |
||
676 | |||
677 | /** |
||
678 | * Format a namespace to be used in an expression (double escape). |
||
679 | * |
||
680 | * @param string $namespace |
||
681 | * |
||
682 | * @return string |
||
683 | */ |
||
684 | 9 | private static function formatNamespaceForExpression(string $namespace) |
|
688 | |||
689 | /** |
||
690 | * Get the first annotation matching given class. |
||
691 | * |
||
692 | * @param array $annotations |
||
693 | * @param string|array $annotationClass |
||
694 | * |
||
695 | * @return mixed |
||
696 | */ |
||
697 | 9 | private static function getFirstAnnotationMatching(array $annotations, $annotationClass) |
|
713 | |||
714 | /** |
||
715 | * Format an expression (ie. add "@=" if not set). |
||
716 | * |
||
717 | * @param string $expression |
||
718 | * |
||
719 | * @return string |
||
720 | */ |
||
721 | 9 | private static function formatExpression(string $expression) |
|
725 | |||
726 | /** |
||
727 | * Suffix a name if it is not already. |
||
728 | * |
||
729 | * @param string $name |
||
730 | * @param string $suffix |
||
731 | * |
||
732 | * @return string |
||
733 | */ |
||
734 | 9 | private static function suffixName(string $name, string $suffix) |
|
738 | |||
739 | /** |
||
740 | * Try to guess a field type base on is annotations. |
||
741 | * |
||
742 | * @param string $namespace |
||
743 | * @param array $annotations |
||
744 | * |
||
745 | * @return string|false |
||
746 | */ |
||
747 | 9 | private static function guessType(string $namespace, array $annotations) |
|
792 | |||
793 | /** |
||
794 | * Resolve a FQN from classname and namespace. |
||
795 | * |
||
796 | * @param string $className |
||
797 | * @param string $namespace |
||
798 | * |
||
799 | * @return string |
||
800 | */ |
||
801 | 9 | public static function fullyQualifiedClassName(string $className, string $namespace) |
|
809 | |||
810 | /** |
||
811 | * Resolve a GraphqlType from a doctrine type. |
||
812 | * |
||
813 | * @param string $doctrineType |
||
814 | * |
||
815 | * @return string|false |
||
816 | */ |
||
817 | 9 | private static function resolveTypeFromDoctrineType(string $doctrineType) |
|
845 | |||
846 | /** |
||
847 | * Transform a method arguments from reflection to a list of GraphQL argument. |
||
848 | * |
||
849 | * @param \ReflectionMethod $method |
||
850 | * |
||
851 | * @return array |
||
852 | */ |
||
853 | 9 | private static function guessArgs(\ReflectionMethod $method) |
|
879 | |||
880 | /** |
||
881 | * Try to guess a GraphQL type from a Reflected Type. |
||
882 | * |
||
883 | * @param \ReflectionType $type |
||
884 | * |
||
885 | * @return string |
||
886 | */ |
||
887 | 9 | private static function resolveGraphqlTypeFromReflectionType(\ReflectionType $type, string $filterGraphqlType = null, bool $isOptionnal = false) |
|
904 | |||
905 | /** |
||
906 | * Resolve a GraphQL Type from a class name. |
||
907 | * |
||
908 | * @param string $className |
||
909 | * @param string $wantedType |
||
910 | * |
||
911 | * @return string|false |
||
912 | */ |
||
913 | 9 | private static function resolveTypeFromClass(string $className, string $wantedType = null) |
|
925 | |||
926 | /** |
||
927 | * Resolve a PHP class from a GraphQL type. |
||
928 | * |
||
929 | * @param string $type |
||
930 | * |
||
931 | * @return string|false |
||
932 | */ |
||
933 | 9 | private static function resolveClassFromType(string $type) |
|
937 | |||
938 | /** |
||
939 | * Convert a PHP Builtin type to a GraphQL type. |
||
940 | * |
||
941 | * @param string $phpType |
||
942 | * |
||
943 | * @return string |
||
944 | */ |
||
945 | 9 | private static function resolveTypeFromPhpType(string $phpType) |
|
963 | } |
||
964 |