Completed
Push — master ( 9087d5...f022af )
by Shcherbak
36:34 queued 21:40
created

Token::equal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Funivan\PhpTokenizer;
6
7
  use Funivan\PhpTokenizer\Exception\Exception;
8
  use Funivan\PhpTokenizer\Exception\InvalidArgumentException;
9
10
  /**
11
   *
12
   * Value is 2 type variable. It can be string or null
13
   * When you set value is automatically cast to string
14
   *
15
   * @package Funivan\PhpTokenizer
16
   */
17
  class Token {
18
19
    const INVALID_TYPE = -1;
20
21
    const INVALID_LINE = -1;
22
23
    const INVALID_VALUE = null;
24
25
    const INVALID_INDEX = -1;
26
27
    /**
28
     * @var int
29
     */
30
    protected $type = self::INVALID_TYPE;
31
32
    /**
33
     * @var string|null
34
     */
35
    protected $value;
36
37
    /**
38
     * @var int
39
     */
40
    protected $line = self::INVALID_LINE;
41
42
    /**
43
     * Indicate position in current collection
44
     *
45
     * @var int
46
     */
47
    protected $index = self::INVALID_INDEX;
48
49
50
    /**
51
     * You need to provide at least 3 elements
52
     *
53
     * @param array $data
54
     * @throws Exception
55
     */
56 504
    public function __construct(array $data = []) {
57 504
      if (!empty($data)) {
58 450
        $this->setData($data);
59 292
      }
60 492
    }
61
62
63
    /**
64
     * @return string
65
     */
66 9
    public function __toString() {
67 9
      return $this->assemble();
68
    }
69
70
71
    /**
72
     * @return string
73
     */
74 3
    public function assemble() : string {
75 3
      return $this->value !== null ? (string) $this->value : '';
76
    }
77
78
79
    /**
80
     * @param array $data
81
     * @return $this
82
     * @throws Exception
83
     */
84 450
    protected function setData(array $data) : self {
85 450
      if (!array_key_exists(0, $data)) {
86 3
        throw new InvalidArgumentException('Please provide type of token');
87
      }
88
89 447
      $this->setType((int) $data[0]);
90
91 447
      if (!isset($data[1])) {
92 3
        throw new InvalidArgumentException('Please provide value of token');
93
      }
94
95 444
      $this->setValue($data[1]);
96
97 444
      if (!array_key_exists(2, $data)) {
98 3
        throw new InvalidArgumentException('Please provide line of token');
99
      }
100
101 441
      $this->setLine($data[2]);
102
103 441
      if (array_key_exists(3, $data)) {
104 6
        $this->setIndex($data[3]);
105 2
      }
106
107 438
      return $this;
108
    }
109
110
111
    /**
112
     * @return array
113
     */
114 9
    public function getData() : array {
115 9
      return [$this->getType(), $this->getValue(), $this->getLine(), $this->getIndex()];
116
    }
117
118
119
    /**
120
     * @param int $type
121
     * @return $this
122
     */
123 447
    public function setType(int $type) : self {
124 447
      $this->type = $type;
125 447
      return $this;
126
    }
127
128
129
    /**
130
     * @return int
131
     */
132 264
    public function getType() : int {
133 264
      return $this->type;
134
    }
135
136
137
    /**
138
     * @return string
139
     */
140 6
    public function getTypeName() : string {
141 6
      return token_name($this->getType());
142
    }
143
144
145
    /**
146
     * @return string|null
147
     */
148 450
    public function getValue() {
149 450
      return $this->value;
150
    }
151
152
153
    /**
154
     * @param string|int $value
155
     * @throws InvalidArgumentException
156
     * @return $this
157
     */
158 459
    public function setValue($value) : self {
159
160 459
      if (!is_string($value) and !is_numeric($value)) {
161 3
        throw new InvalidArgumentException('You can set only string. Given: ' . gettype($value));
162
      }
163
164 456
      $this->value = (string) $value;
165 456
      return $this;
166
    }
167
168
169
    /**
170
     * @return int
171
     */
172 423
    public function getLine() : int {
173 423
      return $this->line;
174
    }
175
176
177
    /**
178
     * @param int $line
179
     * @return $this
180
     */
181 444
    public function setLine(int $line) : self {
182 444
      $this->line = $line;
183 444
      return $this;
184
    }
185
186
187
    /**
188
     * @return bool
189
     */
190 438
    public function isValid() : bool {
191 438
      return $this->getValue() !== null;
192
    }
193
194
195
    /**
196
     * Remove all data from token so this token become invalid
197
     *
198
     * @return $this
199
     */
200 66
    public function remove() : self {
201 66
      $this->type = static::INVALID_TYPE;
202 66
      $this->value = static::INVALID_VALUE;
203 66
      $this->line = static::INVALID_LINE;
204 66
      $this->index = static::INVALID_INDEX;
205 66
      return $this;
206
    }
207
208
209
    /**
210
     * Add part to the end of value
211
     *
212
     * @param string $part
213
     * @return $this
214
     * @throws Exception
215
     */
216 9
    public function appendToValue($part) : self {
217
218 9
      if (!is_string($part) and !is_numeric($part)) {
219 3
        throw new InvalidArgumentException('You can append only string to value');
220
      }
221
222 6
      $this->value = $this->value . $part;
223
224 6
      return $this;
225
    }
226
227
228
    /**
229
     * Add part to the begin of value
230
     *
231
     * @param string $part
232
     * @return $this
233
     * @throws Exception
234
     */
235 12
    public function prependToValue($part) : self {
236
237 12
      if (!is_string($part) and !is_numeric($part)) {
238 3
        throw new InvalidArgumentException('You can prepend only string to value');
239
      }
240
241 9
      $this->value = $part . $this->value;
242
243 9
      return $this;
244
    }
245
246
247
    /**
248
     * @return null|int
249
     */
250 225
    public function getIndex() {
251 225
      return $this->index;
252
    }
253
254
255
    /**
256
     * @param int $index
257
     * @return $this
258
     */
259 462
    public function setIndex(int $index) : self {
260 462
      $this->index = $index;
261 3
      return $this;
262
    }
263
264 459
    public function equal(Token $token) : bool {
265 459
      return (
266
        $this->value === $token->getValue()
267
        and
268
        $this->type === $token->getType()
269
      );
270
    }
271
272
  }