Skip to content

Commit 7f3798c

Browse files
committed
added support for readonly properties
1 parent c6f02b6 commit 7f3798c

File tree

11 files changed

+60
-5
lines changed

11 files changed

+60
-5
lines changed

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ public function __construct(
160160
}
161161
```
162162

163-
If the property, constant, method or parameter already exist, it will be overwritten.
163+
Readonly properties introduced by PHP 8.1 can be marked via `setReadOnly()`.
164+
165+
------
166+
167+
If the added property, constant, method or parameter already exist, it will be overwritten.
164168

165169
Members can be removed using `removeProperty()`, `removeConstant()`, `removeMethod()` or `removeParameter()`.
166170

src/PhpGenerator/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
244244
$prop->setType((string) $from->getType());
245245
}
246246
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
247+
$prop->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->isReadOnly() : false);
247248
} else {
248249
$prop->setInitialized(false);
249250
}

src/PhpGenerator/Printer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
152152
$properties = [];
153153
foreach ($class->getProperties() as $property) {
154154
$type = $property->getType();
155-
$def = (($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' '
155+
$def = (($property->getVisibility() ?: 'public')
156+
. ($property->isStatic() ? ' static' : '')
157+
. ($property->isReadOnly() && $type ? ' readonly' : '')
158+
. ' '
156159
. ltrim($this->printType($type, $property->isNullable(), $namespace) . ' ')
157160
. '$' . $property->getName());
158161

@@ -286,7 +289,10 @@ public function printParameters($function, PhpNamespace $namespace = null, int $
286289
$params[] =
287290
($promoted ? Helpers::formatDocComment((string) $promoted->getComment()) : '')
288291
. ($attrs = self::printAttributes($param->getAttributes(), $namespace, true))
289-
. ($promoted ? ($promoted->getVisibility() ?: 'public') . ' ' : '')
292+
. ($promoted ?
293+
($promoted->getVisibility() ?: 'public')
294+
. ($promoted->isReadOnly() && $type ? ' readonly' : '')
295+
. ' ' : '')
290296
. ltrim($this->printType($type, $param->isNullable(), $namespace) . ' ')
291297
. ($param->isReference() ? '&' : '')
292298
. ($variadic ? '...' : '')

src/PhpGenerator/PromotedParameter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,21 @@ final class PromotedParameter extends Parameter
1717
{
1818
use Traits\VisibilityAware;
1919
use Traits\CommentAware;
20+
21+
/** @var bool */
22+
private $readOnly = false;
23+
24+
25+
/** @return static */
26+
public function setReadOnly(bool $state = true): self
27+
{
28+
$this->readOnly = $state;
29+
return $this;
30+
}
31+
32+
33+
public function isReadOnly(): bool
34+
{
35+
return $this->readOnly;
36+
}
2037
}

src/PhpGenerator/Property.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ final class Property
4040
/** @var bool */
4141
private $initialized = false;
4242

43+
/** @var bool */
44+
private $readOnly = false;
45+
4346

4447
/** @return static */
4548
public function setValue($val): self
@@ -114,4 +117,18 @@ public function isInitialized(): bool
114117
{
115118
return $this->initialized || $this->value !== null;
116119
}
120+
121+
122+
/** @return static */
123+
public function setReadOnly(bool $state = true): self
124+
{
125+
$this->readOnly = $state;
126+
return $this;
127+
}
128+
129+
130+
public function isReadOnly(): bool
131+
{
132+
return $this->readOnly;
133+
}
117134
}

tests/PhpGenerator/ClassType.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ $class->addProperty('order')
5858
->setValue(new Literal('RecursiveIteratorIterator::SELF_FIRST'));
5959

6060
$class->addProperty('typed1')
61-
->setType(Type::ARRAY);
61+
->setType(Type::ARRAY)
62+
->setReadOnly();
6263

6364
$class->addProperty('typed2')
6465
->setType(Type::ARRAY)

tests/PhpGenerator/ClassType.promotion.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Nette\PhpGenerator\ClassType;
6+
use Nette\PhpGenerator\Literal;
67

78

89
require __DIR__ . '/../bootstrap.php';
@@ -18,4 +19,8 @@ $method->addPromotedParameter('c')
1819
->addComment('promo')
1920
->addAttribute('Example');
2021

22+
$method->addPromotedParameter('d', new Literal('new Draft'))
23+
->setType('Draft')
24+
->setReadOnly();
25+
2126
sameFile(__DIR__ . '/expected/ClassType.promotion.expect', (string) $class);

tests/PhpGenerator/expected/ClassType.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class Example extends ParentClass implements IExample, IOne
2020
/** @var resource orignal file handle */
2121
private $handle;
2222
public $order = RecursiveIteratorIterator::SELF_FIRST;
23-
public array $typed1;
23+
public readonly array $typed1;
2424
public ?array $typed2 = null;
2525
public array $typed3 = null;
2626
public static $sections = ['first' => true];

tests/PhpGenerator/expected/ClassType.from.81.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Class11
22
{
33
public Foo&Bar $foo;
4+
public readonly array $ro;
45

56

67
public function foo(Foo&Bar $c): Foo&Bar

tests/PhpGenerator/expected/ClassType.promotion.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Example
55
public $b,
66
/** promo */
77
#[Example] private string $c,
8+
public readonly Draft $d = new Draft,
89
) {
910
}
1011
}

0 commit comments

Comments
 (0)