Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

Terminal   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 49
dl 0
loc 101
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 8 2
A forceOutput() 0 4 1
A echo() 0 10 3
A warning() 0 8 2
A diff() 0 24 5
A addBuffer() 0 3 1
A error() 0 8 2
A success() 0 8 2
A getBuffer() 0 3 1
1
<?php
2
3
namespace terminal;
4
5
/**
6
 * class for terminal/cli/bash output
7
 */
8
final class Terminal {
9
    private $buffer = [];
10
11
    public function addBuffer(string $buffer)
12
    {
13
        $this->buffer[] = $buffer;
14
    }
15
16
    public function getBuffer(): string
17
    {
18
        return implode(PHP_EOL, $this->buffer);
19
    }
20
21
    public function echo(string $string, bool $to_buffer = false, bool $return = false)
22
    {
23
        if ($return) {
24
            return $string;
25
        }
26
27
        if ($to_buffer) {
28
            $this->addBuffer($string);
29
        } else {
30
            echo $string . PHP_EOL;
31
        }
32
    }
33
34
    public function diff(string $string, bool $to_buffer = false, bool $return = false)
0 ignored issues
show
Unused Code introduced by
The parameter $return is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
    public function diff(string $string, bool $to_buffer = false, /** @scrutinizer ignore-unused */ bool $return = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $result_string = '';
37
        $separator = "\r\n";
38
        $line = strtok($string, $separator);
39
40
        while ($line !== false) {
41
            if (strpos($line, '-') === 0) {
42
                $result_string .= $this->error($line, false, true);
43
            } else if(strpos($line, '+') === 0) {
44
                $result_string .= $this->success($line, false, true);
45
            } else {
46
                $result_string .= $line;
47
            }
48
49
            $result_string .= PHP_EOL;
50
51
            $line = strtok($separator);
52
        }
53
54
        if ($to_buffer) {
55
            $this->setBuffer($result_string);
0 ignored issues
show
Bug introduced by
The method setBuffer() does not exist on terminal\Terminal. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            $this->/** @scrutinizer ignore-call */ 
56
                   setBuffer($result_string);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        } else {
57
            echo $result_string;
58
        }
59
    }
60
61
    public function info(string $string, bool $to_buffer = false, bool $return = false)
62
    {
63
        $output = "\e[46m" . $string . "\e[0m";
64
65
        if ($return) {
66
            return $output;
67
        } else {
68
            $this->echo($output, $to_buffer);
69
        }
70
    }
71
72
    public function success(string $string, bool $to_buffer = false, bool $return = false)
73
    {
74
        $output = "\e[32m" . $string . "\e[0m";
75
76
        if ($return) {
77
            return $output;
78
        } else {
79
            $this->echo($output, $to_buffer);
80
        }
81
    }
82
83
    public function warning(string $string, bool $to_buffer = false, bool $return = false)
84
    {
85
        $output = "\e[43m" . $string . "\e[0m";
86
87
        if ($return) {
88
            return $output;
89
        } else {
90
            $this->echo($output, $to_buffer);
91
        }
92
    }
93
94
    public function error(string $string, bool $to_buffer = false, bool $return = false)
95
    {
96
        $output = "\e[1;31m" . $string . "\e[0m";
97
98
        if ($return) {
99
            return $output;
100
        } else {
101
            $this->echo($output, $to_buffer);
102
        }
103
    }
104
105
    public function forceOutput()
106
    {
107
        $this->echo(
108
            $this->getBuffer()
109
        );
110
    }
111
}
112