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 221c7af
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 37 deletions.
25 changes: 0 additions & 25 deletions src/Contracts/TypeMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Rekalogika\Mapper\Contracts;

use Rekalogika\Mapper\Model\MixedType;
use Rekalogika\Mapper\Util\TypeUtil;
use Symfony\Component\PropertyInfo\Type;

class TypeMapping
Expand All @@ -37,35 +36,11 @@ public function getSourceType(): Type|MixedType
return $this->sourceType;
}

/**
* @return array<array-key,Type|MixedType>
*/
public function getSimpleSourceTypes(): array
{
if ($this->sourceType instanceof MixedType) {
return [$this->sourceType];
}

return TypeUtil::getSimpleTypes($this->sourceType);
}

/**
* @return Type|MixedType
*/
public function getTargetType(): Type|MixedType
{
return $this->targetType;
}

/**
* @return array<array-key,Type|MixedType>
*/
public function getSimpleTargetTypes(): array
{
if ($this->targetType instanceof MixedType) {
return [$this->targetType];
}

return TypeUtil::getSimpleTypes($this->targetType);
}
}
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
18 changes: 16 additions & 2 deletions src/Mapping/MappingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace Rekalogika\Mapper\Mapping;

use Rekalogika\Mapper\Contracts\TransformerInterface;
use Rekalogika\Mapper\Model\MixedType;
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface;
use Symfony\Component\PropertyInfo\Type;

/**
* Initialize transformer mappings
Expand Down Expand Up @@ -62,8 +64,8 @@ private function addMapping(
TransformerInterface $transformer
): void {
foreach ($transformer->getSupportedTransformation() as $typeMapping) {
$sourceTypes = $typeMapping->getSimpleSourceTypes();
$targetTypes = $typeMapping->getSimpleTargetTypes();
$sourceTypes = $this->getSimpleTypes($typeMapping->getSourceType());
$targetTypes = $this->getSimpleTypes($typeMapping->getTargetType());

foreach ($sourceTypes as $sourceType) {
foreach ($targetTypes as $targetType) {
Expand All @@ -80,4 +82,16 @@ class: get_class($transformer),
}
}
}

/**
* @return array<array-key,Type|MixedType>
*/
private function getSimpleTypes(Type|MixedType $type): array
{
if ($type instanceof MixedType) {
return [$type];
}

return $this->typeResolver->getSimpleTypes($type);
}
}
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(TypeResolver::class)]
public static function getSimpleTypes(Type|array $type, bool $withParents = false): array
{
if (!is_array($type)) {
Expand Down

0 comments on commit 221c7af

Please sign in to comment.