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 f59a19c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 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
33 changes: 9 additions & 24 deletions src/TypeResolver/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,20 @@ 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();
}
$type = get_debug_type($variable);

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

if (is_string($variable)) {
return TypeFactory::string();
if (class_exists($type) || interface_exists($type) || \enum_exists($type)) {
return new Type(
builtinType: 'object',
class: $type,
);
}

if (is_resource($variable)) {
if (\str_starts_with($type, 'resource')) {
return TypeFactory::resource();
}

Expand Down

0 comments on commit f59a19c

Please sign in to comment.