Skip to content

Commit a21843f

Browse files
authored
Remove AbstractIdGenerator::generate() (#9367)
… and add native types to ID generators
1 parent fd2bab9 commit a21843f

11 files changed

+44
-245
lines changed

UPGRADE.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Upgrade to 3.0
22

3+
## BC BREAK: Rename `AbstractIdGenerator::generate()` to `generateId()`
4+
5+
* Implementations of `AbstractIdGenerator` have to implement the method
6+
`generateId()`.
7+
* The method `generate()` has been removed from `AbstractIdGenerator`.
8+
39
## BC BREAK: Remove cache settings inspection
410

511
Doctrine does not provide its own cache implementation anymore and relies on

lib/Doctrine/ORM/Id/AbstractIdGenerator.php

+2-67
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,14 @@
44

55
namespace Doctrine\ORM\Id;
66

7-
use Doctrine\Deprecations\Deprecation;
8-
use Doctrine\ORM\EntityManager;
97
use Doctrine\ORM\EntityManagerInterface;
10-
use InvalidArgumentException;
11-
use LogicException;
12-
13-
use function get_debug_type;
14-
use function sprintf;
158

169
abstract class AbstractIdGenerator
1710
{
18-
/** @var bool */
19-
private $alreadyDelegatedToGenerateId = false;
20-
2111
/**
2212
* Generates an identifier for an entity.
23-
*
24-
* @deprecated Call {@see generateId()} instead.
25-
*
26-
* @param object|null $entity
27-
*
28-
* @return mixed
2913
*/
30-
public function generate(EntityManager $em, $entity)
31-
{
32-
if ($this->alreadyDelegatedToGenerateId) {
33-
throw new LogicException(sprintf(
34-
'Endless recursion detected in %s. Please implement generateId() without calling the parent implementation.',
35-
get_debug_type($this)
36-
));
37-
}
38-
39-
Deprecation::trigger(
40-
'doctrine/orm',
41-
'https://github.com/doctrine/orm/pull/9325',
42-
'%s::generate() is deprecated, call generateId() instead.',
43-
get_debug_type($this)
44-
);
45-
46-
$this->alreadyDelegatedToGenerateId = true;
47-
48-
try {
49-
return $this->generateId($em, $entity);
50-
} finally {
51-
$this->alreadyDelegatedToGenerateId = false;
52-
}
53-
}
54-
55-
/**
56-
* Generates an identifier for an entity.
57-
*
58-
* @param object|null $entity
59-
*
60-
* @return mixed
61-
*/
62-
public function generateId(EntityManagerInterface $em, $entity)
63-
{
64-
Deprecation::trigger(
65-
'doctrine/orm',
66-
'https://github.com/doctrine/orm/pull/9325',
67-
'Not implementing %s in %s is deprecated.',
68-
__FUNCTION__,
69-
get_debug_type($this)
70-
);
71-
72-
if (! $em instanceof EntityManager) {
73-
throw new InvalidArgumentException('Unsupported entity manager implementation.');
74-
}
75-
76-
return $this->generate($em, $entity);
77-
}
14+
abstract public function generateId(EntityManagerInterface $em, ?object $entity): mixed;
7815

7916
/**
8017
* Gets whether this generator is a post-insert generator which means that
@@ -83,10 +20,8 @@ public function generateId(EntityManagerInterface $em, $entity)
8320
*
8421
* By default, this method returns FALSE. Generators that have this requirement
8522
* must override this method and return TRUE.
86-
*
87-
* @return bool
8823
*/
89-
public function isPostInsertGenerator()
24+
public function isPostInsertGenerator(): bool
9025
{
9126
return false;
9227
}

lib/Doctrine/ORM/Id/AssignedGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AssignedGenerator extends AbstractIdGenerator
2121
*
2222
* @throws EntityMissingAssignedId
2323
*/
24-
public function generateId(EntityManagerInterface $em, $entity)
24+
public function generateId(EntityManagerInterface $em, ?object $entity): array
2525
{
2626
$class = $em->getClassMetadata(get_class($entity));
2727
$idFields = $class->getIdentifierFieldNames();

lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php

+5-18
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,22 @@
1313
*/
1414
class BigIntegerIdentityGenerator extends AbstractIdGenerator
1515
{
16-
/**
17-
* The name of the sequence to pass to lastInsertId(), if any.
18-
*
19-
* @var string|null
20-
*/
21-
private $sequenceName;
22-
2316
/**
2417
* @param string|null $sequenceName The name of the sequence to pass to lastInsertId()
2518
* to obtain the last generated identifier within the current
2619
* database session/connection, if any.
2720
*/
28-
public function __construct($sequenceName = null)
29-
{
30-
$this->sequenceName = $sequenceName;
21+
public function __construct(
22+
private ?string $sequenceName = null
23+
) {
3124
}
3225

33-
/**
34-
* {@inheritDoc}
35-
*/
36-
public function generateId(EntityManagerInterface $em, $entity)
26+
public function generateId(EntityManagerInterface $em, ?object $entity): string
3727
{
3828
return (string) $em->getConnection()->lastInsertId($this->sequenceName);
3929
}
4030

41-
/**
42-
* {@inheritDoc}
43-
*/
44-
public function isPostInsertGenerator()
31+
public function isPostInsertGenerator(): bool
4532
{
4633
return true;
4734
}

lib/Doctrine/ORM/Id/IdentityGenerator.php

+5-18
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,22 @@
1313
*/
1414
class IdentityGenerator extends AbstractIdGenerator
1515
{
16-
/**
17-
* The name of the sequence to pass to lastInsertId(), if any.
18-
*
19-
* @var string|null
20-
*/
21-
private $sequenceName;
22-
2316
/**
2417
* @param string|null $sequenceName The name of the sequence to pass to lastInsertId()
2518
* to obtain the last generated identifier within the current
2619
* database session/connection, if any.
2720
*/
28-
public function __construct($sequenceName = null)
29-
{
30-
$this->sequenceName = $sequenceName;
21+
public function __construct(
22+
private ?string $sequenceName = null
23+
) {
3124
}
3225

33-
/**
34-
* {@inheritDoc}
35-
*/
36-
public function generateId(EntityManagerInterface $em, $entity)
26+
public function generateId(EntityManagerInterface $em, ?object $entity): int
3727
{
3828
return (int) $em->getConnection()->lastInsertId($this->sequenceName);
3929
}
4030

41-
/**
42-
* {@inheritdoc}
43-
*/
44-
public function isPostInsertGenerator()
31+
public function isPostInsertGenerator(): bool
4532
{
4633
return true;
4734
}

lib/Doctrine/ORM/Id/SequenceGenerator.php

+22-58
Original file line numberDiff line numberDiff line change
@@ -16,85 +16,56 @@
1616
*/
1717
class SequenceGenerator extends AbstractIdGenerator implements Serializable
1818
{
19-
/**
20-
* The allocation size of the sequence.
21-
*
22-
* @var int
23-
*/
24-
private $_allocationSize;
25-
26-
/**
27-
* The name of the sequence.
28-
*
29-
* @var string
30-
*/
31-
private $_sequenceName;
32-
33-
/** @var int */
34-
private $_nextValue = 0;
35-
36-
/** @var int|null */
37-
private $_maxValue = null;
19+
private int $nextValue = 0;
20+
private ?int $maxValue = null;
3821

3922
/**
4023
* Initializes a new sequence generator.
4124
*
4225
* @param string $sequenceName The name of the sequence.
4326
* @param int $allocationSize The allocation size of the sequence.
4427
*/
45-
public function __construct($sequenceName, $allocationSize)
46-
{
47-
$this->_sequenceName = $sequenceName;
48-
$this->_allocationSize = $allocationSize;
28+
public function __construct(
29+
private string $sequenceName,
30+
private int $allocationSize
31+
) {
4932
}
5033

51-
/**
52-
* {@inheritDoc}
53-
*/
54-
public function generateId(EntityManagerInterface $em, $entity)
34+
public function generateId(EntityManagerInterface $em, ?object $entity): int
5535
{
56-
if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
36+
if ($this->maxValue === null || $this->nextValue === $this->maxValue) {
5737
// Allocate new values
5838
$connection = $em->getConnection();
59-
$sql = $connection->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);
39+
$sql = $connection->getDatabasePlatform()->getSequenceNextValSQL($this->sequenceName);
6040

6141
if ($connection instanceof PrimaryReadReplicaConnection) {
6242
$connection->ensureConnectedToPrimary();
6343
}
6444

65-
$this->_nextValue = (int) $connection->executeQuery($sql)->fetchOne();
66-
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
45+
$this->nextValue = (int) $connection->executeQuery($sql)->fetchOne();
46+
$this->maxValue = $this->nextValue + $this->allocationSize;
6747
}
6848

69-
return $this->_nextValue++;
49+
return $this->nextValue++;
7050
}
7151

7252
/**
7353
* Gets the maximum value of the currently allocated bag of values.
74-
*
75-
* @return int|null
7654
*/
77-
public function getCurrentMaxValue()
55+
public function getCurrentMaxValue(): ?int
7856
{
79-
return $this->_maxValue;
57+
return $this->maxValue;
8058
}
8159

8260
/**
8361
* Gets the next value that will be returned by generate().
84-
*
85-
* @return int
8662
*/
87-
public function getNextValue()
63+
public function getNextValue(): int
8864
{
89-
return $this->_nextValue;
65+
return $this->nextValue;
9066
}
9167

92-
/**
93-
* @return string
94-
*
95-
* @final
96-
*/
97-
public function serialize()
68+
final public function serialize(): string
9869
{
9970
return serialize($this->__serialize());
10071
}
@@ -105,19 +76,12 @@ public function serialize()
10576
public function __serialize(): array
10677
{
10778
return [
108-
'allocationSize' => $this->_allocationSize,
109-
'sequenceName' => $this->_sequenceName,
79+
'allocationSize' => $this->allocationSize,
80+
'sequenceName' => $this->sequenceName,
11081
];
11182
}
11283

113-
/**
114-
* @param string $serialized
115-
*
116-
* @return void
117-
*
118-
* @final
119-
*/
120-
public function unserialize($serialized)
84+
final public function unserialize(string $serialized): void
12185
{
12286
$this->__unserialize(unserialize($serialized));
12387
}
@@ -127,7 +91,7 @@ public function unserialize($serialized)
12791
*/
12892
public function __unserialize(array $data): void
12993
{
130-
$this->_sequenceName = $data['sequenceName'];
131-
$this->_allocationSize = $data['allocationSize'];
94+
$this->sequenceName = $data['sequenceName'];
95+
$this->allocationSize = $data['allocationSize'];
13296
}
13397
}

psalm.xml

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
<errorLevel type="suppress">
2828
<!-- We're calling the deprecated method for BC here. -->
2929
<file name="lib/Doctrine/ORM/Internal/SQLResultCasing.php"/>
30-
<!-- Remove on 3.0.x -->
31-
<referencedMethod name="Doctrine\ORM\Id\AbstractIdGenerator::generate"/>
3230
</errorLevel>
3331
</DeprecatedMethod>
3432
<DocblockTypeContradiction>

tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2415Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static function loadMetadata(ClassMetadataInfo $metadata): void
108108

109109
class DDC2415Generator extends AbstractIdGenerator
110110
{
111-
public function generateId(EntityManagerInterface $em, $entity): string
111+
public function generateId(EntityManagerInterface $em, ?object $entity): string
112112
{
113113
return md5($entity->getName());
114114
}

tests/Doctrine/Tests/ORM/Functional/Ticket/GH5804Test.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public function testTextColumnSaveAndRetrieve2(): void
5151

5252
final class GH5804Generator extends AbstractIdGenerator
5353
{
54-
/**
55-
* {@inheritdoc}
56-
*/
57-
public function generateId(EntityManagerInterface $em, $entity)
54+
public function generateId(EntityManagerInterface $em, ?object $entity): string
5855
{
5956
return 'test5804';
6057
}

0 commit comments

Comments
 (0)