Skip to content

Commit 4c55779

Browse files
committed
fix: do not cast BackedEnum identifiers
1 parent 139896f commit 4c55779

4 files changed

+188
-2
lines changed

src/Proxy/ProxyGenerator.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -940,12 +940,16 @@ private function generateMethods(ClassMetadata $class)
940940
if ($this->isShortIdentifierGetter($method, $class)) {
941941
$identifier = lcfirst(substr($name, 3));
942942
$fieldType = $class->getTypeOfField($identifier);
943-
$cast = in_array($fieldType, ['integer', 'smallint'], true) ? '(int) ' : '';
943+
$castToInt = in_array($fieldType, ['integer', 'smallint'], true);
944944

945945
$methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
946946
$methods .= ' ';
947947
$methods .= $this->shouldProxiedMethodReturn($method) ? 'return ' : '';
948-
$methods .= $cast . ' parent::' . $method->getName() . "();\n";
948+
if ($castToInt) {
949+
$methods .= '! parent::' . $method->getName() . '() instanceof \BackedEnum ';
950+
$methods .= '? (int) parent::' . $method->getName() . '() : ';
951+
}
952+
$methods .= 'parent::' . $method->getName() . "();\n";
949953
$methods .= ' }' . "\n\n";
950954
}
951955

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Common\Proxy;
4+
5+
class LazyLoadableObjectWithPHP81EnumIntType
6+
{
7+
private LazyLoadableObjectWithPHP81EnumIntTypeIdentfier $identifierFieldEnumIntType;
8+
9+
public function getIdentifierFieldEnumIntType(): LazyLoadableObjectWithPHP81EnumIntTypeIdentfier
10+
{
11+
return $this->identifierFieldEnumIntType;
12+
}
13+
14+
public static function getFooIdentifier(): LazyLoadableObjectWithPHP81EnumIntTypeIdentfier
15+
{
16+
return LazyLoadableObjectWithPHP81EnumIntTypeIdentfier::FOO;
17+
}
18+
}
19+
20+
enum LazyLoadableObjectWithPHP81EnumIntTypeIdentfier: int
21+
{
22+
case FOO = 1;
23+
case BAR = 2;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Doctrine\Tests\Common\Proxy;
5+
6+
use BadMethodCallException;
7+
use Doctrine\Persistence\Mapping\ClassMetadata;
8+
use ReflectionClass;
9+
use function array_keys;
10+
11+
class LazyLoadableObjectWithPHP81EnumIntTypeClassMetadata implements ClassMetadata
12+
{
13+
/** @var ReflectionClass */
14+
protected $reflectionClass;
15+
16+
/** @var array<string,bool> */
17+
protected $identifier = [
18+
'identifierFieldEnumIntType' => true,
19+
];
20+
21+
/** @var array<string,bool> */
22+
protected $fields = [
23+
'identifierFieldEnumIntType' => true,
24+
];
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function getName()
30+
{
31+
return $this->getReflectionClass()->getName();
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getIdentifier()
38+
{
39+
return array_keys($this->identifier);
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function getReflectionClass()
46+
{
47+
if ($this->reflectionClass === null) {
48+
$this->reflectionClass = new ReflectionClass(__NAMESPACE__ . '\LazyLoadableObjectWithPHP81EnumIntType');
49+
}
50+
51+
return $this->reflectionClass;
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
public function isIdentifier($fieldName)
58+
{
59+
return isset($this->identifier[$fieldName]);
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function hasField($fieldName)
66+
{
67+
return isset($this->fields[$fieldName]);
68+
}
69+
70+
/**
71+
* {@inheritDoc}
72+
*/
73+
public function hasAssociation($fieldName)
74+
{
75+
return false;
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
public function isSingleValuedAssociation($fieldName)
82+
{
83+
throw new BadMethodCallException('not implemented');
84+
}
85+
86+
/**
87+
* {@inheritDoc}
88+
*/
89+
public function isCollectionValuedAssociation($fieldName)
90+
{
91+
throw new BadMethodCallException('not implemented');
92+
}
93+
94+
/**
95+
* {@inheritDoc}
96+
*/
97+
public function getFieldNames()
98+
{
99+
return array_keys($this->fields);
100+
}
101+
102+
/**
103+
* {@inheritDoc}
104+
*/
105+
public function getIdentifierFieldNames()
106+
{
107+
return $this->getIdentifier();
108+
}
109+
110+
/**
111+
* {@inheritDoc}
112+
*/
113+
public function getAssociationNames()
114+
{
115+
return [];
116+
}
117+
118+
/**
119+
* {@inheritDoc}
120+
*/
121+
public function getTypeOfField($fieldName)
122+
{
123+
return 'integer';
124+
}
125+
126+
/**
127+
* {@inheritDoc}
128+
*/
129+
public function getAssociationTargetClass($assocName)
130+
{
131+
throw new BadMethodCallException('not implemented');
132+
}
133+
134+
/**
135+
* {@inheritDoc}
136+
*/
137+
public function isAssociationInverseSide($assocName)
138+
{
139+
throw new BadMethodCallException('not implemented');
140+
}
141+
142+
/**
143+
* {@inheritDoc}
144+
*/
145+
public function getAssociationMappedByTargetField($assocName)
146+
{
147+
throw new BadMethodCallException('not implemented');
148+
}
149+
150+
/**
151+
* {@inheritDoc}
152+
*/
153+
public function getIdentifierValues($object)
154+
{
155+
throw new BadMethodCallException('not implemented');
156+
}
157+
}

tests/Common/Proxy/ProxyLogicIdentifierGetterTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function methodsForWhichLazyLoadingShouldBeDisabled()
7575
];
7676

7777
if (PHP_VERSION_ID >= 80000) {
78+
$data[] = [new LazyLoadableObjectWithPHP81EnumIntTypeClassMetadata(), 'identifierFieldEnumIntType', LazyLoadableObjectWithPHP81EnumIntType::getFooIdentifier()];
7879
$data[] = [new LazyLoadableObjectWithPHP8UnionTypeClassMetadata(), 'identifierFieldUnionType', 123];
7980
$data[] = [new LazyLoadableObjectWithPHP8UnionTypeClassMetadata(), 'identifierFieldUnionType', 'string'];
8081
}

0 commit comments

Comments
 (0)