Skip to content

Commit

Permalink
Move potentially expensive TypeUtil call to TypeResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 11, 2024
1 parent d7c32df commit ae1d4a1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/MainTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Rekalogika\Mapper\ObjectCache\ObjectCache;
use Rekalogika\Mapper\ObjectCache\ObjectCacheFactoryInterface;
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface;
use Rekalogika\Mapper\Util\TypeUtil;
use Symfony\Component\PropertyInfo\Type;

class MainTransformer implements MainTransformerInterface
Expand Down Expand Up @@ -87,7 +86,7 @@ public function transform(

// init vars

$targetType = TypeUtil::getSimpleTypes($targetType);
$targetType = $this->typeResolver->getSimpleTypes($targetType);
$sourceType = $this->typeResolver->guessTypeFromVariable($source);

foreach ($targetType as $singleTargetType) {
Expand Down
11 changes: 5 additions & 6 deletions src/ObjectCache/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Rekalogika\Mapper\Exception\CircularReferenceException;
use Rekalogika\Mapper\Exception\LogicException;
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface;
use Rekalogika\Mapper\Util\TypeUtil;
use Symfony\Component\PropertyInfo\Type;

final class ObjectCache
Expand Down Expand Up @@ -64,7 +63,7 @@ public function preCache(mixed $source, Type $targetType): void
return;
}

if (!TypeUtil::isSimpleType($targetType)) {
if (!$this->typeResolver->isSimpleType($targetType)) {
throw new LogicException('Target type must be simple type');
}

Expand All @@ -85,7 +84,7 @@ private function isPreCached(mixed $source, Type $targetType): bool
return false;
}

if (!TypeUtil::isSimpleType($targetType)) {
if (!$this->typeResolver->isSimpleType($targetType)) {
throw new LogicException('Target type must be simple type');
}

Expand All @@ -100,7 +99,7 @@ private function removePrecache(mixed $source, Type $targetType): void
return;
}

if (!TypeUtil::isSimpleType($targetType)) {
if (!$this->typeResolver->isSimpleType($targetType)) {
throw new LogicException('Target type must be simple type');
}

Expand All @@ -121,7 +120,7 @@ public function containsTarget(mixed $source, Type $targetType): bool
return false;
}

if (!TypeUtil::isSimpleType($targetType)) {
if (!$this->typeResolver->isSimpleType($targetType)) {
throw new LogicException('Target type must be simple type');
}

Expand All @@ -144,7 +143,7 @@ public function getTarget(mixed $source, Type $targetType): mixed
throw new CachedTargetObjectNotFoundException();
}

if (!TypeUtil::isSimpleType($targetType)) {
if (!$this->typeResolver->isSimpleType($targetType)) {
throw new LogicException('Target type must be simple type');
}

Expand Down
18 changes: 17 additions & 1 deletion src/TypeResolver/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class TypeResolver implements TypeResolverInterface
{
public static function guessTypeFromVariable(mixed $variable): Type
public function guessTypeFromVariable(mixed $variable): Type
{
if (is_object($variable)) {
return TypeFactory::objectOfClass($variable::class);
Expand Down Expand Up @@ -66,6 +66,22 @@ public function getTypeString(Type|MixedType $type): string
return TypeUtil::getTypeString($type);
}

public function isSimpleType(Type $type): bool
{
return TypeUtil::isSimpleType($type);
}

/**
* Gets all the possible simple types from a Type
*
* @param Type|array<array-key,Type> $type
* @return array<array-key,Type>
*/
public function getSimpleTypes(Type|array $type): array
{
return TypeUtil::getSimpleTypes($type);
}

public function getApplicableTypeStrings(array|Type|MixedType $type): array
{
if ($type instanceof MixedType) {
Expand Down
16 changes: 15 additions & 1 deletion src/TypeResolver/TypeResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@ interface TypeResolverInterface
/**
* Guesses the type of the given variable.
*/
public static function guessTypeFromVariable(mixed $variable): Type;
public function guessTypeFromVariable(mixed $variable): Type;

public function getTypeString(Type|MixedType $type): string;

/**
* Gets all the possible simple types from a Type
*
* @param Type|array<array-key,Type> $type
* @return array<array-key,Type>
*/
public function getSimpleTypes(Type|array $type): array;

/**
* Simple Type is a type that is not nullable, and does not have more
* than one key type or value type.
*/
public function isSimpleType(Type $type): bool;

/**
* Example: If the variable type is
* 'IteratorAggregate<int,IteratorAggregate<int,string>>', then this method
Expand Down
3 changes: 3 additions & 0 deletions src/Util/TypeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use DaveLiddament\PhpLanguageExtensions\Friend;
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
use Rekalogika\Mapper\Contracts\TypeMapping;
use Rekalogika\Mapper\Exception\InvalidArgumentException;
use Rekalogika\Mapper\Exception\MapperReturnsUnexpectedValueException;
use Rekalogika\Mapper\Model\MixedType;
Expand All @@ -33,6 +34,7 @@ private function __construct()
* Simple Type is a type that is not nullable, and does not have more
* than one key type or value type.
*/
#[Friend(TypeResolver::class, TypeUtilTest::class)]
public static function isSimpleType(Type $type): bool
{
if ($type->isNullable()) {
Expand Down Expand Up @@ -69,6 +71,7 @@ public static function isSimpleType(Type $type): bool
* @param Type|array<array-key,Type> $type
* @return array<array-key,Type>
*/
#[Friend(TypeMapping::class, TypeResolver::class)]
public static function getSimpleTypes(Type|array $type, bool $withParents = false): array
{
if (!is_array($type)) {
Expand Down

0 comments on commit ae1d4a1

Please sign in to comment.