From eec81bdd58a40a8f523aa23bd1221dfaa8b92cbb Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Thu, 20 Aug 2020 21:36:37 +0200 Subject: [PATCH 1/2] Make it possible for scalar annotation to register Doctrine and PHP types to map --- src/Annotation/Scalar.php | 10 +++++++ src/Config/Parser/AnnotationParser.php | 13 ++++++++- tests/Config/Parser/AnnotationParserTest.php | 10 +++++++ .../Scalar/ScalarWithTypeMapping.php | 14 ++++++++++ .../annotations/Type/CustomScalarType.php | 27 +++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php create mode 100644 tests/Config/Parser/fixtures/annotations/Type/CustomScalarType.php diff --git a/src/Annotation/Scalar.php b/src/Annotation/Scalar.php index 89d697a13..363861c29 100644 --- a/src/Annotation/Scalar.php +++ b/src/Annotation/Scalar.php @@ -22,4 +22,14 @@ final class Scalar implements Annotation * @var string */ public string $scalarType; + + /** + * @var string + */ + public string $doctrineType; + + /** + * @var string + */ + public string $phpType; } diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 8ca6a6901..868bd8e6e 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -93,6 +93,7 @@ public static function reset(): void self::$classesMap = []; self::$providers = []; self::$graphClassCache = []; + self::$doctrineMapping = []; } /** @@ -102,7 +103,7 @@ public static function reset(): void */ private static function processFile(SplFileInfo $file, ContainerBuilder $container, array $configs, bool $preProcess): array { - self::$doctrineMapping = $configs['doctrine']['types_mapping']; + self::$doctrineMapping += $configs['doctrine']['types_mapping']; $container->addResource(new FileResource($file->getRealPath())); try { @@ -189,6 +190,16 @@ private static function classAnnotationsToGQLConfiguration( $gqlType = self::GQL_SCALAR; if (!$preProcess) { $gqlConfiguration = self::scalarAnnotationToGQLConfiguration($graphClass, $classAnnotation); + } else { + if (isset($classAnnotation->doctrineType)) { + $gqlName = $classAnnotation->name ?? $graphClass->getShortName(); + self::$doctrineMapping[$classAnnotation->doctrineType] = $gqlName; + } + if (isset($classAnnotation->phpType)) { + self::$classesMap[$gqlName] = ['type' => $gqlType, 'class' => $classAnnotation->phpType]; + + return $gqlTypes; + } } break; diff --git a/tests/Config/Parser/AnnotationParserTest.php b/tests/Config/Parser/AnnotationParserTest.php index 3c391ecbe..5be17b302 100644 --- a/tests/Config/Parser/AnnotationParserTest.php +++ b/tests/Config/Parser/AnnotationParserTest.php @@ -426,6 +426,16 @@ public function testRelayConnectionEdge(): void ]); } + public function testScalarRegisterType(): void + { + $this->expect('CustomScalarType', 'object', [ + 'fields' => [ + 'doctrineDatetime' => ['type' => 'ScalarWithTypeMapping'], + 'phpDatetime' => ['type' => 'ScalarWithTypeMapping!'], + ], + ]); + } + public function testInvalidParamGuessing(): void { try { diff --git a/tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php b/tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php new file mode 100644 index 000000000..0f220051c --- /dev/null +++ b/tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php @@ -0,0 +1,14 @@ + Date: Fri, 13 Nov 2020 21:44:13 +0100 Subject: [PATCH 2/2] Combine phpstan line with docblock Co-authored-by: Timur Murtukov --- .../Parser/fixtures/annotations/Type/CustomScalarType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Config/Parser/fixtures/annotations/Type/CustomScalarType.php b/tests/Config/Parser/fixtures/annotations/Type/CustomScalarType.php index c4825d60a..49c1913d2 100644 --- a/tests/Config/Parser/fixtures/annotations/Type/CustomScalarType.php +++ b/tests/Config/Parser/fixtures/annotations/Type/CustomScalarType.php @@ -16,8 +16,8 @@ class CustomScalarType /** * @ORM\Column(type="datetime", nullable=true) * @GQL\Field + * @phpstan-ignore-next-line */ - // @phpstan-ignore-next-line protected $doctrineDatetime; /**