Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transformers): allow phpdoc for callables #547

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Normalizer/Transformer/ValueTransformersHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
17 changes: 17 additions & 0 deletions tests/Integration/Normalizer/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading