Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
25 / 30 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| NamedFunction | |
83.33% |
25 / 30 |
|
80.00% |
4 / 5 |
19.50 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toTypescript | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| toJavascript | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| providesDefinitions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| needsDefinitions | |
64.29% |
9 / 14 |
|
0.00% |
0 / 1 |
10.92 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto; |
| 3 | |
| 4 | use Apie\TypescriptCodeBuilder\Lists\ArgumentList; |
| 5 | use Apie\TypescriptCodeBuilder\Lists\CodeList; |
| 6 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 7 | use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface; |
| 8 | use Apie\TypescriptCodeBuilder\ValueObjects\JavascriptIdentifier; |
| 9 | |
| 10 | class NamedFunction implements TypescriptFileExpressionInterface |
| 11 | { |
| 12 | public function __construct( |
| 13 | public JavascriptIdentifier $name, |
| 14 | public ArgumentList $arguments, |
| 15 | public CodeList $codeList |
| 16 | ) { |
| 17 | } |
| 18 | |
| 19 | public function toTypescript(): string |
| 20 | { |
| 21 | $list = []; |
| 22 | foreach ($this->codeList as $code) { |
| 23 | $codeRow = $code->toTypescript(); |
| 24 | if ($codeRow) { |
| 25 | $list[] = $codeRow; |
| 26 | } |
| 27 | } |
| 28 | $firstPrefix = empty($list) ? '' : ' '; |
| 29 | return 'function ' . $this->name . '(' . $this->arguments->toTypescript() . ') {' . PHP_EOL . $firstPrefix . implode(PHP_EOL . ' ', $list) . PHP_EOL . '}'; |
| 30 | } |
| 31 | public function toJavascript(): string |
| 32 | { |
| 33 | $list = []; |
| 34 | foreach ($this->codeList as $code) { |
| 35 | $codeRow = $code->toJavascript(); |
| 36 | if ($codeRow) { |
| 37 | $list[] = $codeRow; |
| 38 | } |
| 39 | } |
| 40 | $firstPrefix = empty($list) ? '' : ' '; |
| 41 | return 'function ' . $this->name . '(' . $this->arguments->toJavascript() . ') {' . PHP_EOL . $firstPrefix . implode(PHP_EOL . ' ', $list) . PHP_EOL . '}'; |
| 42 | } |
| 43 | |
| 44 | public function providesDefinitions(): JavascriptIdentifierList |
| 45 | { |
| 46 | return new JavascriptIdentifierList([$this->name]); |
| 47 | } |
| 48 | public function needsDefinitions(): JavascriptIdentifierList |
| 49 | { |
| 50 | $provides = []; |
| 51 | foreach ($this->codeList as $code) { |
| 52 | foreach ($code->providesDefinitions() as $definition) { |
| 53 | $provides[$definition->toNative()] = true; |
| 54 | } |
| 55 | } |
| 56 | $list = []; |
| 57 | foreach ($this->codeList as $code) { |
| 58 | foreach ($code->needsDefinitions() as $definition) { |
| 59 | $key = $definition->toNative(); |
| 60 | if (empty($provides[$key])) { |
| 61 | $list[$key] = $definition; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | foreach ($this->arguments as $argument) { |
| 66 | foreach ($argument->needsDefinitions() as $definition) { |
| 67 | $list[$definition->toNative()] = $definition; |
| 68 | } |
| 69 | } |
| 70 | return new JavascriptIdentifierList($list); |
| 71 | } |
| 72 | } |