From f5f1fd7bd67750464efbe31f2f33587826dbaaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 22 Feb 2024 21:47:57 +0100 Subject: [PATCH] Translate comment into code and annotations The phpdoc comment for the return type of ClassMetadata::fullyQualifiedClassName() says that the return type will be null if the input value is null. I have made it more precise by using "if and only if", made the null check more strict and translated that into template annotations. Also, since we say we return a class-string, I've asserted that. --- phpstan-baseline.neon | 5 ----- psalm-baseline.xml | 16 ---------------- src/Mapping/ClassMetadata.php | 16 ++++++++-------- 3 files changed, 8 insertions(+), 29 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c1b0af26bfa..75e8f8aae5a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -125,11 +125,6 @@ parameters: count: 1 path: src/EntityRepository.php - - - message: "#^Method Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadata\\:\\:fullyQualifiedClassName\\(\\) should return class\\-string\\|null but returns string\\|null\\.$#" - count: 1 - path: src/Mapping/ClassMetadata.php - - message: "#^If condition is always true\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 688463b1bb2..bf54038d816 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -290,11 +290,6 @@ $repositoryClassName - - - $class - - columnNames]]> @@ -314,16 +309,7 @@ $definition - subClasses]]> - - $className - $className - namespace . '\\' . $className]]> - - - class-string|null - reflClass]]> @@ -331,8 +317,6 @@ $entity - $class - $className declaredField]]]> diff --git a/src/Mapping/ClassMetadata.php b/src/Mapping/ClassMetadata.php index 3f5b1c77732..b86ab53ee54 100644 --- a/src/Mapping/ClassMetadata.php +++ b/src/Mapping/ClassMetadata.php @@ -2465,12 +2465,16 @@ public function getAssociationMappedByTargetField(string $assocName): string } /** - * @return string|null null if the input value is null - * @psalm-return class-string|null + * @param C $className + * + * @return string|null null if and only if the input value is null + * @psalm-return (C is string ? string : null) + * + * @template C of string|null */ public function fullyQualifiedClassName(string|null $className): string|null { - if (empty($className)) { + if ($className === null) { return $className; } @@ -2514,12 +2518,8 @@ public function mapEmbedded(array $mapping): void throw MappingException::missingEmbeddedClass($mapping['fieldName']); } - $fqcn = $this->fullyQualifiedClassName($mapping['class']); - - assert($fqcn !== null); - $this->embeddedClasses[$mapping['fieldName']] = EmbeddedClassMapping::fromMappingArray([ - 'class' => $fqcn, + 'class' => $this->fullyQualifiedClassName($mapping['class']), 'columnPrefix' => $mapping['columnPrefix'] ?? null, 'declaredField' => $mapping['declaredField'] ?? null, 'originalField' => $mapping['originalField'] ?? null,