Skip to content

Commit

Permalink
Factory: sets readonly for promoted properties [Closes #158]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 12, 2024
1 parent a4930f8 commit a004aff
Show file tree
Hide file tree
Showing 3 changed files with 19 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 @@ -200,7 +200,7 @@ public function fromCallable(callable $from): Method|GlobalFunction|Closure
public function fromParameterReflection(\ReflectionParameter $from): Parameter
{
$param = $from->isPromoted()
? new PromotedParameter($from->name)
? (new PromotedParameter($from->name))->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->getDeclaringClass()->getProperty($from->name)->isReadonly() : false)
: new Parameter($from->name);
$param->setReference($from->isPassedByReference());
$param->setType((string) $from->getType());
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpGenerator/ClassType.from.81.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/classes.81.php';

$res[] = ClassType::from(new Abc\Class11);
$res[] = ClassType::from(Abc\Attr::class);
$res[] = ClassType::from(Abc\Class12::class);

sameFile(__DIR__ . '/expected/ClassType.from.81.expect', implode("\n", $res));
16 changes: 16 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.81.expect
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ class Class11
{
}
}

#[\Attribute]
class Attr
{
}

class Class12
{
private readonly string $bar;


public function __construct(
public readonly string $foo,
) {
}
}

0 comments on commit a004aff

Please sign in to comment.