Skip to content

Commit

Permalink
Extractor: added support for Readonly promoted parameters (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coffelius authored and dg committed May 23, 2022
1 parent b9ba414 commit 3a84a19
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ private function setupFunction($function, Node\FunctionLike $node): void
$function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
foreach ($node->getParams() as $item) {
$visibility = $this->toVisibility($item->flags);
$isReadonly = (bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY);
$param = $visibility
? ($function->addPromotedParameter($item->var->name))->setVisibility($visibility)
? ($function->addPromotedParameter($item->var->name))->setVisibility($visibility)->setReadonly($isReadonly)
: $function->addParameter($item->var->name);
$param->setType($item->type ? $this->toPhp($item->type) : null);
$param->setReference($item->byRef);
Expand Down
11 changes: 11 additions & 0 deletions tests/PhpGenerator/expected/Extractor.classes.81.expect
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ class Class11
class Attr
{
}

class Class12
{
private readonly string $bar;


public function __construct(private readonly string $foo)
{
$this->bar = "foobar";
}
}
11 changes: 11 additions & 0 deletions tests/PhpGenerator/fixtures/classes.81.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ public function bar($c = new \stdClass)
class Attr
{
}

class Class12
{
private readonly string $bar;


public function __construct(private readonly string $foo)
{
$this->bar = "foobar";
}
}

0 comments on commit 3a84a19

Please sign in to comment.