Passed
Push — master ( f833b1...244bf6 )
by Timm
02:07
created

CmdRunner::eol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stefaminator\Cli;
5
6
7
abstract class CmdRunner {
8
9
    /**
10
     * @var Cmd
11
     */
12
    private $cmd;
13
14
    /**
15
     * CmdRunner constructor.
16
     * @param Cmd $cmd
17
     */
18 20
    public function __construct(Cmd $cmd = null) {
19 20
        if($cmd !== null) {
20 3
            $this->cmd = $cmd;
21 3
            return;
22
        }
23 20
        $this->cmd = Cmd::root();
24 20
    }
25
26
    /**
27
     * @return Cmd
28
     */
29 4
    public function getCmd(): Cmd {
30 4
        return $this->cmd;
31
    }
32
33
    /**
34
     * @param Cmd $cmd
35
     */
36 20
    public function setCmd(Cmd $cmd): void {
37 20
        $this->cmd = $cmd;
38 20
    }
39
40
    /**
41
     * Run the cmd
42
     */
43
    abstract public function run(): void;
44
45
46
    public const EOL = "\n";
47
48
49 4
    public static function eol(): void {
50 4
        echo self::EOL;
51 4
    }
52
53 4
    public static function echo(string $str, ?string $foreground_color = null) : void {
54
55 4
        $lines = preg_split("/\r\n|\n|\r/", $str);
56
57 4
        $output = [];
58 4
        foreach ($lines as $line) {
59 4
            if ($foreground_color !== null) {
60 4
                $line = Color::getColoredString($line, $foreground_color);
61
            }
62 4
            $output[] = $line;
63
        }
64
65 4
        echo implode(self::EOL, $output);
66
    }
67
}