From 6c52964d1ad6c24d625df3f47625fd0d29ddfcc2 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 21 May 2024 18:59:24 +0200 Subject: [PATCH 1/4] Make data providers static (#367) --- tests/Persistence/Mapping/ColocatedMappingDriverTest.php | 2 +- tests/Persistence/Mapping/SymfonyFileLocatorTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Persistence/Mapping/ColocatedMappingDriverTest.php b/tests/Persistence/Mapping/ColocatedMappingDriverTest.php index af5c4b1f..cbb4fea6 100644 --- a/tests/Persistence/Mapping/ColocatedMappingDriverTest.php +++ b/tests/Persistence/Mapping/ColocatedMappingDriverTest.php @@ -65,7 +65,7 @@ public function testGetAllClassNames(string $path): void } /** @return Generator */ - public function pathProvider(): Generator + public static function pathProvider(): Generator { yield 'straigthforward path' => [__DIR__ . '/_files/colocated']; yield 'winding path' => [__DIR__ . '/../Mapping/_files/colocated']; diff --git a/tests/Persistence/Mapping/SymfonyFileLocatorTest.php b/tests/Persistence/Mapping/SymfonyFileLocatorTest.php index b93a8cb5..849c6d39 100644 --- a/tests/Persistence/Mapping/SymfonyFileLocatorTest.php +++ b/tests/Persistence/Mapping/SymfonyFileLocatorTest.php @@ -93,7 +93,7 @@ public function testInvalidCustomNamespaceSeparator(): void } /** @return array */ - public function customNamespaceSeparatorProvider(): array + public static function customNamespaceSeparatorProvider(): array { return [ 'directory separator' => [DIRECTORY_SEPARATOR, '/_custom_ns/dir'], @@ -122,7 +122,7 @@ public function testGetClassNamesWithCustomNsSeparator(string $separator, string } /** @return array}> */ - public function customNamespaceLookupQueryProvider(): array + public static function customNamespaceLookupQueryProvider(): array { return [ 'directory separator' => [ From bf7aab0e1ed225b5a247d65373aa833c4a2333ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Wed, 22 May 2024 19:14:26 +0200 Subject: [PATCH 2/4] Handle the right zero-value (#362) --- src/Persistence/Mapping/Driver/FileDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Persistence/Mapping/Driver/FileDriver.php b/src/Persistence/Mapping/Driver/FileDriver.php index d3e6b0c8..d2ba1663 100644 --- a/src/Persistence/Mapping/Driver/FileDriver.php +++ b/src/Persistence/Mapping/Driver/FileDriver.php @@ -173,7 +173,7 @@ abstract protected function loadMappingFile(string $file); protected function initialize() { $this->classCache = []; - if ($this->globalBasename === null) { + if ($this->globalBasename === '') { return; } From 4032a87b4c557000710c281b71d8eddb831503cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Wed, 19 Jun 2024 22:29:00 +0200 Subject: [PATCH 3/4] Relax type declaration getService() does not always return ObjectManager instances --- src/Persistence/AbstractManagerRegistry.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Persistence/AbstractManagerRegistry.php b/src/Persistence/AbstractManagerRegistry.php index 0586c364..cc245ba6 100644 --- a/src/Persistence/AbstractManagerRegistry.php +++ b/src/Persistence/AbstractManagerRegistry.php @@ -7,6 +7,7 @@ use InvalidArgumentException; use ReflectionClass; +use function assert; use function sprintf; /** @@ -63,7 +64,7 @@ public function __construct( * * @param string $name The name of the service. * - * @return ObjectManager The instance of the given service. + * @return object The instance of the given service. */ abstract protected function getService(string $name); @@ -160,7 +161,10 @@ public function getManager(?string $name = null) ); } - return $this->getService($this->managers[$name]); + $service = $this->getService($this->managers[$name]); + assert($service instanceof ObjectManager); + + return $service; } /** @@ -185,6 +189,7 @@ public function getManagerForClass(string $class) foreach ($this->managers as $id) { $manager = $this->getService($id); + assert($manager instanceof ObjectManager); if (! $manager->getMetadataFactory()->isTransient($class)) { return $manager; @@ -210,7 +215,8 @@ public function getManagers() $managers = []; foreach ($this->managers as $name => $id) { - $manager = $this->getService($id); + $manager = $this->getService($id); + assert($manager instanceof ObjectManager); $managers[$name] = $manager; } From bce9a5ef4f94fc50ac2a59e18fe09e63a64d327e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Wed, 19 Jun 2024 22:13:39 +0200 Subject: [PATCH 4/4] Make types compatible with ORM/ODM drivers It does not look like the implementations for these methods return ClassMetadata objects. --- src/Persistence/Mapping/Driver/FileDriver.php | 13 +++++++------ src/Persistence/Mapping/Driver/PHPDriver.php | 2 ++ tests/Persistence/Mapping/FileDriverTest.php | 1 + .../Mapping/Fixtures/TestClassMetadata.php | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Persistence/Mapping/Driver/FileDriver.php b/src/Persistence/Mapping/Driver/FileDriver.php index d2ba1663..c1162331 100644 --- a/src/Persistence/Mapping/Driver/FileDriver.php +++ b/src/Persistence/Mapping/Driver/FileDriver.php @@ -21,6 +21,8 @@ * classes on demand. This requires the user to adhere to the convention of 1 mapping * file per class and the file names of the mapping files must correspond to the full * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'. + * + * @template T */ abstract class FileDriver implements MappingDriver { @@ -28,8 +30,8 @@ abstract class FileDriver implements MappingDriver protected $locator; /** - * @var ClassMetadata[]|null - * @psalm-var array>|null + * @var mixed[]|null + * @psalm-var array|null */ protected $classCache; @@ -78,8 +80,7 @@ public function getGlobalBasename() * * @psalm-param class-string $className * - * @return ClassMetadata The element of schema meta data. - * @psalm-return ClassMetadata + * @return T The element of schema meta data. * * @throws MappingException */ @@ -154,8 +155,8 @@ public function getAllClassNames() * * @param string $file The mapping file to load. * - * @return ClassMetadata[] - * @psalm-return array> + * @return mixed[] + * @psalm-return array */ abstract protected function loadMappingFile(string $file); diff --git a/src/Persistence/Mapping/Driver/PHPDriver.php b/src/Persistence/Mapping/Driver/PHPDriver.php index b2ca7aec..1c1ab9c8 100644 --- a/src/Persistence/Mapping/Driver/PHPDriver.php +++ b/src/Persistence/Mapping/Driver/PHPDriver.php @@ -9,6 +9,8 @@ /** * The PHPDriver includes php files which just populate ClassMetadataInfo * instances with plain PHP code. + * + * @template-extends FileDriver> */ class PHPDriver extends FileDriver { diff --git a/tests/Persistence/Mapping/FileDriverTest.php b/tests/Persistence/Mapping/FileDriverTest.php index 996b22f8..6c9a1e6a 100644 --- a/tests/Persistence/Mapping/FileDriverTest.php +++ b/tests/Persistence/Mapping/FileDriverTest.php @@ -197,6 +197,7 @@ private function createTestFileDriver($locator, ?string $fileExtension = null): } } +/** @template-extends FileDriver> */ class TestFileDriver extends FileDriver { /** @var ClassMetadata */ diff --git a/tests/Persistence/Mapping/Fixtures/TestClassMetadata.php b/tests/Persistence/Mapping/Fixtures/TestClassMetadata.php index fd90bf43..753a944f 100644 --- a/tests/Persistence/Mapping/Fixtures/TestClassMetadata.php +++ b/tests/Persistence/Mapping/Fixtures/TestClassMetadata.php @@ -9,7 +9,7 @@ use ReflectionClass; /** - * @template T of object + * @template-covariant T of object * @template-implements ClassMetadata */ final class TestClassMetadata implements ClassMetadata