doctrine / common

Doctrine Common
https://www.doctrine-project.org/projects/common.html
MIT License
5.79k stars 294 forks source link

Wrong proxy class generation with default values as constants referncing `self` #988

Open Lustmored opened 2 years ago

Lustmored commented 2 years ago

Recent fix for constants in default parameter values broke proxy generation when constant is referenced as self.

Example entity code:

public function hasAccess(int $level = self::LEVEL_WORKER): bool
    {
        return $this->level >= $level;
    }

Generated proxy class:

public function hasAccess(int $level = \self::LEVEL_WORKER): bool
    {

        $this->__initializer__ && $this->__initializer__->__invoke($this, 'hasAccess', [$level]);

        return parent::hasAccess($level);
    }

Which in effect causes Fatal Error in PHP 8.1:

[27-Sep-2022 09:11:12 UTC] PHP Fatal error:  '\self' is an invalid class name in /app/var/cache/beta/doctrine/orm/Proxies/__CG__AppEntitySomeEntity.php on line 242
malarzm commented 2 years ago

I'll start regretting introducing support for new in initializers... Sorry for the bug, we'll try to fix it hopefully this week

nicolas-grekas commented 2 years ago

Here is the code we use in Symfony to dump default values: https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/VarExporter/ProxyHelper.php#L318

Feel free to borrow it (it's written for PHP >= 8.1)