From bb647e85b0c07adc01bd74467e6b75191a70d4fe Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Tue, 13 Jun 2023 11:11:17 -0400 Subject: [PATCH] Improve null checks in drivers with a delegate --- src/Metadata/Driver/AbstractDoctrineTypeDriver.php | 5 +++++ src/Metadata/Driver/DefaultValuePropertyDriver.php | 3 ++- src/Metadata/Driver/DocBlockDriver.php | 3 ++- src/Metadata/Driver/EnumPropertiesDriver.php | 3 ++- src/Metadata/Driver/TypedPropertiesDriver.php | 5 +++++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Metadata/Driver/AbstractDoctrineTypeDriver.php b/src/Metadata/Driver/AbstractDoctrineTypeDriver.php index 23f7c84ef..70320f78e 100644 --- a/src/Metadata/Driver/AbstractDoctrineTypeDriver.php +++ b/src/Metadata/Driver/AbstractDoctrineTypeDriver.php @@ -83,6 +83,11 @@ public function __construct(DriverInterface $delegate, ManagerRegistry $registry public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata { $classMetadata = $this->delegate->loadMetadataForClass($class); + + if (null === $classMetadata) { + return null; + } + \assert($classMetadata instanceof ClassMetadata); // Abort if the given class is not a mapped entity diff --git a/src/Metadata/Driver/DefaultValuePropertyDriver.php b/src/Metadata/Driver/DefaultValuePropertyDriver.php index 60d578ffd..c50503545 100644 --- a/src/Metadata/Driver/DefaultValuePropertyDriver.php +++ b/src/Metadata/Driver/DefaultValuePropertyDriver.php @@ -30,12 +30,13 @@ public function __construct(DriverInterface $delegate) public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata { $classMetadata = $this->delegate->loadMetadataForClass($class); - \assert($classMetadata instanceof SerializerClassMetadata); if (null === $classMetadata) { return null; } + \assert($classMetadata instanceof SerializerClassMetadata); + foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) { \assert($propertyMetadata instanceof PropertyMetadata); if (null !== $propertyMetadata->hasDefault) { diff --git a/src/Metadata/Driver/DocBlockDriver.php b/src/Metadata/Driver/DocBlockDriver.php index 45fda0e31..7c84d8b89 100644 --- a/src/Metadata/Driver/DocBlockDriver.php +++ b/src/Metadata/Driver/DocBlockDriver.php @@ -45,12 +45,13 @@ public function __construct(DriverInterface $delegate, ?ParserInterface $typePar public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata { $classMetadata = $this->delegate->loadMetadataForClass($class); - \assert($classMetadata instanceof SerializerClassMetadata); if (null === $classMetadata) { return null; } + \assert($classMetadata instanceof SerializerClassMetadata); + // We base our scan on the internal driver's property list so that we // respect any internal allow/blocklist like in the AnnotationDriver foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) { diff --git a/src/Metadata/Driver/EnumPropertiesDriver.php b/src/Metadata/Driver/EnumPropertiesDriver.php index f4b4ee7d9..514e3ee8c 100644 --- a/src/Metadata/Driver/EnumPropertiesDriver.php +++ b/src/Metadata/Driver/EnumPropertiesDriver.php @@ -30,12 +30,13 @@ public function __construct(DriverInterface $delegate) public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata { $classMetadata = $this->delegate->loadMetadataForClass($class); - \assert($classMetadata instanceof SerializerClassMetadata); if (null === $classMetadata) { return null; } + \assert($classMetadata instanceof SerializerClassMetadata); + // We base our scan on the internal driver's property list so that we // respect any internal allow/blocklist like in the AnnotationDriver foreach ($classMetadata->propertyMetadata as $propertyMetadata) { diff --git a/src/Metadata/Driver/TypedPropertiesDriver.php b/src/Metadata/Driver/TypedPropertiesDriver.php index c14dc1d75..c3ebbefe4 100644 --- a/src/Metadata/Driver/TypedPropertiesDriver.php +++ b/src/Metadata/Driver/TypedPropertiesDriver.php @@ -67,6 +67,11 @@ private function getDefaultWhiteList(): array public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata { $classMetadata = $this->delegate->loadMetadataForClass($class); + + if (null === $classMetadata) { + return null; + } + \assert($classMetadata instanceof SerializerClassMetadata); if (PHP_VERSION_ID <= 70400) {