Skip to content

Commit d837e45

Browse files
committed
support for final promoted property in PHP 8.5
1 parent 012e3e4 commit d837e45

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nette/php-generator",
3-
"description": "🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.4 features.",
3+
"description": "🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.5 features.",
44
"keywords": ["nette", "php", "code", "scaffolding"],
55
"homepage": "https://nette.org",
66
"license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],

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
}

0 commit comments

Comments
 (0)