Completed
Pull Request — master (#4)
by Nico
02:45 queued 31s
created

BaseToken   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 128
ccs 40
cts 45
cp 0.8889
rs 10
c 0
b 0
f 0

12 Methods

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