Skip to content

Commit 95591e5

Browse files
committed
Add type support to constants
1 parent 0b76c28 commit 95591e5

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

src/PhpConstant.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Stefna\PhpCodeBuilder;
44

55
use Stefna\PhpCodeBuilder\ValueObject\Identifier;
6+
use Stefna\PhpCodeBuilder\ValueObject\Type;
67

78
class PhpConstant
89
{
@@ -14,26 +15,27 @@ class PhpConstant
1415
public const CASE_LOWER = 1;
1516
public const CASE_NONE = 2;
1617

17-
public static function public(string $identifier, mixed $value = null): self
18+
public static function public(string $identifier, mixed $value = null, ?Type $type = null): self
1819
{
19-
return new self(self::PUBLIC_ACCESS, $identifier, $value);
20+
return new self(self::PUBLIC_ACCESS, $identifier, $value, type: $type);
2021
}
2122

22-
public static function private(string $identifier, mixed $value = null): self
23+
public static function private(string $identifier, mixed $value = null, ?Type $type = null): self
2324
{
24-
return new self(self::PRIVATE_ACCESS, $identifier, $value);
25+
return new self(self::PRIVATE_ACCESS, $identifier, $value, type: $type);
2526
}
2627

27-
public static function protected(string $identifier, mixed $value = null): self
28+
public static function protected(string $identifier, mixed $value = null, ?Type $type = null): self
2829
{
29-
return new self(self::PROTECTED_ACCESS, $identifier, $value);
30+
return new self(self::PROTECTED_ACCESS, $identifier, $value, type: $type);
3031
}
3132

3233
public function __construct(
3334
protected string $access,
3435
protected string $identifier,
3536
protected mixed $value = null,
3637
protected int $case = self::CASE_UPPER,
38+
protected ?Type $type = null,
3739
) {}
3840

3941
public function getName(): string
@@ -83,6 +85,11 @@ public function getAccess(): string
8385
return $this->access;
8486
}
8587

88+
public function getType(): ?Type
89+
{
90+
return $this->type;
91+
}
92+
8693
public function setAccess(string $access): static
8794
{
8895
$this->access = $access;

src/Renderer/Php84Renderer.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stefna\PhpCodeBuilder\Renderer;
4+
5+
use Stefna\PhpCodeBuilder\FlattenSource;
6+
use Stefna\PhpCodeBuilder\FormatValue;
7+
use Stefna\PhpCodeBuilder\PhpClass;
8+
use Stefna\PhpCodeBuilder\PhpConstant;
9+
use Stefna\PhpCodeBuilder\PhpEnum;
10+
use Stefna\PhpCodeBuilder\PhpMethod;
11+
use Stefna\PhpCodeBuilder\PhpParam;
12+
use Stefna\PhpCodeBuilder\PhpTrait;
13+
use Stefna\PhpCodeBuilder\PhpVariable;
14+
use Stefna\PhpCodeBuilder\ValueObject\EnumBackedCase;
15+
use Stefna\PhpCodeBuilder\ValueObject\Identifier;
16+
use Stefna\PhpCodeBuilder\ValueObject\Type;
17+
18+
class Php84Renderer extends Php82Renderer
19+
{
20+
public function renderConstant(PhpConstant $constant): array
21+
{
22+
$ret = [];
23+
$line = [];
24+
$access = $constant->getAccess();
25+
if ($access) {
26+
$line[] = $access;
27+
}
28+
29+
$line[] = 'const';
30+
$type = $this->formatTypeHint($constant->getType());
31+
if ($type) {
32+
$line[] = $type;
33+
}
34+
$line[] = $constant->getName();
35+
$line[] = '=';
36+
$lineStr = implode(' ', $line);
37+
38+
$value = FormatValue::format($constant->getValue());
39+
if (is_array($value)) {
40+
if (count($value) > 1) {
41+
$lineStr .= ' ' . array_shift($value);
42+
$ret[] = $lineStr;
43+
}
44+
$ret = FlattenSource::applySourceOn($value, $ret);
45+
$lastKey = (int)array_key_last($ret);
46+
if (is_string($ret[$lastKey])) {
47+
$ret[$lastKey] .= ';';
48+
}
49+
}
50+
else {
51+
$lineStr .= ' ' . $value;
52+
$ret[] = $lineStr . ';';
53+
}
54+
55+
return $ret;
56+
}
57+
}

tests/Renderer/PhpConstantTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PHPUnit\Framework\TestCase;
66
use Stefna\PhpCodeBuilder\PhpConstant;
77
use Stefna\PhpCodeBuilder\Renderer\Php7Renderer;
8+
use Stefna\PhpCodeBuilder\Renderer\Php84Renderer;
9+
use Stefna\PhpCodeBuilder\ValueObject\Type;
810

911
final class PhpConstantTest extends TestCase
1012
{
@@ -108,4 +110,12 @@ public function testSanitizeConstNameFromSpecialCharacters(): void
108110

109111
$this->assertSame(['public const _S_2M = \'>2m\';'], $render->renderConstant($const));
110112
}
113+
114+
public function testTypedConstants(): void
115+
{
116+
$const = PhpConstant::public('NAME', '>2m', type: Type::fromString('string'));
117+
$render = new Php84Renderer();
118+
119+
$this->assertSame(['public const string NAME = \'>2m\';'], $render->renderConstant($const));
120+
}
111121
}

0 commit comments

Comments
 (0)