Skip to content

Commit

Permalink
Use EntityManagerInterface in type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jan 9, 2022
1 parent 1e97742 commit c0f353f
Show file tree
Hide file tree
Showing 53 changed files with 257 additions and 156 deletions.
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Upgrade to 2.11

## Rename `AbstractIdGenerator::generate()` to `generateId()`

Implementations of `AbstractIdGenerator` have to override the method
`generateId()` without calling the parent implementation. Not doing so is
deprecated. Calling `generate()` on any `AbstractIdGenerator` implementation
is deprecated.

## Deprecated: `Doctrine\ORM\Mapping\Driver\PHPDriver`

Use `StaticPHPDriver` instead when you want to programmatically configure
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EntityRepository implements ObjectRepository, Selectable
/** @var string */
protected $_entityName;

/** @var EntityManager */
/** @var EntityManagerInterface */
protected $_em;

/** @var ClassMetadata */
Expand Down Expand Up @@ -282,7 +282,7 @@ public function getClassName()
}

/**
* @return EntityManager
* @return EntityManagerInterface
*/
protected function getEntityManager()
{
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Event/LifecycleEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Event;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;

/**
Expand All @@ -28,7 +28,7 @@ public function getEntity()
/**
* Retrieves associated EntityManager.
*
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace Doctrine\ORM\Event;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs as BaseLoadClassMetadataEventArgs;

/**
* Class that holds event arguments for a loadMetadata event.
*
* @method __construct(ClassMetadata $classMetadata, EntityManager $objectManager)
* @method __construct(ClassMetadata $classMetadata, EntityManagerInterface $objectManager)
* @method ClassMetadata getClassMetadata()
*/
class LoadClassMetadataEventArgs extends BaseLoadClassMetadataEventArgs
{
/**
* Retrieve associated EntityManager.
*
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ORM/Event/PostFlushEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM\Event;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -15,7 +14,7 @@
*/
class PostFlushEventArgs extends EventArgs
{
/** @var EntityManager */
/** @var EntityManagerInterface */
private $em;

public function __construct(EntityManagerInterface $em)
Expand All @@ -26,7 +25,7 @@ public function __construct(EntityManagerInterface $em)
/**
* Retrieves associated EntityManager.
*
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ORM/Event/PreFlushEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM\Event;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -15,7 +14,7 @@
*/
class PreFlushEventArgs extends EventArgs
{
/** @var EntityManager */
/** @var EntityManagerInterface */
private $em;

public function __construct(EntityManagerInterface $em)
Expand All @@ -24,7 +23,7 @@ public function __construct(EntityManagerInterface $em)
}

/**
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
31 changes: 31 additions & 0 deletions lib/Doctrine/ORM/Id/AbstractIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,50 @@

namespace Doctrine\ORM\Id;

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;

use function get_debug_type;

abstract class AbstractIdGenerator
{
/**
* Generates an identifier for an entity.
*
* @deprecated Call {@see generateId()} instead.
*
* @param object|null $entity
*
* @return mixed
*/
abstract public function generate(EntityManager $em, $entity);

/**
* Generates an identifier for an entity.
*
* @param object|null $entity
*
* @return mixed
*/
public function generateId(EntityManagerInterface $em, $entity)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9325',
'Not implementing %s in %s is deprecated.',
__FUNCTION__,
get_debug_type($this)
);

if (! $em instanceof EntityManager) {
throw new InvalidArgumentException('Unsupported entity manager implementation.');
}

return $this->generate($em, $entity);
}

/**
* Gets whether this generator is a post-insert generator which means that
* {@link generate()} must be called after the entity has been inserted
Expand Down
8 changes: 5 additions & 3 deletions lib/Doctrine/ORM/Id/AssignedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\EntityMissingAssignedId;

use function get_class;
Expand All @@ -14,14 +14,16 @@
*/
class AssignedGenerator extends AbstractIdGenerator
{
use LegacyIdGeneratorProxy;

/**
* Returns the identifier assigned to the given entity.
*
* {@inheritDoc}
* {@inheritdoc}
*
* @throws EntityMissingAssignedId
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
$class = $em->getClassMetadata(get_class($entity));
$idFields = $class->getIdentifierFieldNames();
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Id generator that obtains IDs from special "identity" columns. These are columns
Expand All @@ -13,6 +13,8 @@
*/
class BigIntegerIdentityGenerator extends AbstractIdGenerator
{
use LegacyIdGeneratorProxy;

/**
* The name of the sequence to pass to lastInsertId(), if any.
*
Expand All @@ -33,7 +35,7 @@ public function __construct($sequenceName = null)
/**
* {@inheritDoc}
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
return (string) $em->getConnection()->lastInsertId($this->sequenceName);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/ORM/Id/IdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Id generator that obtains IDs from special "identity" columns. These are columns
Expand All @@ -13,6 +13,8 @@
*/
class IdentityGenerator extends AbstractIdGenerator
{
use LegacyIdGeneratorProxy;

/**
* The name of the sequence to pass to lastInsertId(), if any.
*
Expand All @@ -33,7 +35,7 @@ public function __construct($sequenceName = null)
/**
* {@inheritDoc}
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
return (int) $em->getConnection()->lastInsertId($this->sequenceName);
}
Expand Down
35 changes: 35 additions & 0 deletions lib/Doctrine/ORM/Id/LegacyIdGeneratorProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Id;

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManager;

use function get_debug_type;

/**
* @internal This trait will be removed in 3.0.
*/
trait LegacyIdGeneratorProxy
{
/**
* @deprecated
*
* @param object|null $entity
*
* @return mixed
*/
public function generate(EntityManager $em, $entity)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9325',
'%s::generate() is deprecated, call generateId() instead.',
get_debug_type($this)
);

return $this->generateId($em, $entity);
}
}
6 changes: 4 additions & 2 deletions lib/Doctrine/ORM/Id/SequenceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Doctrine\ORM\Id;

use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Serializable;

use function serialize;
Expand All @@ -16,6 +16,8 @@
*/
class SequenceGenerator extends AbstractIdGenerator implements Serializable
{
use LegacyIdGeneratorProxy;

/**
* The allocation size of the sequence.
*
Expand Down Expand Up @@ -51,7 +53,7 @@ public function __construct($sequenceName, $allocationSize)
/**
* {@inheritDoc}
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
// Allocate new values
Expand Down
8 changes: 5 additions & 3 deletions lib/Doctrine/ORM/Id/TableGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Id generator that uses a single-row database table and a hi/lo algorithm.
Expand All @@ -13,6 +13,8 @@
*/
class TableGenerator extends AbstractIdGenerator
{
use LegacyIdGeneratorProxy;

/** @var string */
private $_tableName;

Expand Down Expand Up @@ -43,8 +45,8 @@ public function __construct($tableName, $sequenceName = 'default', $allocationSi
/**
* {@inheritDoc}
*/
public function generate(
EntityManager $em,
public function generateId(
EntityManagerInterface $em,
$entity
) {
if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/ORM/Id/UuidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\NotSupported;

use function method_exists;
Expand All @@ -19,6 +19,8 @@
*/
class UuidGenerator extends AbstractIdGenerator
{
use LegacyIdGeneratorProxy;

public function __construct()
{
Deprecation::trigger(
Expand All @@ -38,7 +40,7 @@ public function __construct()
*
* @throws NotSupported
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
$connection = $em->getConnection();
$sql = 'SELECT ' . $connection->getDatabasePlatform()->getGuidExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function executeInserts()
$stmt->executeStatement();

if ($isPostInsertId) {
$generatedId = $idGenerator->generate($this->em, $entity);
$generatedId = $idGenerator->generateId($this->em, $entity);
$id = [$this->class->identifier[0] => $generatedId];
$postInsertIds[] = [
'generatedId' => $generatedId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function executeInserts()
$rootTableStmt->executeStatement();

if ($isPostInsertId) {
$generatedId = $idGenerator->generate($this->em, $entity);
$generatedId = $idGenerator->generateId($this->em, $entity);
$id = [$this->class->identifier[0] => $generatedId];
$postInsertIds[] = [
'generatedId' => $generatedId,
Expand Down
Loading

0 comments on commit c0f353f

Please sign in to comment.