Passed
Push — master ( db8d65...db8d65 )
by Alec
04:35 queued 02:07
created

ConsoleColour   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 50
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A force256Colors() 0 6 3
A are256ColorsForced() 0 3 1
A __construct() 0 4 1
A are256ColorsSupported() 0 4 2
A apply() 0 10 2
1
<?php
2
/**
3
 * User: alec
4
 * Date: 15.10.18
5
 * Time: 21:33
6
 */
7
8
namespace AlecRabbit\ConsoleColour;
9
10
use AlecRabbit\ConsoleColour\Exception\InvalidStyleException;
11
use AlecRabbit\Traits\DoesProcessException;
12
13
class ConsoleColour extends ConsoleColor
14
{
15
    use DoesProcessException;
16
17
    public const ESC_CHAR = "\033";
18
19
    /** @var bool */
20
    protected $force256Colors;
21
22 52
    public function __construct(?bool $force256Colors = null)
23
    {
24 52
        parent::__construct();
25 52
        $this->force256Colors = $force256Colors ?? false;
26 52
    }
27
28 2
    public function force256Colors(): ConsoleColour
29
    {
30 2
        if (!$this->are256ColorsForced()) {
31 2
            $this->force256Colors = $this->are256ColorsSupported() ? false : true;
32
        }
33 2
        return $this;
34
    }
35
36 2
    public function are256ColorsForced(): bool
37
    {
38 2
        return $this->force256Colors;
39
    }
40
41 2
    public function are256ColorsSupported(): bool
42
    {
43
        return
44 2
            $this->force256Colors ?: parent::are256ColorsSupported();
45
    }
46
47
    /**
48
     * @param array|string $styles
49
     * @param string $text
50
     * @return string
51
     * @throws \Throwable
52
     */
53 38
    public function apply($styles, $text): string
54
    {
55
        try {
56 38
            return parent::apply($styles, $text);
57 6
        } catch (InvalidStyleException $e) {
58
            // Do nothing
59
            // or
60 4
            $this->processException($e);
61
        }
62
        return $text;
63
    }
64
}
65