From f15fcfdeacbc99b6329dd11e891dc3986d190688 Mon Sep 17 00:00:00 2001 From: Vincent QUATREVIEUX Date: Thu, 4 Jul 2024 16:10:48 +0200 Subject: [PATCH 1/2] fix: ignore union type on annotations driver (#FRAM-171) --- .php-cs-fixer.dist.php | 1 + phpunit.xml.dist | 2 +- src/Metadata/Driver/AnnotationsDriver.php | 5 +-- tests/TestPhp80/Fixtures/with_union_type.php | 18 +++++++++ .../Metadata/Driver/AnnotationsDriverTest.php | 37 +++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/TestPhp80/Fixtures/with_union_type.php create mode 100644 tests/TestPhp80/Metadata/Driver/AnnotationsDriverTest.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 588f4e3..bd4104b 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -11,6 +11,7 @@ //'@Symfony' => true, 'array_syntax' => ['syntax' => 'short'], 'declare_strict_types' => false, + 'constant_case' => false, // Do not handle property NormalizationContext::NULL ]) ->setFinder($finder) ->setCacheFile('.php-cs-fixer.cache') // forward compatibility with 3.x line diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5c3d72d..2b714b5 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,7 +21,7 @@ tests/Test ./tests/TestPhp72 - ./tests/TestPhp74 + ./tests/TestPhp80 diff --git a/src/Metadata/Driver/AnnotationsDriver.php b/src/Metadata/Driver/AnnotationsDriver.php index f21c7a6..3b040be 100644 --- a/src/Metadata/Driver/AnnotationsDriver.php +++ b/src/Metadata/Driver/AnnotationsDriver.php @@ -150,9 +150,8 @@ private function getPropertyAnnotations(ReflectionProperty $property): array } // Adding php type if no precision has been added with annotation - if (PHP_VERSION_ID >= 70400 && $property->hasType() && !isset($annotations['type'])) { - /** @psalm-suppress UndefinedMethod */ - $annotations['type'] = $this->findType($property->getType()->getName(), $property); + if (PHP_VERSION_ID >= 70400 && ($type = $property->getType()) && $type instanceof \ReflectionNamedType && !isset($annotations['type'])) { + $annotations['type'] = $this->findType($type->getName(), $property); } return $annotations; diff --git a/tests/TestPhp80/Fixtures/with_union_type.php b/tests/TestPhp80/Fixtures/with_union_type.php new file mode 100644 index 0000000..6b3337a --- /dev/null +++ b/tests/TestPhp80/Fixtures/with_union_type.php @@ -0,0 +1,18 @@ +id = $id; + } + + public function id(): int|string + { + return $this->id; + } +} diff --git a/tests/TestPhp80/Metadata/Driver/AnnotationsDriverTest.php b/tests/TestPhp80/Metadata/Driver/AnnotationsDriverTest.php new file mode 100644 index 0000000..f7403a9 --- /dev/null +++ b/tests/TestPhp80/Metadata/Driver/AnnotationsDriverTest.php @@ -0,0 +1,37 @@ +getMetadataForClass($reflection); + + $this->assertInstanceOf(ClassMetadata::class, $metadata); + $this->assertEquals(WithUnionType::class, $metadata->name()); + + $this->assertEquals(Type::MIXED, $metadata->property('id')->type()->name()); + } +} From e3d7e527a02f54d2c1f6852824fcb2169685e712 Mon Sep 17 00:00:00 2001 From: Vincent QUATREVIEUX Date: Thu, 4 Jul 2024 16:18:25 +0200 Subject: [PATCH 2/2] ci: fix psalm error --- src/Metadata/Driver/AnnotationsDriver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Metadata/Driver/AnnotationsDriver.php b/src/Metadata/Driver/AnnotationsDriver.php index 3b040be..6109c45 100644 --- a/src/Metadata/Driver/AnnotationsDriver.php +++ b/src/Metadata/Driver/AnnotationsDriver.php @@ -8,6 +8,7 @@ use phpDocumentor\Reflection\DocBlock; use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlockFactory; +use phpDocumentor\Reflection\DocBlockFactoryInterface; use phpDocumentor\Reflection\Types\ContextFactory; use ReflectionClass; use ReflectionProperty; @@ -22,7 +23,7 @@ class AnnotationsDriver implements DriverInterface { /** - * @var DocBlockFactory + * @var DocBlockFactoryInterface */ private $docBlockFactory;