Skip to content

Commit 3a84a19

Browse files
Coffeliusdg
authored andcommitted
Extractor: added support for Readonly promoted parameters (#111)
1 parent b9ba414 commit 3a84a19

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,9 @@ private function setupFunction($function, Node\FunctionLike $node): void
389389
$function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
390390
foreach ($node->getParams() as $item) {
391391
$visibility = $this->toVisibility($item->flags);
392+
$isReadonly = (bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY);
392393
$param = $visibility
393-
? ($function->addPromotedParameter($item->var->name))->setVisibility($visibility)
394+
? ($function->addPromotedParameter($item->var->name))->setVisibility($visibility)->setReadonly($isReadonly)
394395
: $function->addParameter($item->var->name);
395396
$param->setType($item->type ? $this->toPhp($item->type) : null);
396397
$param->setReference($item->byRef);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ class Class11
2727
class Attr
2828
{
2929
}
30+
31+
class Class12
32+
{
33+
private readonly string $bar;
34+
35+
36+
public function __construct(private readonly string $foo)
37+
{
38+
$this->bar = "foobar";
39+
}
40+
}

tests/PhpGenerator/fixtures/classes.81.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ public function bar($c = new \stdClass)
2727
class Attr
2828
{
2929
}
30+
31+
class Class12
32+
{
33+
private readonly string $bar;
34+
35+
36+
public function __construct(private readonly string $foo)
37+
{
38+
$this->bar = "foobar";
39+
}
40+
}

0 commit comments

Comments
 (0)