EscapeAttribute   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 1
cbo 0
dl 0
loc 62
ccs 8
cts 9
cp 0.8889
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getEscapeCharacter() 0 4 1
A equals() 0 4 1
A buildEscapeCharacter() 0 6 1
getAllowedEscapeCodes() 0 1 ?
1
<?php
2
3
namespace Matks\Vivian\Output;
4
5
use Exception;
6
7
/**
8
 * ANSI/VT100 Escape attribute
9
 */
10
abstract class EscapeAttribute
11
{
12
    const ANSI_ESCAPE_CODE_BEGIN = "\033[";
13
    const ANSI_ESCAPE_CODE_END   = 'm';
14
15
    /**
16
     * @var string
17
     */
18
    private $escapeCharacter;
19
20
    /**
21
     * @param string $code
22
     */
23
    public function __construct($code)
24
    {
25 1
        if (!in_array($code, $this->getAllowedEscapeCodes())) {
26 1
            throw new Exception("Forbidden code $code");
27
        }
28
29 1
        $escapeCharacter       = $this->buildEscapeCharacter($code);
30 1
        $this->escapeCharacter = $escapeCharacter;
31 1
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function getEscapeCharacter()
37
    {
38 1
        return $this->escapeCharacter;
39
    }
40
41
    /**
42
     * @param EscapeAttribute $escapeAttribute
43
     *
44
     * @return bool
45
     */
46
    public function equals(EscapeAttribute $escapeAttribute)
47
    {
48
        return ($escapeAttribute->getEscapeCharacter() === $this->getEscapeCharacter());
49
    }
50
51
    /**
52
     * Build escape character to be print
53
     *
54
     * @param string $escapeCode
55
     *
56
     * @return string
57
     */
58
    protected function buildEscapeCharacter($escapeCode)
59
    {
60 1
        $result = static::ANSI_ESCAPE_CODE_BEGIN . $escapeCode . static::ANSI_ESCAPE_CODE_END;
61
62 1
        return $result;
63
    }
64
65
    /**
66
     * Get allowed escape codes
67
     *
68
     * @return string[]
69
     */
70
    abstract protected function getAllowedEscapeCodes();
71
}
72