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
Handle PHP8.2 readonly classes #718
Handle PHP8.2 readonly classes #718
Conversation
I don't have a strong opinion on this, but I'm a little bit more in favor of throwing an exception in order to prevent some WTF moment for user which would not understand directly why the data is not mapped |
Agreed, having a RuntimeException is better for DX in my opinion. The Exception should educate about the readonly status and about alternatives. |
After thinking a bit more about this, I'll make it an exception by default and add an opt-out to disable it if required. |
ebccd27
to
9886b04
Compare
Here is a sample generated Mapper after this changes: final class Mapper_array_Jane_Component_AutoMapper_Tests_Fixtures_AddressDTOReadonlyClass extends \Jane\Component\AutoMapper\GeneratedMapper
{
public function __construct()
{
}
public function &map($value, array $context = array())
{
if (null === $value) {
return $value;
}
$result = $context['target_to_populate'] ?? null;
if (!($context['allow_readonly_target_to_populate'] ?? false) && is_object($context['target_to_populate'] ?? null)) {
throw new Jane\Component\AutoMapper\Exception\ReadOnlyTargetException();
}
if (null === $result) {
if (\Jane\Component\AutoMapper\MapperContext::hasConstructorArgument($context, 'Jane\\Component\\AutoMapper\\Tests\\Fixtures\\AddressDTOReadonlyClass', 'city')) {
$constructarg = \Jane\Component\AutoMapper\MapperContext::getConstructorArgument($context, 'Jane\\Component\\AutoMapper\\Tests\\Fixtures\\AddressDTOReadonlyClass', 'city');
} else {
$constructarg = $value['city'];
}
$result = new \Jane\Component\AutoMapper\Tests\Fixtures\AddressDTOReadonlyClass($constructarg);
}
return $result;
}
} There is two levels where you can setup this check:
|
9886b04
to
227fe2b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎
227fe2b
to
c1648bf
Compare
c1648bf
to
f288c36
Compare
I'm not sure we should throw an exception here, the current behavior seems fine to me: when using a readonly class as object to populate, it's simply ignored.
Here is a sample mapper from the test I added in this PR
@nikophil any feedback here ?