Completed
Push — master ( 76acb2...4e56bc )
by Nico
02:11
created

BaseToken   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 113
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
getType() 0 1 ?
A __construct() 0 6 1
A getValue() 0 4 1
A getOriginalValue() 0 4 1
A getOffset() 0 4 1
A getStack() 0 4 1
A setStack() 0 4 1
A isWhitespace() 0 4 1
A isOfType() 0 4 1
A createNode() 0 4 1
A getPosition() 0 8 2
A getLine() 0 8 2
B getLineAndPosition() 0 23 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rule\TokenStream\Token;
11
12
use nicoSWD\Rule\Tokenizer\TokenStack;
13
use nicoSWD\Rule\TokenStream\TokenStream;
14
15
abstract class BaseToken
16
{
17
    /** @var mixed */
18
    protected $value;
19
    /** @var int */
20
    protected $offset = 0;
21
    /** @var TokenStack */
22
    protected $stack;
23
    /** @var int */
24
    protected $position = null;
25
    /** @var int */
26
    protected $line = null;
27
28
    abstract public function getType(): int;
29
30 230
    public function __construct($value, int $offset = 0, TokenStack $stack = null)
31
    {
32 230
        $this->value = $value;
33 230
        $this->offset = $offset;
34 230
        $this->stack = $stack;
35 230
    }
36
37
    /**
38
     * @return mixed
39
     */
40 204
    public function getValue()
41
    {
42 204
        return $this->value;
43
    }
44
45
    /**
46
     * Some tokens can be represented by different operators, so the original value is used for error reporting,
47
     * while the other one is used internally.
48
     *
49
     * @return mixed
50
     */
51 124
    final public function getOriginalValue()
52
    {
53 124
        return $this->value;
54
    }
55
56 134
    public function getOffset(): int
57
    {
58 134
        return $this->offset;
59
    }
60
61 90
    public function getStack(): TokenStack
62
    {
63 90
        return $this->stack;
64
    }
65
66 86
    public function setStack(TokenStack $stack)
67
    {
68 86
        $this->stack = $stack;
69 86
    }
70
71 188
    public function isWhitespace(): bool
72
    {
73 188
        return false;
74
    }
75
76 192
    public function isOfType(int $type): bool
77
    {
78 192
        return $this->getType() === $type;
79
    }
80
81 208
    public function createNode(TokenStream $tokenStream): self
82
    {
83 208
        return $this;
84
    }
85
86 50
    public function getPosition(): int
87
    {
88 50
        if (!isset($this->position)) {
89 48
            $this->getLineAndPosition();
90
        }
91
92 50
        return $this->position;
93
    }
94
95 50
    public function getLine(): int
96
    {
97 50
        if (!isset($this->line)) {
98 2
            $this->getLineAndPosition();
99
        }
100
101 50
        return $this->line;
102
    }
103
104 50
    private function getLineAndPosition()
105
    {
106 50
        $this->line = 1;
107 50
        $this->position = 0;
108
109 50
        foreach ($this->stack as $token) {
110 50
            $sumPosition = true;
111
112 50
            if ($token === $this) {
113 50
                break;
114 40
            } elseif ($token instanceof TokenNewline) {
115 4
                $this->line += 1;
116 4
                $this->position = 0;
117 4
                $sumPosition = false;
118 40
            } elseif ($token instanceof TokenComment) {
119 4
                $this->line += substr_count($token->getValue(), "\n");
120
            }
121
122 40
            if ($sumPosition) {
123 40
                $this->position += strlen($token->getOriginalValue());
124
            }
125
        }
126 50
    }
127
}
128