Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
38.46% |
10 / 26 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ControlFlowUtils | |
38.46% |
10 / 26 |
|
40.00% |
2 / 5 |
67.44 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| indent | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| renderBlock | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| mergeDefinitions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| mergeCodeDefinitions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| blockDefinitions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Utils; |
| 3 | |
| 4 | use Apie\TypescriptCodeBuilder\Dto\ControlFlow\BlockStatement; |
| 5 | use Apie\TypescriptCodeBuilder\Lists\CodeList; |
| 6 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 7 | use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface; |
| 8 | |
| 9 | final class ControlFlowUtils |
| 10 | { |
| 11 | /** |
| 12 | * @codeCoverageIgnore |
| 13 | */ |
| 14 | private function __construct() |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | public static function indent(string $code): string |
| 19 | { |
| 20 | return implode(PHP_EOL, array_map( |
| 21 | static fn (string $line): string => ' ' . $line, |
| 22 | explode(PHP_EOL, $code), |
| 23 | )); |
| 24 | } |
| 25 | |
| 26 | public static function renderBlock(CodeList $codeList, bool $typescript): string |
| 27 | { |
| 28 | $lines = []; |
| 29 | foreach ($codeList as $code) { |
| 30 | $line = $typescript ? $code->toTypescript() : $code->toJavascript(); |
| 31 | if (trim($line)) { |
| 32 | $lines[] = $line; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return self::indent(implode(PHP_EOL, $lines)); |
| 37 | } |
| 38 | |
| 39 | public static function mergeDefinitions(TypescriptFileExpressionInterface ...$expressions): JavascriptIdentifierList |
| 40 | { |
| 41 | $definitions = new JavascriptIdentifierList(); |
| 42 | foreach ($expressions as $expression) { |
| 43 | foreach ($expression->needsDefinitions() as $definition) { |
| 44 | $definitions = $definitions->append($definition); |
| 45 | } |
| 46 | } |
| 47 | return $definitions; |
| 48 | } |
| 49 | |
| 50 | public static function mergeCodeDefinitions(CodeList $codeList): JavascriptIdentifierList |
| 51 | { |
| 52 | $definitions = new JavascriptIdentifierList(); |
| 53 | foreach ($codeList as $code) { |
| 54 | foreach ($code->needsDefinitions() as $definition) { |
| 55 | $definitions = $definitions->append($definition); |
| 56 | } |
| 57 | } |
| 58 | return $definitions; |
| 59 | } |
| 60 | |
| 61 | public static function blockDefinitions(BlockStatement $block): JavascriptIdentifierList |
| 62 | { |
| 63 | $definitions = new JavascriptIdentifierList(); |
| 64 | foreach ($block->codeList as $code) { |
| 65 | $provided = $code->providesDefinitions($code instanceof BlockStatement); |
| 66 | foreach ($provided as $definition) { |
| 67 | $definitions = $definitions->append($definition); |
| 68 | } |
| 69 | } |
| 70 | return $definitions; |
| 71 | } |
| 72 | } |