From c62177e0efaa2714082a125ff011267b0095af55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 17 May 2021 20:25:03 +0200 Subject: [PATCH] Describe some types more accurately Downstream builds are broken because the last release of persistence describes its inputs more accurately, but not its outputs. --- .../Persistence/Mapping/Driver/AnnotationDriver.php | 1 + .../Persistence/Mapping/Driver/DefaultFileLocator.php | 4 +++- lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php | 8 +++++--- lib/Doctrine/Persistence/Mapping/Driver/FileLocator.php | 1 + lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php | 6 +++++- .../Persistence/Mapping/Driver/StaticPHPDriver.php | 1 + .../Persistence/Mapping/Driver/SymfonyFileLocator.php | 8 ++++++-- lib/Doctrine/Persistence/ObjectManager.php | 2 +- phpstan-baseline.neon | 4 ++++ phpstan.neon | 2 +- psalm.xml | 3 ++- .../Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php | 6 +++++- ... Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php} | 0 13 files changed, 35 insertions(+), 11 deletions(-) rename tests/Doctrine/Tests/Persistence/Mapping/_files/{TestEntity.php => Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php} (100%) diff --git a/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php index fd5420c7..0380b916 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php @@ -61,6 +61,7 @@ abstract class AnnotationDriver implements MappingDriver * Cache for AnnotationDriver#getAllClassNames(). * * @var string[]|null + * @psalm-var list|null */ protected $classNames; diff --git a/lib/Doctrine/Persistence/Mapping/Driver/DefaultFileLocator.php b/lib/Doctrine/Persistence/Mapping/Driver/DefaultFileLocator.php index a2eb17c5..532cf42d 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/DefaultFileLocator.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/DefaultFileLocator.php @@ -136,7 +136,9 @@ public function getAllClassNames($globalBasename) } // NOTE: All files found here means classes are not transient! - $classes[] = str_replace('.', '\\', $fileName); + /** @psalm-var class-string */ + $class = str_replace('.', '\\', $fileName); + $classes[] = $class; } } } diff --git a/lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php b/lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php index 86e4e0f6..9dd6a89d 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/FileDriver.php @@ -8,6 +8,7 @@ use function array_keys; use function array_merge; use function array_unique; +use function array_values; use function is_file; use function str_replace; @@ -26,7 +27,7 @@ abstract class FileDriver implements MappingDriver /** * @var ClassMetadata[]|null - * @psalm-var ClassMetadata[]|null + * @psalm-var array>|null */ protected $classCache; @@ -77,6 +78,7 @@ public function getGlobalBasename() * This will lazily load the mapping file if it is not loaded yet. * * @param string $className + * @psalm-param class-string $className * * @return ClassMetadata The element of schema meta data. * @psalm-return ClassMetadata @@ -132,10 +134,10 @@ public function getAllClassNames() return (array) $this->locator->getAllClassNames($this->globalBasename); } - return array_unique(array_merge( + return array_values(array_unique(array_merge( array_keys($this->classCache), (array) $this->locator->getAllClassNames($this->globalBasename) - )); + ))); } /** diff --git a/lib/Doctrine/Persistence/Mapping/Driver/FileLocator.php b/lib/Doctrine/Persistence/Mapping/Driver/FileLocator.php index a4dd570e..d9b8f06c 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/FileLocator.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/FileLocator.php @@ -25,6 +25,7 @@ public function findMappingFile($className); * @param string|null $globalBasename Passed to allow excluding the basename. * * @return string[] + * @psalm-return list */ public function getAllClassNames($globalBasename); diff --git a/lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php b/lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php index 71ac9935..905b7f2b 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/MappingDriver.php @@ -13,9 +13,12 @@ interface MappingDriver * Loads the metadata for the specified class into the provided container. * * @param string $className - * @psalm-param ClassMetadata $metadata + * @psalm-param class-string $className + * @psalm-param ClassMetadata $metadata * * @return void + * + * @template T of object */ public function loadMetadataForClass($className, ClassMetadata $metadata); @@ -23,6 +26,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata); * Gets the names of all mapped classes known to this driver. * * @return string[] The names of all mapped classes known to this driver. + * @psalm-return list */ public function getAllClassNames(); diff --git a/lib/Doctrine/Persistence/Mapping/Driver/StaticPHPDriver.php b/lib/Doctrine/Persistence/Mapping/Driver/StaticPHPDriver.php index 3f7b5cac..844d9e94 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/StaticPHPDriver.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/StaticPHPDriver.php @@ -33,6 +33,7 @@ class StaticPHPDriver implements MappingDriver * Map of all class names. * * @var string[] + * @psalm-var list */ private $classNames; diff --git a/lib/Doctrine/Persistence/Mapping/Driver/SymfonyFileLocator.php b/lib/Doctrine/Persistence/Mapping/Driver/SymfonyFileLocator.php index c2a70891..618c4e48 100644 --- a/lib/Doctrine/Persistence/Mapping/Driver/SymfonyFileLocator.php +++ b/lib/Doctrine/Persistence/Mapping/Driver/SymfonyFileLocator.php @@ -194,10 +194,14 @@ public function getAllClassNames($globalBasename = null) '\\' ); - $classes[] = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' . str_replace($this->nsSeparator, '\\', $fileName); + /** @psalm-var class-string */ + $class = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' . str_replace($this->nsSeparator, '\\', $fileName); } else { - $classes[] = str_replace($this->nsSeparator, '\\', $fileName); + /** @psalm-var class-string */ + $class = str_replace($this->nsSeparator, '\\', $fileName); } + + $classes[] = $class; } } } diff --git a/lib/Doctrine/Persistence/ObjectManager.php b/lib/Doctrine/Persistence/ObjectManager.php index 74f0016c..136d61f1 100644 --- a/lib/Doctrine/Persistence/ObjectManager.php +++ b/lib/Doctrine/Persistence/ObjectManager.php @@ -22,7 +22,7 @@ interface ObjectManager * @return object|null The found object. * @psalm-return T|null * - * @template T + * @template T of object */ public function find($className, $id); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ebf697bc..fd5c14be 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20,3 +20,7 @@ parameters: - message: '#Method Doctrine\\Tests\\Persistence\\Mapping\\TestClassMetadataFactory\:\:getFqcnFromAlias\(\) should return class\-string but returns string\.#' path: 'tests/Doctrine/Tests/Persistence/Mapping/TestClassMetadataFactory.php' + # https://github.com/phpstan/phpstan/issues/1267 + - + message: "#^Call to an undefined static method T of object\\:\\:loadMetadata\\(\\)\\.$#" + path: lib/Doctrine/Persistence/Mapping/Driver/StaticPHPDriver.php diff --git a/phpstan.neon b/phpstan.neon index bf412b71..94f38865 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,4 +9,4 @@ parameters: - tests excludes_analyse: - - tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php + - tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php diff --git a/psalm.xml b/psalm.xml index c80b2938..97010531 100644 --- a/psalm.xml +++ b/psalm.xml @@ -11,7 +11,8 @@ - + + diff --git a/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php b/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php index bd5652e7..1b6d730e 100644 --- a/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php +++ b/tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php @@ -14,6 +14,10 @@ public function testLoadMetadata(): void $metadata->expects($this->once())->method('getFieldNames'); $driver = new PHPDriver([__DIR__ . '/_files']); - $driver->loadMetadataForClass('TestEntity', $metadata); + $driver->loadMetadataForClass(PHPTestEntity::class, $metadata); } } + +class PHPTestEntity +{ +} diff --git a/tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php b/tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php similarity index 100% rename from tests/Doctrine/Tests/Persistence/Mapping/_files/TestEntity.php rename to tests/Doctrine/Tests/Persistence/Mapping/_files/Doctrine.Tests.Persistence.Mapping.PHPTestEntity.php