|
4 | 4 |
|
5 | 5 | namespace TypeLang\Parser\Node\Stmt\Callable; |
6 | 6 |
|
7 | | -class CallableParameterNode extends ParameterNode {} |
| 7 | +use TypeLang\Parser\Node\Literal\VariableLiteralNode; |
| 8 | +use TypeLang\Parser\Node\Node; |
| 9 | +use TypeLang\Parser\Node\Stmt\Attribute\AttributeGroupsListNode; |
| 10 | +use TypeLang\Parser\Node\Stmt\TypeStatement; |
| 11 | + |
| 12 | +class CallableParameterNode extends Node implements \Stringable |
| 13 | +{ |
| 14 | + public function __construct( |
| 15 | + public ?TypeStatement $type = null, |
| 16 | + public ?VariableLiteralNode $name = null, |
| 17 | + public bool $output = false, |
| 18 | + public bool $variadic = false, |
| 19 | + public bool $optional = false, |
| 20 | + public ?AttributeGroupsListNode $attributes = null, |
| 21 | + ) { |
| 22 | + assert($type !== null || $name !== null, new \TypeError( |
| 23 | + 'Required indication of the type or name of the parameter (one of)', |
| 24 | + )); |
| 25 | + |
| 26 | + assert($variadic === false || $optional === false, new \TypeError( |
| 27 | + 'Parameter cannot be both variable and optional (variadic parameter is already optional)', |
| 28 | + )); |
| 29 | + } |
| 30 | + |
| 31 | + public function __toString(): string |
| 32 | + { |
| 33 | + $result = []; |
| 34 | + |
| 35 | + if ($this->output) { |
| 36 | + $result[] = 'output'; |
| 37 | + } |
| 38 | + |
| 39 | + if ($this->variadic) { |
| 40 | + $result[] = 'variadic'; |
| 41 | + } |
| 42 | + |
| 43 | + if ($this->optional) { |
| 44 | + $result[] = 'optional'; |
| 45 | + } |
| 46 | + |
| 47 | + if ($result === []) { |
| 48 | + return 'simple'; |
| 49 | + } |
| 50 | + |
| 51 | + return \implode(', ', $result); |
| 52 | + } |
| 53 | +} |
0 commit comments