Skip to content

Commit 69b41f5

Browse files
committed
added support for final promoted property
1 parent 4c484a9 commit 69b41f5

File tree

9 files changed

+52
-18
lines changed

9 files changed

+52
-18
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,12 @@ private function setupFunction(GlobalFunction|Method|PropertyHook $function, Nod
476476
foreach ($node->getParams() as $item) {
477477
$getVisibility = $this->toVisibility($item->flags);
478478
$setVisibility = $this->toSetterVisibility($item->flags);
479-
if ($getVisibility || $setVisibility) {
479+
$final = (bool) ($item->flags & Modifiers::FINAL);
480+
if ($getVisibility || $setVisibility || $final) {
480481
$param = $function->addPromotedParameter($item->var->name)
481482
->setVisibility($getVisibility, $setVisibility)
482-
->setReadonly((bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY));
483+
->setReadonly((bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY))
484+
->setFinal($final);
483485
$this->addHooksToProperty($param, $item);
484486
} else {
485487
$param = $function->addParameter($item->var->name);

src/PhpGenerator/Factory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
211211
$property = $from->getDeclaringClass()->getProperty($from->name);
212212
$param = (new PromotedParameter($from->name))
213213
->setVisibility($this->getVisibility($property))
214-
->setReadOnly(PHP_VERSION_ID >= 80100 && $property->isReadonly());
214+
->setReadOnly(PHP_VERSION_ID >= 80100 && $property->isReadonly())
215+
->setFinal(PHP_VERSION_ID >= 80500 && $property->isFinal() && !$property->isPrivateSet());
215216
$this->addHooks($property, $param);
216217
} else {
217218
$param = new Parameter($from->name);

src/PhpGenerator/Printer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,10 @@ private function formatParameters(Closure|GlobalFunction|Method|PropertyHook $fu
345345
$this->printDocComment($param)
346346
. ($attrs ? ($multiline ? substr($attrs, 0, -1) . "\n" : $attrs) : '')
347347
. ($param instanceof PromotedParameter
348-
? $this->printPropertyVisibility($param) . ($param->isReadOnly() && $param->getType() ? ' readonly' : '') . ' '
348+
? ($param->isFinal() ? 'final ' : '')
349+
. $this->printPropertyVisibility($param)
350+
. ($param->isReadOnly() && $param->getType() ? ' readonly' : '')
351+
. ' '
349352
: '')
350353
. ltrim($this->printType($param->getType(), $param->isNullable()) . ' ')
351354
. ($param->isReference() ? '&' : '')

src/PhpGenerator/Property.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ final class Property
2828
private ?string $type = null;
2929
private bool $nullable = false;
3030
private bool $initialized = false;
31-
private bool $final = false;
3231
private bool $abstract = false;
3332

3433

@@ -101,19 +100,6 @@ public function isInitialized(): bool
101100
}
102101

103102

104-
public function setFinal(bool $state = true): static
105-
{
106-
$this->final = $state;
107-
return $this;
108-
}
109-
110-
111-
public function isFinal(): bool
112-
{
113-
return $this->final;
114-
}
115-
116-
117103
public function setAbstract(bool $state = true): static
118104
{
119105
$this->abstract = $state;

src/PhpGenerator/Traits/PropertyLike.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ trait PropertyLike
2323
{
2424
/** @var array{'set' => ?string, 'get' => ?string} */
2525
private array $visibility = [PropertyAccessMode::Set => null, PropertyAccessMode::Get => null];
26+
private bool $final = false;
2627
private bool $readOnly = false;
2728

2829
/** @var array<string, ?PropertyHook> */
@@ -95,6 +96,19 @@ public function isPrivate(string $mode = PropertyAccessMode::Get): bool
9596
}
9697

9798

99+
public function setFinal(bool $state = true): static
100+
{
101+
$this->final = $state;
102+
return $this;
103+
}
104+
105+
106+
public function isFinal(): bool
107+
{
108+
return $this->final;
109+
}
110+
111+
98112
public function setReadOnly(bool $state = true): static
99113
{
100114
$this->readOnly = $state;

tests/PhpGenerator/PropertyLike.hooks.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ $method->addPromotedParameter('second')
7979
->addParameter('value')
8080
->setType('string');
8181

82+
$method->addPromotedParameter('third')
83+
->setPublic()
84+
->setProtected('set')
85+
->setFinal()
86+
->setType('string')
87+
->addComment('hello')
88+
->addAttribute('Example');
89+
8290
same(<<<'XX'
8391
class Demo
8492
{
@@ -91,6 +99,9 @@ same(<<<'XX'
9199
public string $second {
92100
final set(string $value) => $value;
93101
},
102+
/** hello */
103+
#[Example]
104+
final public protected(set) string $third,
94105
) {
95106
}
96107
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
class Class85
22
{
33
private(set) static string $foo;
4+
5+
6+
public function __construct(
7+
final public $final,
8+
) {
9+
}
410
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ namespace Abc;
77
class Class85
88
{
99
private(set) static string $foo;
10+
11+
12+
function __construct(
13+
final public $final,
14+
) {
15+
}
1016
}

tests/PhpGenerator/fixtures/classes.85.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
class Class85
88
{
99
private(set) static string $foo;
10+
11+
function __construct(
12+
final $final,
13+
) {
14+
}
1015
}

0 commit comments

Comments
 (0)