Passed
Push — master ( 244bf6...cc00b6 )
by Timm
01:59
created

CmdRunner   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 66
ccs 24
cts 24
cp 1
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCmd() 0 2 1
A setCmd() 0 2 1
A __construct() 0 6 2
A eol() 0 2 1
A help() 0 1 1
A echo() 0 13 3
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 21
    public function __construct(Cmd $cmd = null) {
19 21
        if($cmd !== null) {
20 4
            $this->cmd = $cmd;
21 4
            return;
22
        }
23 21
        $this->cmd = Cmd::root();
24 21
    }
25
26
    /**
27
     * @return Cmd
28
     */
29 5
    public function getCmd(): Cmd {
30 5
        return $this->cmd;
31
    }
32
33
    /**
34
     * @param Cmd $cmd
35
     */
36 21
    public function setCmd(Cmd $cmd): void {
37 21
        $this->cmd = $cmd;
38 21
    }
39
40
    /**
41
     * Run the cmd
42
     */
43
    abstract public function run(): void;
44
45
    /**
46
     * Overwrite this method for extended help
47
     */
48 3
    public function help(): void {
49
50 3
    }
51
52
53
    public const EOL = "\n";
54
55
56 6
    public static function eol(): void {
57 6
        echo self::EOL;
58 6
    }
59
60 6
    public static function echo(string $str, ?string $foreground_color = null) : void {
61
62 6
        $lines = preg_split("/\r\n|\n|\r/", $str);
63
64 6
        $output = [];
65 6
        foreach ($lines as $line) {
66 6
            if ($foreground_color !== null) {
67 6
                $line = Color::getColoredString($line, $foreground_color);
68
            }
69 6
            $output[] = $line;
70
        }
71
72 6
        echo implode(self::EOL, $output);
73 6
    }
74
75
}