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

Make the class parameter mandatory for references #464

Merged
merged 2 commits into from
Mar 8, 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
11 changes: 11 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ awareness about deprecated code.

You need PHP 8.1 or newer to use this library.

For the following method, the `class` param is now mandatory:
- `AbstractFixture::getReference`
- `AbstractFixture::hasReference`
- `ReferenceRepository::setReferenceIdentity`
- `ReferenceRepository::hasIdentity`
- `ReferenceRepository::getReference`
- `ReferenceRepository::setReference`

The following method was removed:
- `ReferenceRepository::getReferences`

# Upgrade to 1.8

Executor and Purger classes are final, they cannot be extended.
Expand Down
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ parameters:

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getReference\\(\\)\\.$#"
count: 2
path: src/ProxyReferenceRepository.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 1
path: src/ProxyReferenceRepository.php

Expand Down Expand Up @@ -71,7 +66,7 @@ parameters:
path: src/Purger/PHPCRPurger.php

-
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\<object\\>\\:\\:\\$name\\.$#"
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\<T of object\\>\\:\\:\\$name\\.$#"
count: 1
path: src/ReferenceRepository.php

Expand Down
29 changes: 5 additions & 24 deletions src/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\Common\DataFixtures;

use BadMethodCallException;
use Doctrine\Deprecations\Deprecation;

use function assert;

Expand Down Expand Up @@ -79,24 +78,15 @@
*
* @see ReferenceRepository::getReference()
*
* @psalm-param class-string<T>|null $class
* @psalm-param class-string<T> $class
*
* @return object
* @psalm-return ($class is null ? object : T)
* @psalm-return T
*
* @template T of object
*/
public function getReference(string $name, string|null $class = null)
public function getReference(string $name, string $class)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}

return $this->getReferenceRepository()->getReference($name, $class);
}

Expand All @@ -106,21 +96,12 @@
*
* @see ReferenceRepository::hasReference()
*
* @psalm-param class-string|null $class
* @psalm-param class-string $class
*
* @return bool
*/
public function hasReference(string $name, string|null $class = null)
public function hasReference(string $name, string $class)

Check warning on line 103 in src/AbstractFixture.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractFixture.php#L103

Added line #L103 was not covered by tests
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}

return $this->getReferenceRepository()->hasReference($name, $class);
}
}
34 changes: 0 additions & 34 deletions src/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ class ProxyReferenceRepository extends ReferenceRepository
*/
public function serialize()
{
$unitOfWork = $this->getManager()->getUnitOfWork();
$simpleReferences = [];

foreach ($this->getReferences() as $name => $reference) {
$className = $this->getRealClass($reference::class);

$simpleReferences[$name] = [$className, $this->getIdentifier($reference, $unitOfWork)];
}

return serialize([
'references' => $simpleReferences, // For BC, remove in next major.
'identities' => $this->getIdentities(), // For BC, remove in next major.
'identitiesByClass' => $this->getIdentitiesByClass(),
]);
}
Expand All @@ -52,29 +41,6 @@ public function unserialize(string $serializedData)
{
$repositoryData = unserialize($serializedData);

// For BC, remove in next major.
if (! isset($repositoryData['identitiesByClass'])) {
$references = $repositoryData['references'];

foreach ($references as $name => $proxyReference) {
$this->setReference(
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1], // identifiers
),
);
}

$identities = $repositoryData['identities'];

foreach ($identities as $name => $identity) {
$this->setReferenceIdentity($name, $identity);
}

return;
}

foreach ($repositoryData['identitiesByClass'] as $className => $identities) {
foreach ($identities as $name => $identity) {
$this->setReference(
Expand Down
139 changes: 13 additions & 126 deletions src/ReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\Common\DataFixtures;

use BadMethodCallException;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ODM\PHPCR\DocumentManager as PhpcrDocumentManager;
use Doctrine\ORM\UnitOfWork as OrmUnitOfWork;
use Doctrine\Persistence\ObjectManager;
Expand All @@ -22,14 +21,6 @@
*/
class ReferenceRepository
{
/**
* List of named references to the fixture objects
* gathered during fixure loading
*
* @psalm-var array<string, object>
*/
private array $references = [];

/**
* List of named references to the fixture objects
* gathered during fixure loading
Expand All @@ -38,15 +29,6 @@ class ReferenceRepository
*/
private array $referencesByClass = [];

/**
* List of identifiers stored for references
* in case a reference gets no longer managed, it will
* use a proxy referenced by this identity
*
* @psalm-var array<string, mixed>
*/
private array $identities = [];

/**
* List of identifiers stored for references
* in case a reference gets no longer managed, it will
Expand Down Expand Up @@ -110,9 +92,6 @@ public function setReference(string $name, object $reference)

$this->referencesByClass[$class][$name] = $reference;

// For BC, to be removed in next major.
$this->references[$name] = $reference;

if (! $this->hasIdentifier($reference)) {
return;
}
Expand All @@ -122,34 +101,19 @@ public function setReference(string $name, object $reference)
$identifier = $this->getIdentifier($reference, $uow);

$this->identitiesByClass[$class][$name] = $identifier;

// For BC, to be removed in next major.
$this->identities[$name] = $identifier;
}

/**
* Store the identifier of a reference
*
* @param mixed $identity
* @param class-string|null $class
* @param mixed $identity
* @param class-string $class
*
* @return void
*/
public function setReferenceIdentity(string $name, $identity, string|null $class = null)
public function setReferenceIdentity(string $name, $identity, string $class)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}

$this->identitiesByClass[$class][$name] = $identity;

// For BC, to be removed in next major.
$this->identities[$name] = $identity;
}

/**
Expand All @@ -169,14 +133,6 @@ public function setReferenceIdentity(string $name, $identity, string|null $class
*/
public function addReference(string $name, object $object)
{
// For BC, to be removed in next major.
if (isset($this->references[$name])) {
throw new BadMethodCallException(sprintf(
'Reference to "%s" already exists, use method setReference() in order to override it',
$name,
));
}

$class = $this->getRealClass($object::class);
if (isset($this->referencesByClass[$class][$name])) {
throw new BadMethodCallException(sprintf(
Expand All @@ -193,52 +149,29 @@ public function addReference(string $name, object $object)
* Loads an object using stored reference
* named by $name
*
* @psalm-param class-string<T>|null $class
* @psalm-param class-string<T> $class
*
* @return object
* @psalm-return ($class is null ? object : T)
* @psalm-return T
*
* @throws OutOfBoundsException - if repository does not exist.
*
* @template T of object
*/
public function getReference(string $name, string|null $class = null)
public function getReference(string $name, string $class)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}

if (! $this->hasReference($name, $class)) {
// For BC, to be removed in next major.
if ($class === null) {
throw new OutOfBoundsException(sprintf('Reference to "%s" does not exist', $name));
}

throw new OutOfBoundsException(sprintf('Reference to "%s" for class "%s" does not exist', $name, $class));
}

$reference = $class === null
? $this->references[$name] // For BC, to be removed in next major.
: $this->referencesByClass[$class][$name];

$identity = $class === null
? ($this->identities[$name] ?? null) // For BC, to be removed in next major.
: ($this->identitiesByClass[$class][$name] ?? null);
$reference = $this->referencesByClass[$class][$name];

if ($class === null) { // For BC, to be removed in next major.
$class = $this->getRealClass($reference::class);
}
$identity = ($this->identitiesByClass[$class][$name] ?? null);

$meta = $this->manager->getClassMetadata($class);

if (! $this->manager->contains($reference) && $identity !== null) {
$reference = $this->manager->getReference($meta->name, $identity);
$this->references[$name] = $reference; // already in identity map
$this->referencesByClass[$class][$name] = $reference; // already in identity map
}

Expand All @@ -253,20 +186,9 @@ public function getReference(string $name, string|null $class = null)
*
* @return bool
*/
public function hasReference(string $name, string|null $class = null)
public function hasReference(string $name, string $class)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}

return $class === null
? isset($this->references[$name]) // For BC, to be removed in next major.
: isset($this->referencesByClass[$class][$name]);
return isset($this->referencesByClass[$class][$name]);
}

/**
Expand All @@ -288,36 +210,13 @@ public function getReferenceNames(object $reference)
/**
* Checks if reference has identity stored
*
* @param class-string|null $class
* @param class-string $class
*
* @return bool
*/
public function hasIdentity(string $name, string|null $class = null)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}

return $class === null
? array_key_exists($name, $this->identities) // For BC, to be removed in next major.
: array_key_exists($class, $this->identitiesByClass) && array_key_exists($name, $this->identitiesByClass[$class]);
}

/**
* @deprecated in favor of getIdentitiesByClass
*
* Get all stored identities
*
* @psalm-return array<string, object>
*/
public function getIdentities()
public function hasIdentity(string $name, string $class)
{
return $this->identities;
return array_key_exists($class, $this->identitiesByClass) && array_key_exists($name, $this->identitiesByClass[$class]);
}

/**
Expand All @@ -330,18 +229,6 @@ public function getIdentitiesByClass(): array
return $this->identitiesByClass;
}

/**
* @deprecated in favor of getReferencesByClass
*
* Get all stored references
*
* @psalm-return array<string, object>
*/
public function getReferences()
{
return $this->references;
}

/**
* Get all stored references
*
Expand Down
Loading