Completed
Push — master ( 42324d...1863b6 )
by Steve
03:59 queued 02:04
created

DelimiterAtom   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 89
rs 9.28
c 0
b 0
f 0
wmc 39

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidAtom() 0 3 2
D isValidDelimiter() 0 40 29
A __toString() 0 6 1
A equalsIdentifier() 0 6 5
A __construct() 0 3 1
A isValidDelimiterAtom() 0 3 1
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\RangeDifferencer\Tag;
12
13
/**
14
 * A TextAtom with an identifier from a limited set of delimiter strings.
15
 */
16
class DelimiterAtom extends TextAtom
17
{
18
    /**
19
     * @param string $c
20
     */
21
    public function __construct(string $c)
22
    {
23
        parent::__construct($c);
24
    }
25
26
    /**
27
     * @param string $c
28
     * @return bool
29
     */
30
    public static function isValidDelimiter(string $c): bool
31
    {
32
        if (\mb_strlen($c) > 1) {
33
            return false;
34
        }
35
36
        switch ($c) {
37
            // Basic Delimiters.
38
            case '/':
39
            case '.':
40
            case '!':
41
            case ',':
42
            case ';':
43
            case '?':
44
            case ' ':
45
            case '=':
46
            case "'":
47
            case '"':
48
            case "\t":
49
            case "\r":
50
            case "\n":
51
            // Extra Delimiters.
52
            case '[':
53
            case ']':
54
            case '{':
55
            case '}':
56
            case '(':
57
            case ')':
58
            case '&':
59
            case '|':
60
            case "\\":
61
            case '-':
62
            case '_':
63
            case '+':
64
            case '*':
65
            case ':':
66
                return true;
67
        }
68
69
        return false;
70
    }
71
72
    /** {@inheritdoc} */
73
    public function isValidAtom(string $c): bool
74
    {
75
        return parent::isValidAtom($c) && $this->isValidDelimiterAtom($c);
76
    }
77
78
    /**
79
     * @param string $c
80
     * @return bool
81
     */
82
    private function isValidDelimiterAtom(string $c): bool
83
    {
84
        return self::isValidDelimiter($c);
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function __toString(): string
91
    {
92
        $search  = ["\n", "\r", "\t"];
93
        $replace = ["\\\\n", "\\\\r", "\\\\t"];
94
95
        return \sprintf('DelimiterAtom: %s', \str_replace($search, $replace, $this->getFullText()));
96
    }
97
98
    /** {@inheritdoc} */
99
    public function equalsIdentifier(AtomInterface $other): bool
100
    {
101
        return
102
            parent::equalsIdentifier($other) ||
103
            (($other->getIdentifier() === '' || $other->getIdentifier() === "\n") &&
104
            ($this->getIdentifier() === '' || $this->getIdentifier() === "\n"));
105
    }
106
}
107