From 69e0e3a5f1de6a5eedcfa4125d8639be91f0c303 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Fri, 19 Jul 2024 16:56:36 +0200 Subject: [PATCH] fix: allow docblock for transformer callable type --- .../Transformer/ValueTransformersHandler.php | 4 ++-- tests/Integration/Normalizer/NormalizerTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Normalizer/Transformer/ValueTransformersHandler.php b/src/Normalizer/Transformer/ValueTransformersHandler.php index d5cdf90a..ef42b56c 100644 --- a/src/Normalizer/Transformer/ValueTransformersHandler.php +++ b/src/Normalizer/Transformer/ValueTransformersHandler.php @@ -118,8 +118,8 @@ private function checkTransformer(MethodDefinition|FunctionDefinition $method): throw new TransformerHasTooManyParameters($method); } - if ($parameters->count() > 1 && ! $parameters->at(1)->type instanceof CallableType) { - throw new TransformerHasInvalidCallableParameter($method, $parameters->at(1)->type); + if ($parameters->count() > 1 && ! $parameters->at(1)->nativeType instanceof CallableType) { + throw new TransformerHasInvalidCallableParameter($method, $parameters->at(1)->nativeType); } } } diff --git a/tests/Integration/Normalizer/NormalizerTest.php b/tests/Integration/Normalizer/NormalizerTest.php index 7f3183fc..15a0ad55 100644 --- a/tests/Integration/Normalizer/NormalizerTest.php +++ b/tests/Integration/Normalizer/NormalizerTest.php @@ -979,6 +979,23 @@ public function test_second_param_in_transformer_is_not_callable_throws_exceptio ->normalize(new stdClass()); } + public function test_second_param_in_transformer_is_callable_with_phpdoc_spec_does_not_throw(): void + { + $class = new class () { + /** @param callable():mixed $next */ + public function __invoke(stdClass $object, callable $next): int + { + return 42; + } + }; + $this->mapperBuilder() + ->registerTransformer($class) + ->normalizer(Format::array()) + ->normalize(new stdClass()); + + self::addToAssertionCount(1); + } + public function test_no_param_in_transformer_attribute_throws_exception(): void { $this->expectException(TransformerHasNoParameter::class);