-
-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: proxy with BackedEnum integer on identifier #997
base: 3.4.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) ' : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or better yet: idea that needs somebody with a deeper ORM knowledge than me to be validated: what if we get rid of the cast instead? The proxied entity will have the field already casted to a correct type thanks to initial id hydration. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I think that's the best way. If the method is typed, for the scalar types PHP already cast return value if needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @malarzm A test in ORM would be: $proxy = $em->getReference(CmsUser, "1");
$proxy->getId(); Unsure if we test this at the moment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @beberlei I'll try to have a closer look there in ORM 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @malarzm any news on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoting myself from #998
Sadly I didn't have time to get back to this :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can I help ? |
||
} | ||
|
||
$methods .= ' if ($this->__isInitialized__ === false) {' . "\n"; | ||
$methods .= ' '; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would type the argument as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Union types are only allowed since PHP 8.0. The PHP min version for this project is 7.1. |
||
{ | ||
if (PHP_VERSION_ID <= 80100 || $type === null) { | ||
return false; | ||
} | ||
|
||
if ($type instanceof ReflectionUnionType || $type instanceof ReflectionIntersectionType) { | ||
foreach ($type->getTypes() as $unionedType) { | ||
if (self::containsEnumType($unionedType)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
assert($type instanceof ReflectionNamedType); | ||
|
||
return enum_exists($type->getName()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this is only half-baked solution as without method's return type we still do not know what's inside. Maybe it'd be more efficient to generate an
instanceof \BackedEnum
check for thereturn
statement?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check instance in template, like this ?
from:
to: