Skip to content

Commit

Permalink
Translate comment into code and annotations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greg0ire committed Feb 22, 2024
1 parent 2df4d75 commit 713ff42
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@
<code>$repositoryClassName</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Mapping/Builder/EntityListenerBuilder.php">
<PossiblyNullArgument>
<code>$class</code>
</PossiblyNullArgument>
</file>
<file src="src/Mapping/ClassMetadata.php">
<DeprecatedProperty>
<code><![CDATA[$this->columnNames]]></code>
Expand All @@ -314,25 +309,14 @@
</InvalidNullableReturnType>
<InvalidPropertyAssignmentValue>
<code>$definition</code>
<code><![CDATA[$this->subClasses]]></code>
</InvalidPropertyAssignmentValue>
<LessSpecificReturnStatement>
<code>$className</code>
<code>$className</code>
<code><![CDATA[$this->namespace . '\\' . $className]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code>class-string|null</code>
</MoreSpecificReturnType>
<NullableReturnStatement>
<code><![CDATA[$this->reflClass]]></code>
</NullableReturnStatement>
<ParamNameMismatch>
<code>$entity</code>
</ParamNameMismatch>
<PossiblyNullArgument>
<code>$class</code>
<code>$className</code>
<code><![CDATA[$mapping['targetEntity']]]></code>
<code><![CDATA[$mapping['targetEntity']]]></code>
<code><![CDATA[$parentReflFields[$embeddedClass->declaredField]]]></code>
Expand Down
20 changes: 11 additions & 9 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2465,19 +2465,25 @@ 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 ? class-string : null)
*
* @template C of string|null
*/
public function fullyQualifiedClassName(string|null $className): string|null
{
if (empty($className)) {
if ($className === null) {
return $className;
}

if (! str_contains($className, '\\') && $this->namespace) {
return $this->namespace . '\\' . $className;
$className = $this->namespace . '\\' . $className;
}

assert(class_exists($className) || interface_exists($className));

return $className;
}

Expand Down Expand Up @@ -2514,12 +2520,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,
Expand Down

0 comments on commit 713ff42

Please sign in to comment.