Skip to content

Commit

Permalink
fix: proxy with BackedEnum integer on identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Gwemox committed Oct 14, 2022
1 parent 139896f commit 4f878f3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use function chmod;
use function class_exists;
use function dirname;
use function enum_exists;
use function explode;
use function file;
use function file_put_contents;
Expand Down Expand Up @@ -940,7 +941,12 @@ private function generateMethods(ClassMetadata $class)
if ($this->isShortIdentifierGetter($method, $class)) {
$identifier = lcfirst(substr($name, 3));
$fieldType = $class->getTypeOfField($identifier);
$cast = in_array($fieldType, ['integer', 'smallint'], true) ? '(int) ' : '';

if (ClassUtils::containsEnumType($method->getReturnType())) {
$cast = '';
} else {
$cast = in_array($fieldType, ['integer', 'smallint'], true) ? '(int) ' : '';
}

$methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
$methods .= ' ';
Expand Down
34 changes: 34 additions & 0 deletions src/Util/ClassUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

use Doctrine\Persistence\Proxy;
use ReflectionClass;
use ReflectionIntersectionType;
use ReflectionNamedType;
use ReflectionUnionType;

use function assert;
use function enum_exists;
use function get_class;
use function get_parent_class;
use function ltrim;
use function rtrim;
use function strrpos;
use function substr;

use const PHP_VERSION_ID;

/**
* Class and reflection related functionality for objects that
* might or not be proxy objects at the moment.
Expand Down Expand Up @@ -110,4 +117,31 @@ public static function generateProxyClassName($className, $proxyNamespace)
{
return rtrim($proxyNamespace, '\\') . '\\' . Proxy::MARKER . '\\' . ltrim($className, '\\');
}

/**
* Check if the type is an enum type or type containing an enum type
*
* @param ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type
*
*/
public static function containsEnumType($type): bool
{
if (PHP_VERSION_ID <= 80100 || $type === null) {
return false;
}

if ($type instanceof ReflectionUnionType || $type instanceof ReflectionIntersectionType) {
foreach ($type->getTypes() as $unionedType) {
if (static::containsEnumType($unionedType)) {
return true;
}
}

return false;
}

assert($type instanceof ReflectionNamedType);

return enum_exists($type->getName());
}
}

0 comments on commit 4f878f3

Please sign in to comment.