Passed
Push — develop ( 9b8c4e...79e7ae )
by Andrea Marco
03:34 queued 14s
created

output()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 18
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum;
6
7
use Closure;
8
use Illuminate\Console\OutputStyle;
9
use Throwable;
10
11
/**
12
 * Output the outcome of the given enum operation.
13
 *
14
 * @param class-string<\UnitEnum> $namespace
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\UnitEnum> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\UnitEnum>.
Loading history...
15
 * @param Closure(): bool $callback
16
 */
17
function output(OutputStyle $output, string $enum, Closure $callback): bool
18
{
19
    $error = null;
20
21
    try {
22
        $succeeded = $callback();
23
    } catch (Throwable $e) {
24
        $succeeded = false;
25
        $error = "\e[38;2;220;38;38m{$e?->getMessage()}\e[0m";
26
    }
27
28
    if ($succeeded) {
29
        $output->writeln("\e[48;2;163;230;53m\e[38;2;63;98;18m\e[1m DONE \e[0m {$enum}" . PHP_EOL);
30
    } else {
31
        $output->writeln("\e[48;2;248;113;113m\e[38;2;153;27;27m\e[1m FAIL \e[0m {$enum} {$error}" . PHP_EOL);
32
    }
33
34
    return $succeeded;
35
}
36
37
/**
38
 * Annotate the given enum in a new process.
39
 *
40
 * @param class-string<\UnitEnum> $enum
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\UnitEnum> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\UnitEnum>.
Loading history...
41
 */
42
function runAnnotate(string $enum, bool $force = false): bool
43
{
44
    return runEnum("annotate \"{$enum}\"" . ($force ? ' --force' : ''));
45
}
46
47
/**
48
 * Run the enum CLI in a new process.
49
 */
50
function runEnum(string $command): bool
51
{
52
    // Once an enum is loaded, PHP accesses it from the memory and not from the disk.
53
    // Since our commands write on disk, the enum in memory might get out of sync.
54
    // To make sure that we are dealing with the current contents of such enum,
55
    // we spin a new process to load the latest state of the enum in memory.
56
    $cmd = vsprintf('"%s" "%s" %s 2>&1', [
57
        PHP_BINARY,
58
        Enums::basePath('artisan'),
59
        "enum:{$command}",
60
    ]);
61
62
    ob_start();
63
64
    $succeeded = passthru($cmd, $status) === null;
65
66
    ob_end_clean();
67
68
    return $succeeded;
69
}
70
71
/**
72
 * Synchronize the given enum in TypeScript within a new process.
73
 *
74
 * @param class-string<\UnitEnum> $enum
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\UnitEnum> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\UnitEnum>.
Loading history...
75
 */
76
function runTs(string $enum, bool $force = false): bool
77
{
78
    return runEnum("ts \"{$enum}\"" . ($force ? ' --force' : ''));
79
}
80