-
-
Notifications
You must be signed in to change notification settings - 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
base: 2.13.x
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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:
|
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; | ||
|
||
/** | ||
|
@@ -143,52 +141,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
-176
to
-177
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.