Skip to content

Commit 0f1275b

Browse files
committed
added support for typed constants
1 parent b9ca0b4 commit 0f1275b

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ We can add constants (class [Constant](https://api.nette.org/php-generator/maste
7272
```php
7373
$class->addConstant('ID', 123)
7474
->setProtected() // constant visiblity
75+
->setType('int')
7576
->setFinal();
7677

7778
$class->addProperty('items', [1, 2, 3])
@@ -87,7 +88,7 @@ $class->addProperty('list')
8788
It generates:
8889

8990
```php
90-
final protected const ID = 123;
91+
final protected const int ID = 123;
9192

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

src/PhpGenerator/Constant.php

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class Constant
2525

2626
private mixed $value;
2727
private bool $final = false;
28+
private ?string $type = null;
2829

2930

3031
public function setValue(mixed $val): static
@@ -51,4 +52,17 @@ public function isFinal(): bool
5152
{
5253
return $this->final;
5354
}
55+
56+
57+
public function setType(?string $type): static
58+
{
59+
$this->type = Helpers::validateType($type);
60+
return $this;
61+
}
62+
63+
64+
public function getType(): ?string
65+
{
66+
return $this->type;
67+
}
5468
}

src/PhpGenerator/Helpers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static function createObject(string $class, array $props): object
165165
}
166166

167167

168-
public static function validateType(?string $type, bool &$nullable): ?string
168+
public static function validateType(?string $type, bool &$nullable = false): ?string
169169
{
170170
if ($type === '' || $type === null) {
171171
return null;

src/PhpGenerator/Printer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ public function printClass(
180180
foreach ($class->getConstants() as $const) {
181181
$def = ($const->isFinal() ? 'final ' : '')
182182
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
183-
. 'const ' . $const->getName() . ' = ';
183+
. 'const '
184+
. ltrim($this->printType($const->getType(), nullable: false) . ' ')
185+
. $const->getName() . ' = ';
184186

185187
$consts[] = $this->printDocComment($const)
186188
. $this->printAttributes($const->getAttributes())

tests/PhpGenerator/ClassType.phpt

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ $trait2 = $class->addTrait('AnotherTrait')
3939

4040
$class->addConstant('ROLE', 'admin');
4141
$class->addConstant('ACTIVE', false)
42-
->setFinal();
42+
->setFinal()
43+
->setType('bool');
4344

4445
Assert::false($class->isFinal());
4546
Assert::true($class->isAbstract());

tests/PhpGenerator/expected/ClassType.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class Example extends ParentClass implements IExample, IOne
1717
}
1818

1919
public const ROLE = 'admin';
20-
final public const ACTIVE = false;
20+
final public const bool ACTIVE = false;
2121

2222
/** Commented */
2323
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;

0 commit comments

Comments
 (0)