Skip to content

Commit ea40f2f

Browse files
committed
Factory: properties in readonly classes are not readonly
1 parent 43c9b67 commit ea40f2f

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

src/PhpGenerator/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
265265
$prop->setType((string) $from->getType());
266266

267267
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
268-
$prop->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->isReadOnly() : false);
268+
$prop->setReadOnly(PHP_VERSION_ID >= 80100 && $from->isReadOnly() && !(PHP_VERSION_ID >= 80200 && $from->getDeclaringClass()->isReadOnly()));
269269
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
270270
$prop->setAttributes($this->getAttributes($from));
271271
return $prop;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @phpVersion 8.2
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\ClassType;
10+
use Nette\PhpGenerator\Extractor;
11+
use Tester\Assert;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
require __DIR__ . '/fixtures/classes.82.php';
16+
17+
$class = ClassType::from(new Abc\Class13);
18+
Assert::false($class->getProperty('foo')->isReadOnly());
19+
Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());
20+
21+
$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.82.php')))->extractAll();
22+
$class = $file->getClasses()[Abc\Class13::class];
23+
Assert::false($class->getProperty('foo')->isReadOnly());
24+
Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());

tests/PhpGenerator/expected/ClassType.from.82.expect

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
readonly class Class13
22
{
3+
public bool $foo;
4+
5+
6+
public function __construct(
7+
public bool $bar = true,
8+
) {
9+
}
10+
11+
312
public function func(C|(X&D)|null $foo): (A&B)|null
413
{
514
}

tests/PhpGenerator/expected/Extractor.classes.82.expect

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ namespace Abc;
66

77
readonly class Class13
88
{
9+
public bool $foo;
10+
11+
12+
public function __construct(
13+
private bool $bar = true,
14+
) {
15+
}
16+
17+
918
public function func(C|(X&D)|null $foo): (A&B)|null
1019
{
1120
}

tests/PhpGenerator/fixtures/classes.82.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
readonly class Class13
88
{
9+
public bool $foo;
10+
11+
12+
public function __construct(private bool $bar = true)
13+
{
14+
}
15+
16+
917
public function func(C|(X&D)|null $foo): (A&B)|null
1018
{
1119
}

0 commit comments

Comments
 (0)