Skip to content

Commit a004aff

Browse files
committed
Factory: sets readonly for promoted properties [Closes #158]
1 parent a4930f8 commit a004aff

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/PhpGenerator/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function fromCallable(callable $from): Method|GlobalFunction|Closure
200200
public function fromParameterReflection(\ReflectionParameter $from): Parameter
201201
{
202202
$param = $from->isPromoted()
203-
? new PromotedParameter($from->name)
203+
? (new PromotedParameter($from->name))->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->getDeclaringClass()->getProperty($from->name)->isReadonly() : false)
204204
: new Parameter($from->name);
205205
$param->setReference($from->isPassedByReference());
206206
$param->setType((string) $from->getType());

tests/PhpGenerator/ClassType.from.81.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ require __DIR__ . '/../bootstrap.php';
1212
require __DIR__ . '/fixtures/classes.81.php';
1313

1414
$res[] = ClassType::from(new Abc\Class11);
15+
$res[] = ClassType::from(Abc\Attr::class);
16+
$res[] = ClassType::from(Abc\Class12::class);
1517

1618
sameFile(__DIR__ . '/expected/ClassType.from.81.expect', implode("\n", $res));

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,19 @@ class Class11
1616
{
1717
}
1818
}
19+
20+
#[\Attribute]
21+
class Attr
22+
{
23+
}
24+
25+
class Class12
26+
{
27+
private readonly string $bar;
28+
29+
30+
public function __construct(
31+
public readonly string $foo,
32+
) {
33+
}
34+
}

0 commit comments

Comments
 (0)