nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Add possibility to use constants in attributes #81

Closed leprz closed 3 years ago

leprz commented 3 years ago

Hi, Is it possible to add support for constants in attributes arguments?

So generated code may look like this:

#[Argument('name', type: ArgumentTypes::REQUIRED)]

Currently it is possible to do something like [code below] or just use const value:

$class->setAttribute(
    'Argument', 
    [
        'name',
        'type' => 'ArgumentTypes::REQUIRED'
    ]
);

in the result a constant will be wrapped in the quotes and treated as a string

#[Argument('name', type: 'ArgumentTypes::REQUIRED')]

I believe this feature will be highly demanded as more and more libs and frameworks are adding support for PHP 8 attributes. And they already use constants.

mabar commented 3 years ago

new Nette\PhpGenerator\PhpLiteral('ArgumentTypes::REQUIRED') should do the trick.

leprz commented 3 years ago

Thanks @mabar 🥇 It works. I should read documentation more carefully there is a chapter about Literals.