Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate comment into code and annotations #11294

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 3.1

## Deprecate passing null to `ClassMetadata::fullyQualifiedClassName()`

Passing `null` to `Doctrine\ORM\ClassMetadata::fullyQualifiedClassName()` is
deprecated and will no longer be possible in 4.0.

## Deprecate array access

Using array access on instances of the following classes is deprecated:
Expand Down
7 changes: 0 additions & 7 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 Down Expand Up @@ -322,8 +317,6 @@
<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
33 changes: 25 additions & 8 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BackedEnum;
use BadMethodCallException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;
use Doctrine\Instantiator\Instantiator;
use Doctrine\Instantiator\InstantiatorInterface;
use Doctrine\ORM\Cache\Exception\NonCacheableEntityAssociation;
Expand Down Expand Up @@ -2003,6 +2004,12 @@ protected function _storeAssociationMapping(AssociationMapping $assocMapping): v
*/
public function setCustomRepositoryClass(string|null $repositoryClassName): void
{
if ($repositoryClassName === null) {
derrabus marked this conversation as resolved.
Show resolved Hide resolved
$this->customRepositoryClassName = null;

return;
}

$this->customRepositoryClassName = $this->fullyQualifiedClassName($repositoryClassName);
}

Expand Down Expand Up @@ -2464,11 +2471,25 @@ public function getAssociationMappedByTargetField(string $assocName): string
return $assoc->mappedBy;
}

/** @return string|null null if the input value is null */
/**
* @param C $className
*
* @return string|null null if and only if the input value is null
* @psalm-return (C is class-string ? class-string : (C is string ? string : null))
*
* @template C of string|null
*/
public function fullyQualifiedClassName(string|null $className): string|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to call this method with null? Should we rather deprecate that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would. Right now, it is only useful for a call from setCustomRepositoryClass. Let me push an extra commit that adds the deprecation.

{
if (empty($className)) {
return $className;
if ($className === null) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/11294',
'Passing null to %s is deprecated and will not be supported in Doctrine ORM 4.0',
__METHOD__,
);

return null;
}

if (! str_contains($className, '\\') && $this->namespace) {
Expand Down Expand Up @@ -2511,12 +2532,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
12 changes: 12 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArrayObject;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ChainTypedFieldMapper;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -66,6 +67,8 @@

class ClassMetadataTest extends OrmTestCase
{
use VerifyDeprecations;

public function testClassMetadataInstanceSerialization(): void
{
$cm = new ClassMetadata(CmsUser::class);
Expand Down Expand Up @@ -1052,6 +1055,15 @@ public function testItAddingLifecycleCallbackOnEmbeddedClassIsIllegal(): void

$metadata->addLifecycleCallback('foo', 'bar');
}

public function testGettingAnFQCNForNullIsDeprecated(): void
{
$metadata = new ClassMetadata(self::class);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/11294');

$metadata->fullyQualifiedClassName(null);
}
}

#[MappedSuperclass]
Expand Down
Loading