Skip to content

Commit

Permalink
added support for typed constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 13, 2023
1 parent b9ca0b4 commit 0f1275b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ We can add constants (class [Constant](https://api.nette.org/php-generator/maste
```php
$class->addConstant('ID', 123)
->setProtected() // constant visiblity
->setType('int')
->setFinal();

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

```php
final protected const ID = 123;
final protected const int ID = 123;

/** @var int[] */
private static $items = [1, 2, 3];
Expand Down
14 changes: 14 additions & 0 deletions src/PhpGenerator/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class Constant

private mixed $value;
private bool $final = false;
private ?string $type = null;


public function setValue(mixed $val): static
Expand All @@ -51,4 +52,17 @@ public function isFinal(): bool
{
return $this->final;
}


public function setType(?string $type): static
{
$this->type = Helpers::validateType($type);
return $this;
}


public function getType(): ?string
{
return $this->type;
}
}
2 changes: 1 addition & 1 deletion src/PhpGenerator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function createObject(string $class, array $props): object
}


public static function validateType(?string $type, bool &$nullable): ?string
public static function validateType(?string $type, bool &$nullable = false): ?string
{
if ($type === '' || $type === null) {
return null;
Expand Down
4 changes: 3 additions & 1 deletion src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ public function printClass(
foreach ($class->getConstants() as $const) {
$def = ($const->isFinal() ? 'final ' : '')
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
. 'const ' . $const->getName() . ' = ';
. 'const '
. ltrim($this->printType($const->getType(), nullable: false) . ' ')
. $const->getName() . ' = ';

$consts[] = $this->printDocComment($const)
. $this->printAttributes($const->getAttributes())
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpGenerator/ClassType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ $trait2 = $class->addTrait('AnotherTrait')

$class->addConstant('ROLE', 'admin');
$class->addConstant('ACTIVE', false)
->setFinal();
->setFinal()
->setType('bool');

Assert::false($class->isFinal());
Assert::true($class->isAbstract());
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/expected/ClassType.expect
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class Example extends ParentClass implements IExample, IOne
}

public const ROLE = 'admin';
final public const ACTIVE = false;
final public const bool ACTIVE = false;

/** Commented */
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
Expand Down

0 comments on commit 0f1275b

Please sign in to comment.