From c142c2d7c378546c73a2c612c4e8aa5762d0ec07 Mon Sep 17 00:00:00 2001 From: Arjan Frans Date: Tue, 20 Jul 2021 13:42:08 +0200 Subject: [PATCH 1/2] Allow defining PHP mapping with a static function --- .../Persistence/Mapping/Driver/PHPDriver.php | 7 ++++++- .../Tests/Persistence/Mapping/PHPDriverTest.php | 13 +++++++++++++ ...ests.Persistence.Mapping.PHPStaticTestEntity.php | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPStaticTestEntity.php diff --git a/lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php b/lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php index cc7f0fe0..84e5b5f4 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php @@ -40,7 +40,12 @@ public function loadMetadataForClass($className, ClassMetadata $metadata) protected function loadMappingFile($file) { $metadata = $this->metadata; - include $file; + + $mapping = include $file; + + if (is_callable($mapping)) { + $mapping($metadata); + } return [$metadata->getName() => $metadata]; } diff --git a/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php b/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php index 1b6d730e..b4c55ffc 100644 --- a/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php +++ b/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php @@ -16,8 +16,21 @@ public function testLoadMetadata(): void $driver = new PHPDriver([__DIR__ . '/_files']); $driver->loadMetadataForClass(PHPTestEntity::class, $metadata); } + + public function testLoadMetadataWithFunction(): void + { + $metadata = $this->createMock(ClassMetadata::class); + $metadata->expects($this->once())->method('getFieldNames'); + + $driver = new PHPDriver([__DIR__ . '/_files']); + $driver->loadMetadataForClass(PHPStaticTestEntity::class, $metadata); + } } class PHPTestEntity { } + +class PHPStaticTestEntity +{ +} diff --git a/tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPStaticTestEntity.php b/tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPStaticTestEntity.php new file mode 100644 index 00000000..6001d892 --- /dev/null +++ b/tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPStaticTestEntity.php @@ -0,0 +1,8 @@ +getFieldNames(); +}; From 4ac868817f2974dbc73aca09d61e9b24c3a65dd1 Mon Sep 17 00:00:00 2001 From: Arjan Frans Date: Mon, 9 Aug 2021 11:10:29 +0200 Subject: [PATCH 2/2] Fix codestyle --- .../Mapping/Driver/AnnotationDriver.php | 3 +-- .../RuntimePublicReflectionPropertyTest.php | 10 ++-------- .../TypedNoDefaultReflectionPropertyTest.php | 15 +++------------ 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php index 0380b916..ceec0e88 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php @@ -14,7 +14,6 @@ use function array_merge; use function array_unique; use function assert; -use function get_class; use function get_declared_classes; use function in_array; use function is_dir; @@ -179,7 +178,7 @@ public function isTransient($className) $classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className)); foreach ($classAnnotations as $annot) { - if (isset($this->entityAnnotationClasses[get_class($annot)])) { + if (isset($this->entityAnnotationClasses[$annot::class])) { return false; } } diff --git a/tests/Doctrine/Tests/Persistence/RuntimePublicReflectionPropertyTest.php b/tests/Doctrine/Tests/Persistence/RuntimePublicReflectionPropertyTest.php index 71cde933..a5651018 100644 --- a/tests/Doctrine/Tests/Persistence/RuntimePublicReflectionPropertyTest.php +++ b/tests/Doctrine/Tests/Persistence/RuntimePublicReflectionPropertyTest.php @@ -167,10 +167,7 @@ public function __setInitialized($initialized) $this->initialized = (bool) $initialized; } - /** - * @return mixed - */ - public function __get(string $name) + public function __get(string $name): mixed { if ($this->initializer) { $cb = $this->initializer; @@ -180,10 +177,7 @@ public function __get(string $name) return $this->checkedProperty; } - /** - * @param mixed $value - */ - public function __set(string $name, $value): void + public function __set(string $name, mixed $value): void { if ($this->initializer) { $cb = $this->initializer; diff --git a/tests/Doctrine/Tests_PHP74/Persistence/Reflection/TypedNoDefaultReflectionPropertyTest.php b/tests/Doctrine/Tests_PHP74/Persistence/Reflection/TypedNoDefaultReflectionPropertyTest.php index b617b109..94957d08 100644 --- a/tests/Doctrine/Tests_PHP74/Persistence/Reflection/TypedNoDefaultReflectionPropertyTest.php +++ b/tests/Doctrine/Tests_PHP74/Persistence/Reflection/TypedNoDefaultReflectionPropertyTest.php @@ -64,10 +64,7 @@ class TypedFoo { private int $id; - /** - * @param mixed $id - */ - public function setId($id): void + public function setId(mixed $id): void { $this->id = $id; } @@ -77,18 +74,12 @@ class TypedNullableFoo { private ?string $value; - /** - * @param mixed $value - */ - public function setValue($value): void + public function setValue(mixed $value): void { $this->value = $value; } - /** - * @return mixed - */ - public function getValue() + public function getValue(): mixed { return $this->value; }