Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| InterfaceDefinition | |
100.00% |
18 / 18 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toTypescript | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| toJavascript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| providesDefinitions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| needsDefinitions | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto\Typehints; |
| 3 | |
| 4 | use Apie\TypescriptCodeBuilder\Dto\FunctionArgument; |
| 5 | use Apie\TypescriptCodeBuilder\Lists\ArgumentList; |
| 6 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 7 | use Apie\TypescriptCodeBuilder\TypescriptTypeDeclarationInterface; |
| 8 | use Apie\TypescriptCodeBuilder\Utils\ControlFlowUtils; |
| 9 | use Apie\TypescriptCodeBuilder\ValueObjects\JavascriptIdentifier; |
| 10 | |
| 11 | /** |
| 12 | * Typescript definition for an interface. |
| 13 | */ |
| 14 | class InterfaceDefinition implements TypescriptTypeDeclarationInterface |
| 15 | { |
| 16 | public function __construct( |
| 17 | public JavascriptIdentifier $name, |
| 18 | public ArgumentList $properties, |
| 19 | public ?JavascriptIdentifierList $extends = null |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | public function toTypescript(): string |
| 24 | { |
| 25 | $properties = []; |
| 26 | foreach ($this->properties as $property) { |
| 27 | $properties[] = $property->toTypescript() . ';'; |
| 28 | } |
| 29 | $extends = ''; |
| 30 | if ($this->extends && $this->extends->count()) { |
| 31 | $extends = ' extends ' . implode(', ', $this->extends->toStringArray()); |
| 32 | } |
| 33 | return 'interface ' . $this->name->toNative() . $extends . ' {' . PHP_EOL . ControlFlowUtils::indent(implode(PHP_EOL, $properties)) . PHP_EOL . '}'; |
| 34 | } |
| 35 | |
| 36 | public function toJavascript(): string |
| 37 | { |
| 38 | return ''; |
| 39 | } |
| 40 | |
| 41 | public function providesDefinitions(bool $applyBlockScope): JavascriptIdentifierList |
| 42 | { |
| 43 | return new JavascriptIdentifierList([$this->name]); |
| 44 | } |
| 45 | |
| 46 | public function needsDefinitions(): JavascriptIdentifierList |
| 47 | { |
| 48 | $definitions = new JavascriptIdentifierList(); |
| 49 | foreach ($this->properties as $property) { |
| 50 | /** @var FunctionArgument $property */ |
| 51 | foreach ($property->needsDefinitions() as $definition) { |
| 52 | $definitions = $definitions->append($definition); |
| 53 | } |
| 54 | } |
| 55 | if ($this->extends) { |
| 56 | foreach ($this->extends as $implement) { |
| 57 | $definitions = $definitions->append($implement); |
| 58 | } |
| 59 | } |
| 60 | return $definitions; |
| 61 | } |
| 62 | } |