Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.47% |
26 / 34 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
| BinaryOperationExpression | |
76.47% |
26 / 34 |
|
42.86% |
3 / 7 |
17.93 | |
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 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| providesDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| merge | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| createRandom | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
7 | |||
| 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 BinaryOperationExpression implements TypescriptFileExpressionInterface |
| 12 | { |
| 13 | public function __construct( |
| 14 | public TypescriptFileExpressionInterface $left, |
| 15 | public BinaryOperator $operator, |
| 16 | public TypescriptFileExpressionInterface $right, |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | public function toTypescript(): string |
| 21 | { |
| 22 | return $this->left->toTypescript() . ' ' . $this->operator->value . ' ' . $this->right->toTypescript(); |
| 23 | } |
| 24 | public function toJavascript(): string |
| 25 | { |
| 26 | return $this->left->toJavascript() . ' ' . $this->operator->value . ' ' . $this->right->toJavascript(); |
| 27 | } |
| 28 | public function providesDefinitions(): JavascriptIdentifierList |
| 29 | { |
| 30 | return new JavascriptIdentifierList(); |
| 31 | } |
| 32 | public function needsDefinitions(): JavascriptIdentifierList |
| 33 | { |
| 34 | return $this->merge($this->left, $this->right); |
| 35 | } |
| 36 | |
| 37 | private function merge(TypescriptFileExpressionInterface ...$expressions): JavascriptIdentifierList |
| 38 | { |
| 39 | $definitions = new JavascriptIdentifierList(); |
| 40 | foreach ($expressions as $expression) { |
| 41 | foreach ($expression->needsDefinitions() as $definition) { |
| 42 | $definitions = $definitions->append($definition); |
| 43 | } |
| 44 | } |
| 45 | return $definitions; |
| 46 | } |
| 47 | |
| 48 | public static function createRandom(Generator $faker): self |
| 49 | { |
| 50 | $operator = $faker->fakeClass(BinaryOperator::class); |
| 51 | $options = []; |
| 52 | if ($operator->allowsNumbers()) { |
| 53 | $options[] = function () use ($faker) { |
| 54 | return new NumberLiteralExpression($faker->randomNumber()); |
| 55 | }; |
| 56 | } |
| 57 | if ($operator->allowsBoolean()) { |
| 58 | $options[] = function () use ($faker) { |
| 59 | return new BooleanLiteralExpression($faker->boolean()); |
| 60 | }; |
| 61 | } |
| 62 | if ($operator->allowsNull()) { |
| 63 | $options[] = function () use ($faker) { |
| 64 | return new NullExpression(); |
| 65 | }; |
| 66 | } |
| 67 | if ($operator->allowsString()) { |
| 68 | $options[] = function () use ($faker) { |
| 69 | return new StringLiteralExpression($faker->word()); |
| 70 | }; |
| 71 | } |
| 72 | $pickedOption = $faker->randomElement($options); |
| 73 | return new self( |
| 74 | $faker->boolean() ? $pickedOption() : $faker->fakeClass(IdentifierExpression::class), |
| 75 | $operator, |
| 76 | $faker->boolean() ? $pickedOption() : $faker->fakeClass(IdentifierExpression::class), |
| 77 | ); |
| 78 | } |
| 79 | } |