Skip to content

Commit

Permalink
Factory: properties in readonly classes are not readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 30, 2023
1 parent 43c9b67 commit ea40f2f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
$prop->setType((string) $from->getType());

$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
$prop->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->isReadOnly() : false);
$prop->setReadOnly(PHP_VERSION_ID >= 80100 && $from->isReadOnly() && !(PHP_VERSION_ID >= 80200 && $from->getDeclaringClass()->isReadOnly()));
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$prop->setAttributes($this->getAttributes($from));
return $prop;
Expand Down
24 changes: 24 additions & 0 deletions tests/PhpGenerator/ClassType.readonly.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @phpVersion 8.2
*/

declare(strict_types=1);

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Extractor;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/classes.82.php';

$class = ClassType::from(new Abc\Class13);
Assert::false($class->getProperty('foo')->isReadOnly());
Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());

$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.82.php')))->extractAll();
$class = $file->getClasses()[Abc\Class13::class];
Assert::false($class->getProperty('foo')->isReadOnly());
Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());
9 changes: 9 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.82.expect
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
readonly class Class13
{
public bool $foo;


public function __construct(
public bool $bar = true,
) {
}


public function func(C|(X&D)|null $foo): (A&B)|null
{
}
Expand Down
9 changes: 9 additions & 0 deletions tests/PhpGenerator/expected/Extractor.classes.82.expect
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ namespace Abc;

readonly class Class13
{
public bool $foo;


public function __construct(
private bool $bar = true,
) {
}


public function func(C|(X&D)|null $foo): (A&B)|null
{
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PhpGenerator/fixtures/classes.82.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

readonly class Class13
{
public bool $foo;


public function __construct(private bool $bar = true)
{
}


public function func(C|(X&D)|null $foo): (A&B)|null
{
}
Expand Down

0 comments on commit ea40f2f

Please sign in to comment.