Completed
Branch dev (f76876)
by De Cramer
02:07
created

Console::ansiOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace eXpansion\Core\Services;
4
5
use Symfony\Component\Console\Output\ConsoleOutput;
6
use Symfony\Component\Console\Output\ConsoleOutputInterface;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
10
class Console
11
{
12
13
    const black = "\e[0;30m";
14
    const b_black = "\e[30;1m";
15
16
    const red = "\e[0;31m";
17
    const b_red = "\e[31;1m";
18
19
    const green = "\e[0;32m";
20
    const b_green = "\e[32;1m";
21
22
    const yellow = "\e[0;33m";
23
    const b_yellow = "\e[33;1m";
24
25
    const blue = "\e[0;34m";
26
    const b_blue = "\e[34;1m";
27
28
    const magenta = "\e[0;35m";
29
    const b_magenta = "\e[35;1m";
30
31
    const cyan = "\e[0;36m";
32
    const b_cyan = "\e[36;1m";
33
34
    const white = "\e[0;37m";
35
    const b_white = "\e[37;1m";
36
37
    // define aliases for colors
38
    const error = "\e[37;1m\e[41m";
39
    const success = self::b_green;
40
    const normal = self::white;
41
    const bold = self::b_white;
42
43
    /** @var ContainerInterface */
44
    protected $container;
45
46
47
    /** @var ConsoleOutput */
48
    protected $consoleOutput;
49
50
    function __construct(ContainerInterface $container)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52
        $this->container = $container;
53
    }
54
55
    function init(ConsoleOutputInterface $consoleOutput)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        $this->consoleOutput = $consoleOutput;
0 ignored issues
show
Documentation Bug introduced by
$consoleOutput is of type object<Symfony\Component...ConsoleOutputInterface>, but the property $consoleOutput was declared to be of type object<Symfony\Component...e\Output\ConsoleOutput>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
58
    }
59
60
    function writeln($string)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
    {
62
        $this->write($string, true);
63
    }
64
65
    function write($string, $newline = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
    {
67
        $array = array("000" => self::b_black,
68
            "100" => self::red,
69
            "010" => self::green,
70
            "110" => self::yellow,
71
            "001" => self::blue,
72
            "011" => self::magenta,
73
            "101" => self::cyan,
74
            "111" => self::white,
75
            "200" => self::b_red,
76
            "211" => self::red,
77
            "121" => self::green,
78
            "020" => self::b_green,
79
            "021" => self::green,
80
            "012" => self::cyan,
81
            "221" => self::b_yellow,
82
            "220" => self::b_yellow,
83
            "120" => self::green,
84
            "210" => self::yellow,
85
            "112" => self::b_blue,
86
            "002" => self::b_blue,
87
            "122" => self::b_cyan,
88
            "022" => self::b_cyan,
89
            "202" => self::b_magenta,
90
            "212" => self::b_magenta,
91
            "102" => self::magenta,
92
            "201" => self::b_red,
93
            "222" => self::b_white,
94
        );
95
        $matches = array();
96
        preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches);
97
        $split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string);
98
99
        $out = "";
100
        foreach ($matches[0] as $i => $rgb) {
101
            $code = $this->fix(hexdec($rgb[1]), hexdec($rgb[2]), hexdec($rgb[3]));
102
            if (array_key_exists($code, $array)) {
103
                $out .= $array[$code] . $this->stripStyles($split[$i + 1]);
104
            } else {
105
                $out .= self::white . $this->stripStyles($split[$i + 1]);
106
            }
107
            $end = $this->stripStyles($split[$i + 1]);
108
        }
109
110
111
        if (!empty($end)) {
112
            if ($end == $this->stripStyles(end($split))) {
113
                $end = "";
114
            }
115
        } else {
116
            $end = "";
117
        }
118
119
        $out = self::white . $this->stripStyles(reset($split)) . $out . $end;
120
121
        $this->ansiOut($out, $newline);
122
    }
123
124
125
    private function ansiOut($msg, $newline)
126
    {
127
        /*if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
            $msg = str_replace("\e", "", $msg);
129
            $msg = preg_replace("/\[(\d{1,2}\;){0,1}\d{1,2}m/", "", $msg);
130
        } */
131
132
        $this->consoleOutput->write($msg, $newline, ConsoleOutput::OUTPUT_RAW);
133
    }
134
135
136
    private function stripStyles($string)
137
    {
138
139
        $string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
140
        return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string);
141
    }
142
143
144
    public function fix($r, $g, $b)
145
    {
146
        $out = "111";
147
        // black/gray/white
148
        if ($r == $g && $g == $b && $b == $r) {
149
            if ($r >= 0 && $r < 5) {
150
                $out = "000";
151
            }
152
            if ($r >= 5 && $r < 13) {
153
                $out = "111";
154
            }
155
            if ($r >= 13 && $r <= 16) {
156
                $out = "222";
157
            }
158
        } else {
159
            $out = $this->convert($r) . $this->convert($g) . $this->convert($b);
160
        }
161
        return $out;
162
    }
163
164
    public function convert($number)
165
    {
166
        $out = "0";
167
168
        if ($number >= 9 && $number <= 16) {
169
            $out = "2";
170
        }
171
        if ($number >= 3 && $number < 9) {
172
            $out = "1";
173
        }
174
        if ($number >= 0 && $number < 3) {
175
            $out = "0";
176
        }
177
        return $out;
178
    }
179
180
181
}
182