Passed
Push — master ( f247da...c8939d )
by Kacper
02:56
created

ConsoleHelper::_style()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 13

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 6.5458
cc 13
eloc 17
nc 13
nop 2
crap 13

How to fix   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
 * Highlighter
4
 *
5
 * Copyright (C) 2015, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Utils;
17
18
class ConsoleHelper
19
{
20
    private $_stack   = [];
21
    private $_current = [];
22
23
    private $_default = [
24
        'color'      => 'default',
25
        'background' => 'default',
26
        'bold'       => false,
27
        'underlined' => false,
28
        'dim'        => false,
29
        'blink'      => false,
30
    ];
31
32 1
    public function styled($style, $text)
33
    {
34 1
        return $this->open($style).$text.$this->close();
35
    }
36
37 12
    public function open($style)
38
    {
39 12
        if (!empty($this->_current)) {
40 2
            $this->_stack[] = $this->_current;
41 2
            $style          = array_diff($style, $this->_current);
42 2
        }
43
44 12
        $this->_current = array_merge($this->_current, $style);
45
46 12
        return $this->_set(array_diff($this->_current, $this->_default));
47
    }
48
49 3
    public function close()
50
    {
51 3
        $this->_current = empty($this->_stack) ? $this->_default : array_pop($this->_stack);
52
53 3
        return "\033[0m".$this->_set(array_diff($this->_current, $this->_default));
54
    }
55
56 6
    private function _color($name, $bg = false)
57
    {
58
        $colors = [
59 6
            'default'       => 39,
60 6
            'black'         => 30,
61 6
            'red'           => 31,
62 6
            'green'         => 32,
63 6
            'yellow'        => 33,
64 6
            'blue'          => 34,
65 6
            'magenta'       => 35,
66 6
            'cyan'          => 36,
67 6
            'light gray'    => 37,
68 6
            'dark gray'     => 90,
69 6
            'light red'     => 91,
70 6
            'light green'   => 92,
71 6
            'light yellow'  => 93,
72 6
            'light blue'    => 94,
73 6
            'light magenta' => 95,
74 6
            'light cyan'    => 96,
75 6
            'white'         => 97,
76 6
        ];
77
78 6
        return $colors[strtolower($name)] + ($bg ? 10 : 0);
79
    }
80
81 12
    private function _style($name, $value)
82
    {
83
        switch ($name) {
84 12
            case 'color':
85 4
                return $this->_color($value);
86 9
            case 'background':
87 3
                return $this->_color($value, true);
88 8
            case 'bold':
89 3
                return $value ? 1 : 21;
90 5
            case 'dim':
91 1
                return $value ? 2 : 22;
92 4
            case 'underline':
93 1
                return $value ? 4 : 24;
94 3
            case 'blink':
95 1
                return $value ? 5 : 25;
96 2
            case 'invert':
97 1
                return $value ? 7 : 27;
98
        }
99
100 1
        return null;
101
    }
102
103
    private function _set($style)
104
    {
105 12
        $escape = "\e[".implode(';', array_map(function ($style, $name) {
106 12
            return $this->_style($style, $name);
107 12
        }, array_keys($style), $style)).'m';
108
109 12
        return $escape === "\e[m" ? null : $escape;
110
    }
111
112 2
    public function reset()
113
    {
114 2
        return "\e[0m";
115
    }
116
}
117