diff --git a/CHANGELOG.md b/CHANGELOG.md index e94507d..fdeb06d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * docs: Add rationale. * fix: Add variance safeguard. +* dx: Clarity. ## 0.5.12 diff --git a/src/TransformerRegistry/TransformerRegistry.php b/src/TransformerRegistry/TransformerRegistry.php index 0bf861e..1b7590c 100644 --- a/src/TransformerRegistry/TransformerRegistry.php +++ b/src/TransformerRegistry/TransformerRegistry.php @@ -47,7 +47,27 @@ public function get(string $id): TransformerInterface return $transformer; } - public function findBySourceAndTargetType( + /** + * @todo cache this + * @param Type|MixedType $sourceType + * @param Type|MixedType $targetType + * @return array + */ + private function getMappingBySourceAndTargetType( + Type|MixedType $sourceType, + Type|MixedType $targetType, + ): array { + $sourceTypeStrings = $this->typeResolver + ->getAcceptedTransformerInputTypeStrings($sourceType); + + $targetTypeStrings = $this->typeResolver + ->getAcceptedTransformerOutputTypeStrings($targetType); + + return $this->mappingFactory->getMapping() + ->getMappingBySourceAndTarget($sourceTypeStrings, $targetTypeStrings); + } + + private function findBySourceAndTargetType( Type|MixedType $sourceType, Type|MixedType $targetType, ): SearchResult { @@ -59,7 +79,9 @@ public function findBySourceAndTargetType( $searchResultEntries = []; foreach ($mapping as $mappingEntry) { - if ($mappingEntry->isVariantTargetType() || !TypeCheck::isObject($targetType)) { + if ($mappingEntry->isVariantTargetType()) { + // if variant + $searchResultEntry = new SearchResultEntry( mappingOrder: $mappingEntry->getOrder(), sourceType: $sourceType, @@ -70,6 +92,8 @@ public function findBySourceAndTargetType( $searchResultEntries[] = $searchResultEntry; } else { + // if invariant, check if target type is somewhat identical + if ( TypeCheck::isSomewhatIdentical( $targetType, @@ -116,23 +140,4 @@ public function findBySourceAndTargetTypes( return new SearchResult($searchResultEntries); } - - /** - * @param Type|MixedType $sourceType - * @param Type|MixedType $targetType - * @return array - */ - private function getMappingBySourceAndTargetType( - Type|MixedType $sourceType, - Type|MixedType $targetType, - ): array { - $sourceTypeStrings = $this->typeResolver - ->getAcceptedTransformerInputTypeStrings($sourceType); - - $targetTypeStrings = $this->typeResolver - ->getAcceptedTransformerOutputTypeStrings($targetType); - - return $this->mappingFactory->getMapping() - ->getMappingBySourceAndTarget($sourceTypeStrings, $targetTypeStrings); - } }