Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TernaryExpression | |
94.74% |
18 / 19 |
|
83.33% |
5 / 6 |
8.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toTypescript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toJavascript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| providesDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsDefinitions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| createRandom | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto\Expressions; |
| 3 | |
| 4 | use Apie\Core\Attributes\FakeMethod; |
| 5 | use Apie\TypescriptCodeBuilder\Enums\BinaryOperator; |
| 6 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 7 | use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface; |
| 8 | use Faker\Generator; |
| 9 | |
| 10 | #[FakeMethod('createRandom')] |
| 11 | class TernaryExpression implements TypescriptFileExpressionInterface |
| 12 | { |
| 13 | public function __construct( |
| 14 | public TypescriptFileExpressionInterface $condition, |
| 15 | public TypescriptFileExpressionInterface $whenTrue, |
| 16 | public TypescriptFileExpressionInterface $whenFalse, |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | public function toTypescript(): string |
| 21 | { |
| 22 | return $this->condition->toTypescript() . ' ? ' . $this->whenTrue->toTypescript() . ' : ' . $this->whenFalse->toTypescript(); |
| 23 | } |
| 24 | |
| 25 | public function toJavascript(): string |
| 26 | { |
| 27 | return $this->condition->toJavascript() . ' ? ' . $this->whenTrue->toJavascript() . ' : ' . $this->whenFalse->toJavascript(); |
| 28 | } |
| 29 | |
| 30 | public function providesDefinitions(): JavascriptIdentifierList |
| 31 | { |
| 32 | return new JavascriptIdentifierList(); |
| 33 | } |
| 34 | |
| 35 | public function needsDefinitions(): JavascriptIdentifierList |
| 36 | { |
| 37 | $definitions = new JavascriptIdentifierList(); |
| 38 | foreach ([$this->condition, $this->whenTrue, $this->whenFalse] as $expression) { |
| 39 | foreach ($expression->needsDefinitions() as $definition) { |
| 40 | $definitions = $definitions->append($definition); |
| 41 | } |
| 42 | } |
| 43 | return $definitions; |
| 44 | } |
| 45 | |
| 46 | public static function createRandom(Generator $faker): self |
| 47 | { |
| 48 | $condition = new BinaryOperationExpression( |
| 49 | $faker->fakeClass(IdentifierExpression::class), |
| 50 | $faker->randomElement([BinaryOperator::Equal, BinaryOperator::NotEqual]), |
| 51 | $faker->fakeClass(IdentifierExpression::class) |
| 52 | ); |
| 53 | return new self( |
| 54 | $condition, |
| 55 | $faker->fakeclass(IdentifierExpression::class), |
| 56 | $faker->fakeClass($faker->randomElement([IdentifierExpression::class, StringLiteralExpression::class, NullExpression::class])) |
| 57 | ); |
| 58 | } |
| 59 | } |