We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 40 |
| Total Lines | 153 |
| Duplicated Lines | 0 % |
| Coverage | 97.3% |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DoctrineTypeGuesser 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.
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 DoctrineTypeGuesser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DoctrineTypeGuesser extends TypeGuesser |
||
| 24 | { |
||
| 25 | protected ?AnnotationReader $annotationReader = null; |
||
| 26 | protected array $doctrineMapping = []; |
||
| 27 | |||
| 28 | 93 | public function __construct(ClassesTypesMap $map, array $doctrineMapping = []) |
|
| 32 | 93 | } |
|
| 33 | |||
| 34 | 4 | public function getName(): string |
|
| 35 | { |
||
| 36 | 4 | return 'Doctrine annotations '; |
|
| 37 | } |
||
| 38 | |||
| 39 | 51 | public function supports(Reflector $reflector): bool |
|
| 40 | { |
||
| 41 | 51 | return $reflector instanceof ReflectionProperty; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param ReflectionProperty $reflector |
||
| 46 | */ |
||
| 47 | 52 | public function guessType(ReflectionClass $reflectionClass, Reflector $reflector, array $filterGraphQLTypes = []): ?string |
|
| 48 | { |
||
| 49 | 52 | if (!$reflector instanceof ReflectionProperty) { |
|
| 50 | 1 | throw new TypeGuessingException('Doctrine type guesser only apply to properties.'); |
|
| 51 | } |
||
| 52 | /** @var Column|null $columnAnnotation */ |
||
| 53 | 52 | $columnAnnotation = $this->getAnnotation($reflector, Column::class); |
|
| 54 | |||
| 55 | 52 | if (null !== $columnAnnotation) { |
|
| 56 | 51 | $type = $this->resolveTypeFromDoctrineType($columnAnnotation->type ?: 'string'); |
|
| 57 | 51 | $nullable = $columnAnnotation->nullable; |
|
| 58 | 51 | if ($type) { |
|
| 59 | 51 | return $nullable ? $type : sprintf('%s!', $type); |
|
| 60 | } else { |
||
| 61 | 2 | throw new TypeGuessingException(sprintf('Unable to auto-guess GraphQL type from Doctrine type "%s"', $columnAnnotation->type)); |
|
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | 52 | $associationAnnotations = [ |
|
| 66 | OneToMany::class => true, |
||
| 67 | OneToOne::class => false, |
||
| 68 | ManyToMany::class => true, |
||
| 69 | ManyToOne::class => false, |
||
| 70 | ]; |
||
| 71 | |||
| 72 | 52 | foreach ($associationAnnotations as $associationClass => $isMultiple) { |
|
| 73 | /** @var OneToMany|OneToOne|ManyToMany|ManyToOne|null $associationAnnotation */ |
||
| 74 | 52 | $associationAnnotation = $this->getAnnotation($reflector, $associationClass); |
|
| 75 | 52 | if (null !== $associationAnnotation) { |
|
| 76 | 51 | $target = $this->fullyQualifiedClassName($associationAnnotation->targetEntity, $reflectionClass->getNamespaceName()); |
|
|
|
|||
| 77 | 51 | $type = $this->map->resolveType($target, ['type']); |
|
| 78 | |||
| 79 | 51 | if ($type) { |
|
| 80 | 51 | $isMultiple = $associationAnnotations[get_class($associationAnnotation)]; |
|
| 81 | 51 | if ($isMultiple) { |
|
| 82 | 51 | return sprintf('[%s]!', $type); |
|
| 83 | } else { |
||
| 84 | 51 | $isNullable = false; |
|
| 85 | /** @var JoinColumn|null $joinColumn */ |
||
| 86 | 51 | $joinColumn = $this->getAnnotation($reflector, JoinColumn::class); |
|
| 87 | 51 | if (null !== $joinColumn) { |
|
| 88 | 51 | $isNullable = $joinColumn->nullable; |
|
| 89 | } |
||
| 90 | |||
| 91 | 51 | return sprintf('%s%s', $type, $isNullable ? '' : '!'); |
|
| 92 | } |
||
| 93 | } else { |
||
| 94 | 2 | throw new TypeGuessingException(sprintf('Unable to auto-guess GraphQL type from Doctrine target class "%s" (check if the target class is a GraphQL type itself (with a @Metadata\Type metadata).', $target)); |
|
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | 1 | throw new TypeGuessingException(sprintf('No Doctrine ORM annotation found.')); |
|
| 99 | } |
||
| 100 | |||
| 101 | 52 | private function getAnnotation(Reflector $reflector, string $annotationClass): ?MappingAnnotation |
|
| 118 | } |
||
| 119 | |||
| 120 | 52 | private function getAnnotationReader(): AnnotationReader |
|
| 121 | { |
||
| 122 | 52 | if (null === $this->annotationReader) { |
|
| 123 | 52 | if (!class_exists(AnnotationReader::class) || |
|
| 124 | 52 | !class_exists(AnnotationRegistry::class)) { |
|
| 125 | throw new RuntimeException('In order to use graphql annotation/attributes, you need to require doctrine annotations'); |
||
| 126 | } |
||
| 127 | |||
| 128 | 52 | if (class_exists(AnnotationRegistry::class)) { |
|
| 129 | 52 | AnnotationRegistry::registerLoader('class_exists'); |
|
| 130 | } |
||
| 131 | $this->annotationReader = new AnnotationReader(); |
||
| 132 | 52 | } |
|
| 133 | |||
| 134 | return $this->annotationReader; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Resolve a FQN from classname and namespace. |
||
| 139 | * |
||
| 140 | 51 | * @internal |
|
| 141 | */ |
||
| 142 | 51 | public function fullyQualifiedClassName(string $className, string $namespace): string |
|
| 143 | 51 | { |
|
| 144 | if (false === strpos($className, '\\') && $namespace) { |
||
| 145 | return $namespace.'\\'.$className; |
||
| 146 | } |
||
| 147 | |||
| 148 | return $className; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | 51 | * Resolve a GraphQLType from a doctrine type. |
|
| 153 | */ |
||
| 154 | 51 | private function resolveTypeFromDoctrineType(string $doctrineType): ?string |
|
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 |