Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| FunctionArgument | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createRandom | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| toTypescript | |
100.00% |
3 / 3 |
|
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% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto; |
| 3 | |
| 4 | use Apie\Core\Attributes\FakeMethod; |
| 5 | use Apie\TypescriptCodeBuilder\Enums\TypescriptType; |
| 6 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 7 | use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface; |
| 8 | use Apie\TypescriptCodeBuilder\TypescriptTypeDeclarationInterface; |
| 9 | use Apie\TypescriptCodeBuilder\ValueObjects\JavascriptIdentifier; |
| 10 | use Faker\Generator; |
| 11 | |
| 12 | #[FakeMethod('createRandom')] |
| 13 | class FunctionArgument implements TypescriptFileExpressionInterface |
| 14 | { |
| 15 | public function __construct( |
| 16 | public JavascriptIdentifier $name, |
| 17 | public ?TypescriptTypeDeclarationInterface $typehint = null, |
| 18 | public bool $optional = false, |
| 19 | ) { |
| 20 | } |
| 21 | |
| 22 | public static function createRandom(Generator $faker): self |
| 23 | { |
| 24 | return new self( |
| 25 | $faker->fakeClass(JavascriptIdentifier::class), |
| 26 | $faker->boolean(95) ? $faker->fakeClass(TypescriptType::class) : null, |
| 27 | $faker->boolean(5) |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | public function toTypescript(): string |
| 32 | { |
| 33 | if ($this->typehint === null) { |
| 34 | return $this->name->toNative() . ($this->optional ? '?: unknown' : ''); |
| 35 | } |
| 36 | return $this->name->toNative() . ($this->optional ? '?: ' : ': ') . $this->typehint->toTypescript(); |
| 37 | } |
| 38 | public function toJavascript(): string |
| 39 | { |
| 40 | return $this->name->toNative(); |
| 41 | } |
| 42 | public function providesDefinitions(): JavascriptIdentifierList |
| 43 | { |
| 44 | return new JavascriptIdentifierList(); |
| 45 | } |
| 46 | public function needsDefinitions(): JavascriptIdentifierList |
| 47 | { |
| 48 | return $this->typehint ? $this->typehint->needsDefinitions() : new JavascriptIdentifierList(); |
| 49 | } |
| 50 | } |