Completed
Push — master ( b8b783...188bc3 )
by De Cramer
9s
created

Console::write()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 62
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 62
ccs 0
cts 47
cp 0
rs 7.3333
c 3
b 0
f 0
cc 7
eloc 49
nc 4
nop 2
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace eXpansion\Core\Services;
4
5
use Symfony\Component\Console\Output\ConsoleOutputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Class Console to print in the console.
10
 *
11
 * @package eXpansion\Core\Services
12
 * @author Reaby
13
 */
14
class Console
15
{
16
17
    const black = "\e[0;30m";
18
    const b_black = "\e[30;1m";
19
20
    const red = "\e[0;31m";
21
    const b_red = "\e[31;1m";
22
23
    const green = "\e[0;32m";
24
    const b_green = "\e[32;1m";
25
26
    const yellow = "\e[0;33m";
27
    const b_yellow = "\e[33;1m";
28
29
    const blue = "\e[0;34m";
30
    const b_blue = "\e[34;1m";
31
32
    const magenta = "\e[0;35m";
33
    const b_magenta = "\e[35;1m";
34
35
    const cyan = "\e[0;36m";
36
    const b_cyan = "\e[36;1m";
37
38
    const white = "\e[0;37m";
39
    const b_white = "\e[37;1m";
40
41
    // define aliases for colors
42
    const error = "\e[37;1m\e[41m";
43
    const success = self::b_green;
44
    const normal = "\e[0m";
45
    const bold = self::b_white;
46
47
48
    /** @var ConsoleOutputInterface */
49
    protected $consoleOutput;
50
51
    /** @var boolean Color console enabled */
52
    protected $colorEnabled;
53
54
    /**
55
     * Console constructor.
56
     *
57
     * @param bool $colorEnabled
58
     */
59
    public function __construct($colorEnabled)
60
    {
61
        $this->colorEnabled = $colorEnabled;
62
    }
63
64
65
    /**
66
     * Initialize service with the console output.
67
     *
68
     * @param ConsoleOutputInterface $consoleOutput
69
     */
70
    public function init(ConsoleOutputInterface $consoleOutput)
71
    {
72
        $this->consoleOutput = $consoleOutput;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function writeln($string)
79
    {
80
        $this->write($string, true);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function write($string, $newline = false)
87
    {
88
        if ($this->colorEnabled && $this->consoleOutput->isDecorated()) {
89
            $array = array("000" => self::b_black,
90
                "100" => self::red,
91
                "010" => self::green,
92
                "110" => self::yellow,
93
                "001" => self::blue,
94
                "011" => self::magenta,
95
                "101" => self::cyan,
96
                "111" => self::white,
97
                "200" => self::b_red,
98
                "211" => self::red,
99
                "121" => self::green,
100
                "020" => self::b_green,
101
                "021" => self::green,
102
                "012" => self::cyan,
103
                "221" => self::b_yellow,
104
                "220" => self::b_yellow,
105
                "120" => self::green,
106
                "210" => self::yellow,
107
                "112" => self::b_blue,
108
                "002" => self::b_blue,
109
                "122" => self::b_cyan,
110
                "022" => self::b_cyan,
111
                "202" => self::b_magenta,
112
                "212" => self::b_magenta,
113
                "102" => self::magenta,
114
                "201" => self::b_red,
115
                "222" => self::b_white,
116
            );
117
            $matches = array();
118
            preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches);
119
            $split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string);
120
121
            $out = "";
122
            foreach ($matches[0] as $i => $rgb) {
123
                $code = $this->fixColors(hexdec($rgb[1]), hexdec($rgb[2]), hexdec($rgb[3]));
124
                if (array_key_exists($code, $array)) {
125
                    $out .= $array[$code].$this->stripStyles($split[$i + 1]);
126
                } else {
127
                    $out .= self::white.$this->stripStyles($split[$i + 1]);
128
                }
129
                $end = $this->stripStyles($split[$i + 1]);
130
            }
131
132
133
            if (!empty($end)) {
134
                if ($end == $this->stripStyles(end($split))) {
135
                    $end = "";
136
                }
137
            } else {
138
                $end = "";
139
            }
140
141
            $out = self::white.$this->stripStyles(reset($split)).$out.$end;
142
        } else {
143
            $out = $this->stripStyles($string);
144
        }
145
146
        $this->ansiOut($out, $newline);
147
    }
148
149
    /**
150
     * Outoyt brute text.
151
     *
152
     * @param string $msg
153
     * @param boolean $newline
154
     *
155
     */
156
    protected function ansiOut($msg, $newline)
157
    {
158
        // $this->consoleOutput->write($msg . self::normal, $newline, ConsoleOutputInterface::OUTPUT_RAW);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
159
        $nl = "";
160
        if ($newline) {
161
            $nl = "\n";
162
        }
163
        echo $msg.self::normal.$nl;
164
    }
165
166
    /**
167
     * Strip styles from a string.
168
     *
169
     * @param $string
170
     *
171
     * @return mixed
172
     */
173
    protected function stripStyles($string)
174
    {
175
        $string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
176
        return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string);
177
    }
178
179
    /**
180
     * Fix the color codes from MP standard to world standard
181
     *
182
     * @param string $r
183
     * @param string $g
184
     * @param string $b
185
     *
186
     * @return string
187
     */
188
    public function fixColors($r, $g, $b)
189
    {
190
        $out = "111";
191
        // black/gray/white
192
        if ($r == $g && $g == $b && $b == $r) {
193
            if ($r >= 0 && $r < 5) {
194
                $out = "000";
195
            }
196
            if ($r >= 5 && $r < 13) {
197
                $out = "111";
198
            }
199
            if ($r >= 13 && $r <= 16) {
200
                $out = "222";
201
            }
202
        } else {
203
            $out = $this->convert($r).$this->convert($g).$this->convert($b);
204
        }
205
        return $out;
206
    }
207
208
    /**
209
     * Convert from number to numeric string
210
     *
211
     * @param int $number
212
     *
213
     * @return string
214
     */
215
    public function convert($number)
216
    {
217
        $out = "0";
218
        if ($number >= 9 && $number <= 16) {
219
            $out = "2";
220
        }
221
        if ($number >= 3 && $number < 9) {
222
            $out = "1";
223
        }
224
        if ($number >= 0 && $number < 3) {
225
            $out = "0";
226
        }
227
        return $out;
228
    }
229
230
    /**
231
     * Get symfony console.
232
     *
233
     * @return OutputInterface
234
     */
235
    public function getConsoleOutput()
236
    {
237
        return $this->consoleOutput;
238
    }
239
}
240