-
-
Couldn't load subscription status.
- Fork 514
Improve detection of the type from a PHP variable #2814
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
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 |
|---|---|---|
|
|
@@ -2823,9 +2823,19 @@ private function isTypedProperty(string $name): bool | |
| */ | ||
| private function validateAndCompleteTypedFieldMapping(array $mapping): array | ||
| { | ||
| if (isset($mapping['type'])) { | ||
| return $mapping; | ||
| } | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $type = $this->reflClass->getProperty($mapping['fieldName'])->getType(); | ||
|
|
||
| if (! $type instanceof ReflectionNamedType || isset($mapping['type'])) { | ||
| if (! $type instanceof ReflectionNamedType) { | ||
| return $mapping; | ||
| } | ||
|
|
||
| if (! $type->isBuiltin() && Type::hasType($type->getName())) { | ||
|
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. If I'm not mistaken, this would allow us to use a FQCN as identifier for a type name to automatically link values of that class with a given type? I haven't even thought of that before, but it makes so much sense! |
||
| $mapping['type'] = $type->getName(); | ||
|
|
||
| return $mapping; | ||
| } | ||
|
|
||
|
|
@@ -2836,6 +2846,7 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array | |
| throw MappingException::nonBackedEnumMapped($this->name, $mapping['fieldName'], $reflection->getName()); | ||
| } | ||
|
|
||
| // Use the backing type of the enum for the mapping type | ||
| $type = $reflection->getBackingType(); | ||
| assert($type instanceof ReflectionNamedType); | ||
| $mapping['enumType'] = $reflection->getName(); | ||
|
|
||
|
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. We don't have a global namespace for exception like it is generally done in libraries. Existing exception names are:
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. We could introduce a marker interface similar to how we have |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\ODM\MongoDB\Types; | ||
|
|
||
| use InvalidArgumentException; | ||
|
|
||
| use function sprintf; | ||
|
|
||
| final class InvalidTypeException extends InvalidArgumentException | ||
| { | ||
| public static function invalidTypeName(string $name): self | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return new self(sprintf('Invalid type specified: "%s"', $name)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,18 +4,16 @@ | |
|
|
||
| namespace Doctrine\ODM\MongoDB\Types; | ||
|
|
||
| use DateTimeImmutable; | ||
| use DateTimeInterface; | ||
| use Doctrine\ODM\MongoDB\Mapping\MappingException; | ||
| use Doctrine\ODM\MongoDB\Types; | ||
| use InvalidArgumentException; | ||
| use MongoDB\BSON\ObjectId; | ||
| use Symfony\Component\Uid\Uuid; | ||
|
|
||
| use function end; | ||
| use function explode; | ||
| use function gettype; | ||
| use function is_object; | ||
| use function sprintf; | ||
| use function str_replace; | ||
|
|
||
| /** | ||
|
|
@@ -157,52 +155,52 @@ public static function registerType(string $name, string $class): void | |
| /** | ||
| * Get a Type instance. | ||
| * | ||
| * @throws InvalidArgumentException | ||
| * @throws InvalidTypeException | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public static function getType(string $type): Type | ||
| { | ||
| if (! isset(self::$typesMap[$type])) { | ||
| throw new InvalidArgumentException(sprintf('Invalid type specified "%s".', $type)); | ||
| throw InvalidTypeException::invalidTypeName($type); | ||
| } | ||
|
|
||
| if (! isset(self::$typeObjects[$type])) { | ||
| $className = self::$typesMap[$type]; | ||
| self::$typeObjects[$type] = new $className(); | ||
| } | ||
|
|
||
| return self::$typeObjects[$type]; | ||
| return self::$typeObjects[$type] ??= new (self::$typesMap[$type]); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Get a Type instance based on the type of the passed php variable. | ||
| * | ||
| * @param mixed $variable | ||
| * | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public static function getTypeFromPHPVariable($variable): ?Type | ||
| { | ||
| if (is_object($variable)) { | ||
| if ($variable instanceof DateTimeInterface) { | ||
| return self::getType(self::DATE); | ||
| if ($variable instanceof DateTimeImmutable) { | ||
| return self::getType(self::DATE_IMMUTABLE); | ||
| } | ||
|
|
||
| if ($variable instanceof ObjectId) { | ||
| return self::getType(self::ID); | ||
|
Comment on lines
-190
to
-191
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 must check why this could be removed. 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. In fact, this type detection is invalid when the property type is
|
||
| if ($variable instanceof DateTimeInterface) { | ||
| return self::getType(self::DATE); | ||
| } | ||
|
|
||
| if ($variable instanceof Uuid) { | ||
| return self::getType(self::UUID); | ||
| } | ||
| } else { | ||
| $type = gettype($variable); | ||
| switch ($type) { | ||
| case 'integer': | ||
| return self::getType('int'); | ||
|
|
||
| // Try the variable class as type name | ||
| if (self::hasType($variable::class)) { | ||
| return self::getType($variable::class); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return null; | ||
| return match (gettype($variable)) { | ||
| 'integer' => self::getType(self::INT), | ||
| 'boolean' => self::getType(self::BOOL), | ||
| 'double' => self::getType(self::FLOAT), | ||
| 'string' => self::getType(self::STRING), | ||
| default => null, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.