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

Promoted Parameters don't have "readonly" when using Factory #158

Closed WalterWoshid closed 5 months ago

WalterWoshid commented 5 months ago

Version: 6.36.0

Bug Description

When using $method = (new Factory)->fromMethodReflection($refMethod); and printing the code with $method->__toString(); the readonly keyword is missing.

Steps To Reproduce

class Number
{
    public function __construct(public readonly int $value) {}
}

$reflectionClass = new \ReflectionClass(Number::class);
$reflectionMethod = $reflectionClass->getMethod('__construct');
$method = (new Factory)->fromMethodReflection($reflectionMethod);
$result = (string)$method;

Expected Behavior

Should include readonly

Possible Solution

Factory::fromParameterReflection():

public function fromParameterReflection(...): Parameter
{
    // ...

    if ($from->isPromoted()
        && PHP_VERSION_ID >= 80100
        && ($declaringClass = $from->getDeclaringClass())
        && $declaringClass->hasProperty($from->name)
        && ($property = $declaringClass->getProperty($from->name))
        && $property->isPromoted()
        && !(PHP_VERSION_ID >= 80200 && $from->getDeclaringClass()->isReadOnly())
    ) {
        $param->setReadOnly($property->isReadOnly());
    }

    // ...
}
WalterWoshid commented 5 months ago

@dg Thank you!