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

Attribute wrapped in a Foundry Proxy is not compatible with Symfony\Component\VarExporter\LazyProxyTrait #639

Closed
jdecool opened this issue Jun 16, 2024 · 6 comments · Fixed by #644

Comments

@jdecool
Copy link

jdecool commented Jun 16, 2024

This issue is extracted from #626

Using Foundry 2 in a project that uses the odolbeau/phone-number-bundle, I've an error trying to generate an entity using a PhoneNumber class even if the PhoneNumber isn't created through a factory.

According to this entity:

class Customer
{
    public function __construct(
        #[ORM\Column(length: 255)]
        private ?string $name = null,

        #[ORM\Column(type: PhoneNumberType::NAME)]
        private PhoneNumber $phone,
    ) {
    }
}

And this Foundry factory:

final class CustomerFactory extends PersistentProxyObjectFactory
{
    public function __construct(
        private readonly PhoneNumberUtil $phoneNumberUtil,
    ) {
        parent::__construct();
    }

    public static function class(): string
    {
        return Customer::class;
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
     */
    protected function defaults(): array|callable
    {
        return [
            'name' => self::faker()->text(255),
            'phone' => $this->phoneNumberUtil->parse(self::faker()->phoneNumber(), 'FR_fr'),
        ];
    }
}

I've this error:

PHP Fatal error:  Declaration of Symfony\Component\VarExporter\LazyProxyTrait::__unserialize(array $data): void must be compatible with libphonenumber\PhoneNumber::__unserialize($data) in /home/jdecool/Workspace/sandbox/bug-foundry/vendor/symfony/var-exporter/LazyProxyTrait.php on line 322

Fatal error: Declaration of Symfony\Component\VarExporter\LazyProxyTrait::__unserialize(array $data): void must be compatible with libphonenumber\PhoneNumber::__unserialize($data) in /home/jdecool/Workspace/sandbox/bug-foundry/vendor/symfony/var-exporter/LazyProxyTrait.php on line 322
[critical] Fatal Compile Error: Declaration of Symfony\Component\VarExporter\LazyProxyTrait::__unserialize(array $data): void must be compatible with libphonenumber\PhoneNumber::__unserialize($data)

In LazyProxyTrait.php line 322:

  Compile Error: Declaration of Symfony\Component\VarExporter\LazyProxyTrait::__unserialize(array $data): void must be compatible with libphonenumber\PhoneNumber::__unserialize($data)

Because Foundry proxy defined a public function __unserialize(array $data): void function from Symfony\Component\VarExporter\LazyProxyTrait while the PhoneNumber class defines public function __unserialize($data): void

I've created a small project to reproduce the bug: https://github.com/jdecool/foundry-bug-demo

You just have to:

  1. composer install
  2. Run bin/console doctrine:fixtures:load -n to use the Foundry factory
@nikophil
Copy link
Member

nikophil commented Jun 20, 2024

Hi @jdecool

I think the problem comes from symfony/var-exporter
symfony/symfony#57460

I'll also fix this in Foundry, we should not try to "proxify" objects that are not orm entities nor odm documents

@jdecool
Copy link
Author

jdecool commented Jun 20, 2024

Thanks @nikophil

@nikophil
Copy link
Member

@jdecool any chance to test this PR before I merge and release?

@jdecool
Copy link
Author

jdecool commented Jun 20, 2024

I confirm it solves the problem 👍🏼

@jdecool
Copy link
Author

jdecool commented Jun 20, 2024

Thanks @nikophil

@nikophil
Copy link
Member

v2.0.4 released :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants