Skip to content

Commit

Permalink
perf: Optimize guessTypeFromVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 17, 2024
1 parent 9b3e99a commit e9d89fa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 0.5.15

* perf: Add caching for `TransformerRegistry`.
* perf: Optimize `guessTypeFromVariable`

## 0.5.14

Expand Down
32 changes: 5 additions & 27 deletions src/TypeResolver/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,14 @@ class TypeResolver implements TypeResolverInterface
{
public function guessTypeFromVariable(mixed $variable): Type
{
if (is_object($variable)) {
return TypeFactory::objectOfClass($variable::class);
}

if (is_null($variable)) {
return TypeFactory::null();
}

if (is_array($variable)) {
return TypeFactory::array();
}

if (is_bool($variable)) {
return TypeFactory::bool();
}

if (is_int($variable)) {
return TypeFactory::int();
}

if (is_float($variable)) {
return TypeFactory::float();
}
$type = get_debug_type($variable);

if (is_string($variable)) {
return TypeFactory::string();
if (in_array($type, ['array', 'bool', 'int', 'float', 'string'])) {
return new Type($type);
}

if (is_resource($variable)) {
return TypeFactory::resource();
if (class_exists($type)) {
return TypeFactory::objectOfClass($type);
}

throw new InvalidArgumentException(sprintf(
Expand Down

0 comments on commit e9d89fa

Please sign in to comment.