Skip to content

Commit ed215d8

Browse files
committed
added support for final constants
1 parent 7f3798c commit ed215d8

File tree

8 files changed

+32
-5
lines changed

8 files changed

+32
-5
lines changed

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ We can add constants ([Constant](https://api.nette.org/3.0/Nette/PhpGenerator/Co
8787

8888
```php
8989
$class->addConstant('ID', 123)
90-
->setPrivate(); // constant visiblity
90+
->setProtected() // constant visiblity
91+
->setFinal();
9192

9293
$class->addProperty('items', [1, 2, 3])
9394
->setPrivate() // or setVisibility('private')
@@ -103,7 +104,7 @@ $class->addProperty('list')
103104
It generates:
104105

105106
```php
106-
private const ID = 123;
107+
final protected const ID = 123;
107108

108109
/** @var int[] */
109110
private static $items = [1, 2, 3];

src/PhpGenerator/Constant.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ final class Constant
2626
/** @var mixed */
2727
private $value;
2828

29+
/** @var bool */
30+
private $final = false;
31+
2932

3033
/** @return static */
3134
public function setValue($val): self
@@ -39,4 +42,18 @@ public function getValue()
3942
{
4043
return $this->value;
4144
}
45+
46+
47+
/** @return static */
48+
public function setFinal(bool $state = true): self
49+
{
50+
$this->final = $state;
51+
return $this;
52+
}
53+
54+
55+
public function isFinal(): bool
56+
{
57+
return $this->final;
58+
}
4259
}

src/PhpGenerator/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant
206206
? ClassType::VISIBILITY_PRIVATE
207207
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
208208
);
209+
$const->setFinal(PHP_VERSION_ID >= 80100 ? $from->isFinal() : false);
209210
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
210211
$const->setAttributes(self::getAttributes($from));
211212
return $const;

src/PhpGenerator/Printer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
142142

143143
$consts = [];
144144
foreach ($class->getConstants() as $const) {
145-
$def = ($const->getVisibility() ? $const->getVisibility() . ' ' : '') . 'const ' . $const->getName() . ' = ';
145+
$def = ($const->isFinal() ? 'final ' : '')
146+
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
147+
. 'const ' . $const->getName() . ' = ';
148+
146149
$consts[] = Helpers::formatDocComment((string) $const->getComment())
147150
. self::printAttributes($const->getAttributes(), $namespace)
148151
. $def

tests/PhpGenerator/ClassType.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ $class
3636
->addComment("Description of class.\nThis is example\n")
3737
->addComment('@property-read Nette\Forms\Form $form')
3838
->setConstants(['ROLE' => 'admin'])
39-
->addConstant('ACTIVE', false);
39+
->addConstant('ACTIVE', false)
40+
->setFinal();
4041

4142
Assert::false($class->isFinal());
4243
Assert::true($class->isAbstract());

tests/PhpGenerator/expected/ClassType.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class Example extends ParentClass implements IExample, IOne
1212
}
1313

1414
const ROLE = 'admin';
15-
const ACTIVE = false;
15+
final const ACTIVE = false;
1616

1717
/** Commented */
1818
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Class11
22
{
3+
final public const FOO = 10;
4+
35
public Foo&Bar $foo;
46
public readonly array $ro;
57

tests/PhpGenerator/fixtures/classes.php81

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Abc;
66

77
class Class11
88
{
9+
final public const FOO = 10;
10+
911
public Foo&Bar $foo;
1012

1113
public readonly array $ro;

0 commit comments

Comments
 (0)