Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| ConstantGenerator | |
100.00% |
29 / 29 |
|
100.00% |
10 / 10 |
16 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTyped | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isTyped | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Code\Generator; |
| 16 | |
| 17 | use Pop\Code\Generator\Support\ValueFormatter; |
| 18 | |
| 19 | /** |
| 20 | * Constant generator class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Code |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 6.0.0 |
| 28 | */ |
| 29 | class ConstantGenerator extends AbstractClassElementGenerator |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Constant type |
| 34 | * @var ?string |
| 35 | */ |
| 36 | protected ?string $type = null; |
| 37 | |
| 38 | /** |
| 39 | * Constant value |
| 40 | * @var mixed |
| 41 | */ |
| 42 | protected mixed $value = null; |
| 43 | |
| 44 | /** |
| 45 | * Typed-signature flag: when true and a type is set, render() emits the type in the signature |
| 46 | * @var bool |
| 47 | */ |
| 48 | protected bool $typed = false; |
| 49 | |
| 50 | /** |
| 51 | * Constructor |
| 52 | * |
| 53 | * Instantiate the constant generator object |
| 54 | * |
| 55 | * @param string $name |
| 56 | * @param string $type |
| 57 | * @param mixed $value |
| 58 | */ |
| 59 | public function __construct(string $name, string $type, mixed $value = null) |
| 60 | { |
| 61 | $this->setName($name); |
| 62 | $this->setType($type); |
| 63 | if ($value !== null) { |
| 64 | $this->setValue($value); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set the constant type |
| 70 | * |
| 71 | * @param string $type |
| 72 | * @return ConstantGenerator |
| 73 | */ |
| 74 | public function setType(string $type): ConstantGenerator |
| 75 | { |
| 76 | $this->type = $type; |
| 77 | return $this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the constant type |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public function getType(): string |
| 86 | { |
| 87 | return $this->type; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set the constant value |
| 92 | * |
| 93 | * @param mixed $value |
| 94 | * @return ConstantGenerator |
| 95 | */ |
| 96 | public function setValue(mixed $value = null): ConstantGenerator |
| 97 | { |
| 98 | $this->value = $value; |
| 99 | return $this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the constant value |
| 104 | * |
| 105 | * @return mixed |
| 106 | */ |
| 107 | public function getValue(): mixed |
| 108 | { |
| 109 | return $this->value; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Has constant value |
| 114 | * |
| 115 | * @return bool |
| 116 | */ |
| 117 | public function hasValue(): bool |
| 118 | { |
| 119 | return ($this->value !== null); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set whether the declared type is rendered in the signature (PHP 8.3+ typed constants) |
| 124 | * |
| 125 | * @param bool $typed |
| 126 | * @return ConstantGenerator |
| 127 | */ |
| 128 | public function setTyped(bool $typed = true): ConstantGenerator |
| 129 | { |
| 130 | $this->typed = $typed; |
| 131 | return $this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Determine if the declared type is rendered in the signature |
| 136 | * |
| 137 | * @return bool |
| 138 | */ |
| 139 | public function isTyped(): bool |
| 140 | { |
| 141 | return $this->typed; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Render constant |
| 146 | * |
| 147 | * @return string |
| 148 | */ |
| 149 | public function render(): string |
| 150 | { |
| 151 | if ($this->docblock === null) { |
| 152 | $this->docblock = new DocblockGenerator(null, $this->indent); |
| 153 | } |
| 154 | |
| 155 | $this->docblock->addTag('var', $this->type); |
| 156 | $this->output = PHP_EOL . $this->docblock->render(); |
| 157 | $this->output .= $this->formatAttributes(); |
| 158 | $this->output .= $this->printIndent() . $this->visibility . ' const'; |
| 159 | |
| 160 | if ($this->typed && ($this->type !== null)) { |
| 161 | $this->output .= ' ' . $this->type; |
| 162 | } |
| 163 | |
| 164 | $this->output .= ' ' . $this->name; |
| 165 | |
| 166 | if (($this->value === null) && ($this->type === 'array')) { |
| 167 | $val = '[]'; |
| 168 | } else { |
| 169 | $val = ValueFormatter::format($this->value, $this->type, $this->printIndent()); |
| 170 | } |
| 171 | $this->output .= ' = ' . $val . ';'; |
| 172 | |
| 173 | return $this->output; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Print constant |
| 178 | * |
| 179 | * @return string |
| 180 | */ |
| 181 | public function __toString(): string |
| 182 | { |
| 183 | return $this->render(); |
| 184 | } |
| 185 | |
| 186 | } |