Completed
Pull Request — master (#75)
by
unknown
02:24
created

Console::doHslConvert()   F

Complexity

Conditions 24
Paths 2064

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 3.4662
c 0
b 0
f 0
ccs 0
cts 33
cp 0
cc 24
eloc 34
nc 2064
nop 3
crap 600

How to fix   Long Method    Complexity   

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