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

new object in default parameter generate syntax error by Proxy Generator #971

Closed
yobrx opened this issue Aug 19, 2022 · 2 comments
Closed

Comments

@yobrx
Copy link

yobrx commented Aug 19, 2022

Bug Report

Q A
BC Break no
Version 2.13.1
PHP Version 8.1

Summary

In my entity, I have a method that returns an active season to me according to a start datetime, an end datetime and a non-required user datetime. If this parameter is not defined when method is called, then current datetime is setted.

Since PHP8.1, a new object is allowed as a default parameter value.

But Doctrine's Proxy Generator generate a class with syntax error.

Current behavior

Fatal error: Constant expression contains invalid operations in /var/www/html/var/cache/dev/doctrine/orm/Proxies/__CG__AppEntitySeason.php on line 374

    /**
     * {@inheritDoc}
     */
    public function isActive(\DateTimeInterface $now = DateTimeImmutable::__set_state(array(
   'date' => '2022-08-19 14:51:20.369415',
   'timezone_type' => 3,
   'timezone' => 'Europe/Paris',
))): bool
    {

        $this->__initializer__ && $this->__initializer__->__invoke($this, 'isActive', [$now]);

        return parent::isActive($now);
    }

How to reproduce

Define a method with a default parameter value in a entity.

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Table(name: 'saison')]
#[ORM\Entity(repositoryClass: SeasonRepository::class)]
class Season 
{
    #[ORM\Column(name: 'idsaison', type: 'integer', nullable: false)]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    private ?int $id = null;

    #[ORM\Column(name: 'debut', type: 'datetime_immutable', nullable: false)]
    private DateTimeInterface $start;

    #[ORM\Column(name: 'fin', type: 'datetime_immutable', nullable: false)]
    private DateTimeInterface $end;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getStart(): DateTimeInterface
    {
        return $this->start;
    }

    public function setStart(DateTimeInterface $start): self
    {
        $this->start = $start;

        return $this;
    }

    public function getEnd(): DateTimeInterface
    {
        return $this->end;
    }

    public function setEnd(DateTimeInterface $end): self
    {
        $this->end = $end;

        return $this;
    }

    public function isActive(\DateTimeInterface $now = new \DateTimeImmutable()): bool
    {
        return (null === $this->getStart() || $now >= $this->getStart())
            && (null === $this->getEnd() || $now < $this->getEnd());
    }
}

Expected behavior

/var/www/html/var/cache/dev/doctrine/orm/Proxies/__CG__AppEntitySeason.php

    /**
     * {@inheritDoc}
     */
    public function isActive(\DateTimeInterface $now = new DateTimeImmutable()): bool
    {

        $this->__initializer__ && $this->__initializer__->__invoke($this, 'isActive', [$now]);

        return parent::isActive($now);
    }

Temporary solution to avoid error

    public function isActive(\DateTimeInterface $now = null): bool
    {
        if (null === $now) {
            $now = new \DateTimeImmutable();
        }

        return (null === $this->getStart() || $now >= $this->getStart())
            && (null === $this->getEnd() || $now < $this->getEnd());
    }
@greg0ire greg0ire transferred this issue from doctrine/orm Aug 19, 2022
@greg0ire
Copy link
Member

Duplicate of #961

@greg0ire greg0ire marked this as a duplicate of #961 Aug 19, 2022
@greg0ire greg0ire closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2022
@yobrx
Copy link
Author

yobrx commented Aug 19, 2022

Oops, sorry... I was searched similar issue on doctrine/orm, not in doctrine/common. Glad to see the issue has been taken care of.

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

No branches or pull requests

2 participants