Skip to content

Commit 5b54ff3

Browse files
committed
fix: proxy with BackedEnum integer on identifier
1 parent 139896f commit 5b54ff3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Proxy/ProxyGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use function chmod;
2828
use function class_exists;
2929
use function dirname;
30+
use function enum_exists;
3031
use function explode;
3132
use function file;
3233
use function file_put_contents;
@@ -940,7 +941,12 @@ private function generateMethods(ClassMetadata $class)
940941
if ($this->isShortIdentifierGetter($method, $class)) {
941942
$identifier = lcfirst(substr($name, 3));
942943
$fieldType = $class->getTypeOfField($identifier);
943-
$cast = in_array($fieldType, ['integer', 'smallint'], true) ? '(int) ' : '';
944+
945+
if (ClassUtils::containsEnumType($method->getReturnType())) {
946+
$cast = '';
947+
} else {
948+
$cast = in_array($fieldType, ['integer', 'smallint'], true) ? '(int) ' : '';
949+
}
944950

945951
$methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
946952
$methods .= ' ';

src/Util/ClassUtils.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44

55
use Doctrine\Persistence\Proxy;
66
use ReflectionClass;
7+
use ReflectionIntersectionType;
8+
use ReflectionNamedType;
9+
use ReflectionUnionType;
710

11+
use function assert;
12+
use function enum_exists;
813
use function get_class;
914
use function get_parent_class;
1015
use function ltrim;
1116
use function rtrim;
1217
use function strrpos;
1318
use function substr;
1419

20+
use const PHP_VERSION_ID;
21+
1522
/**
1623
* Class and reflection related functionality for objects that
1724
* might or not be proxy objects at the moment.
@@ -110,4 +117,31 @@ public static function generateProxyClassName($className, $proxyNamespace)
110117
{
111118
return rtrim($proxyNamespace, '\\') . '\\' . Proxy::MARKER . '\\' . ltrim($className, '\\');
112119
}
120+
121+
/**
122+
* Check if the type is an enum type or type containing an enum type
123+
*
124+
* @param ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type
125+
*
126+
*/
127+
public static function containsEnumType($type): bool
128+
{
129+
if (PHP_VERSION_ID <= 80100 || $type === null) {
130+
return false;
131+
}
132+
133+
if ($type instanceof ReflectionUnionType || $type instanceof ReflectionIntersectionType) {
134+
foreach ($type->getTypes() as $unionedType) {
135+
if (self::containsEnumType($unionedType)) {
136+
return true;
137+
}
138+
}
139+
140+
return false;
141+
}
142+
143+
assert($type instanceof ReflectionNamedType);
144+
145+
return enum_exists($type->getName());
146+
}
113147
}

0 commit comments

Comments
 (0)