Skip to content

Commit

Permalink
fix: Static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 13, 2024
1 parent 03e7b69 commit 1d04756
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Exception/UnableToFindSuitableTransformerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
class UnableToFindSuitableTransformerException extends NotMappableValueException
{
/**
* @param array<array-key,Type|MixedType> $sourceTypes
* @param array<array-key,Type|MixedType> $targetTypes
* @param array<int,Type|MixedType> $sourceTypes
* @param array<int,Type|MixedType> $targetTypes
*/
public function __construct(array $sourceTypes, array $targetTypes)
{
Expand Down
44 changes: 30 additions & 14 deletions src/MainTransformer/MainTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rekalogika\Mapper\ObjectCache\ObjectCacheFactoryInterface;
use Rekalogika\Mapper\TransformerRegistry\TransformerRegistryInterface;
use Rekalogika\Mapper\TypeResolver\TypeResolverInterface;
use Symfony\Component\PropertyInfo\Type;

class MainTransformer implements MainTransformerInterface
{
Expand Down Expand Up @@ -70,6 +71,24 @@ public static function getObjectCache(
return $objectCache;
}

/**
* @param array<array-key,Type|MixedType> $types
* @return array<int,Type|MixedType>
*/
private function getSimpleTypes(
array $types
): array {
$simpleTypes = [];

foreach ($types as $type) {
foreach ($this->typeResolver->getSimpleTypes($type) as $simpleType) {
$simpleTypes[] = $simpleType;
}
}

return $simpleTypes;
}

public function transform(
mixed $source,
mixed $target,
Expand All @@ -96,34 +115,31 @@ public function transform(

// gets simple target types from the provided target type

$simpleTargetTypes = [];

foreach ($targetTypes as $targetType) {
foreach ($this->typeResolver->getSimpleTypes($targetType) as $simpleType) {
$simpleTargetTypes[] = $simpleType;
}
}
$targetTypes = $this->getSimpleTypes($targetTypes);

// guess the source type

$sourceType = $this->typeResolver->guessTypeFromVariable($source);
$sourceTypes = [$this->typeResolver->guessTypeFromVariable($source)];

// iterate simple target types and find the suitable transformer
// search for the matching transformers according to the source and
// target types

$searchResult = $this->transformerRegistry
->findBySourceAndTargetTypes(
[$sourceType],
$simpleTargetTypes
$sourceTypes,
$targetTypes
);

// loop over the result and transform the source to the target

foreach ($searchResult as $searchEntry) {
$transformer = $this->processTransformer($searchEntry->getTransformer());

$sourceType = $searchEntry->getSourceType();
$sourceTypeForTransformer = $sourceType instanceof MixedType ? null : $sourceType;

$targetTypes = $searchEntry->getTargetType();
$targetTypeForTransformer = $targetTypes instanceof MixedType ? null : $targetTypes;
$targetType = $searchEntry->getTargetType();
$targetTypeForTransformer = $targetType instanceof MixedType ? null : $targetType;

/** @var mixed */
$result = $transformer->transform(
Expand All @@ -137,6 +153,6 @@ public function transform(
return $result;
}

throw new UnableToFindSuitableTransformerException([$sourceType], $simpleTargetTypes);
throw new UnableToFindSuitableTransformerException($sourceTypes, $targetTypes);
}
}

0 comments on commit 1d04756

Please sign in to comment.