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

DelimiterAtom::isValidDelimiter()   D

Complexity

Conditions 29
Paths 29

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 29
eloc 32
nc 29
nop 1
dl 0
loc 40
rs 4.1666
c 0
b 0
f 0

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
 * (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