Passed
Branch dev (ea35c5)
by Wilmer
17:29 queued 12:43
created

SqlToken   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 398
Duplicated Lines 0 %

Test Coverage

Coverage 85.29%

Importance

Changes 0
Metric Value
wmc 46
eloc 99
dl 0
loc 398
ccs 87
cts 102
cp 0.8529
rs 8.72
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 5 1
A offsetSet() 0 11 2
A offsetUnset() 0 9 2
A getChildren() 0 3 1
A __toString() 0 3 1
A offsetExists() 0 3 1
A setChildren() 0 10 2
A calculateOffset() 0 7 2
A getContent() 0 3 1
A getHasChildren() 0 3 2
A getIsCollection() 0 3 1
A endOffset() 0 5 1
A getType() 0 3 1
A startOffset() 0 5 1
A type() 0 5 1
A updateCollectionOffsets() 0 8 2
A content() 0 5 1
A parent() 0 5 1
C tokensMatch() 0 61 16
A matches() 0 13 3
A getSql() 0 19 3

How to fix   Complexity   

Complex Class

Complex classes like SqlToken often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SqlToken, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use ArrayAccess;
8
9
use function array_splice;
10
use function count;
11
use function end;
12
use function in_array;
13
use function mb_substr;
14
use function reset;
15
16
/**
17
 * SqlToken represents SQL tokens produced by {@see SqlTokenizer} or its child classes.
18
 *
19
 * @property SqlToken[] $children Child tokens.
20
 * @property bool $hasChildren Whether the token has children. This property is read-only.
21
 * @property bool $isCollection Whether the token represents a collection of tokens. This property is
22
 * read-only.
23
 * @property string $sql SQL code. This property is read-only.
24
 */
25
final class SqlToken implements ArrayAccess
26
{
27
    public const TYPE_CODE = 0;
28
    public const TYPE_STATEMENT = 1;
29
    public const TYPE_TOKEN = 2;
30
    public const TYPE_PARENTHESIS = 3;
31
    public const TYPE_KEYWORD = 4;
32
    public const TYPE_OPERATOR = 5;
33
    public const TYPE_IDENTIFIER = 6;
34
    public const TYPE_STRING_LITERAL = 7;
35
    private int $type = self::TYPE_TOKEN;
36
    private ?string $content = null;
37
    private ?int $startOffset = null;
38
    private ?int $endOffset = null;
39
    private ?SqlToken $parent = null;
40
    private array $children = [];
41
42
    /**
43
     * Returns the SQL code representing the token.
44
     *
45
     * @return string SQL code.
46
     */
47 3
    public function __toString(): string
48
    {
49 3
        return $this->getSql();
50
    }
51
52
    /**
53
     * Returns whether there is a child token at the specified offset.
54
     *
55
     * This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something
56
     * like `isset($token[$offset])`.
57
     *
58
     * @param int $offset child token offset.
59
     *
60
     * @return bool whether the token exists.
61
     */
62 12
    public function offsetExists($offset): bool
63
    {
64 12
        return isset($this->children[$this->calculateOffset($offset)]);
65
    }
66
67
    /**
68
     * Returns a child token at the specified offset.
69
     *
70
     * This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something
71
     * like `$child = $token[$offset];`.
72
     *
73
     * @param int $offset child token offset.
74
     *
75
     * @return SqlToken|null the child token at the specified offset, `null` if there's no token.
76
     */
77 17
    public function offsetGet($offset): ?self
78
    {
79 17
        $offset = $this->calculateOffset($offset);
80
81 17
        return $this->children[$offset] ?? null;
82
    }
83
84
    /**
85
     * Adds a child token to the token.
86
     *
87
     * This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something
88
     * like `$token[$offset] = $child;`.
89
     *
90
     * @param mixed $offset child token offset.
91
     * @param mixed $value  token to be added.
92
     */
93 17
    public function offsetSet(mixed $offset, mixed $value): void
94
    {
95 17
        $value->parent = $this;
96
97 17
        if ($offset === null) {
98 17
            $this->children[] = $value;
99
        } else {
100
            $this->children[$this->calculateOffset((int) $offset)] = $value;
101
        }
102
103 17
        $this->updateCollectionOffsets();
104
    }
105
106
    /**
107
     * Removes a child token at the specified offset.
108
     *
109
     * This method is required by the SPL {@see ArrayAccess} interface. It is implicitly called when you use something
110
     * like `unset($token[$offset])`.
111
     *
112
     * @param int $offset child token offset.
113
     */
114 5
    public function offsetUnset($offset): void
115
    {
116 5
        $offset = $this->calculateOffset($offset);
117
118 5
        if (isset($this->children[$offset])) {
119 5
            array_splice($this->children, $offset, 1);
120
        }
121
122 5
        $this->updateCollectionOffsets();
123
    }
124
125
    /**
126
     * Returns child tokens.
127
     *
128
     * @return SqlToken[] child tokens.
129
     */
130 5
    public function getChildren(): array
131
    {
132 5
        return $this->children;
133
    }
134
135
    /**
136
     * Sets a list of child tokens.
137
     *
138
     * @param SqlToken[] $children child tokens.
139
     */
140
    public function setChildren(array $children): void
141
    {
142
        $this->children = [];
143
144
        foreach ($children as $child) {
145
            $child->parent = $this;
146
            $this->children[] = $child;
147
        }
148
149
        $this->updateCollectionOffsets();
150
    }
151
152
    /**
153
     * Returns whether the token represents a collection of tokens.
154
     *
155
     * @return bool whether the token represents a collection of tokens.
156
     */
157 17
    public function getIsCollection(): bool
158
    {
159 17
        return in_array($this->type, [self::TYPE_CODE, self::TYPE_STATEMENT, self::TYPE_PARENTHESIS], true);
160
    }
161
162
    /**
163
     * Returns whether the token represents a collection of tokens and has non-zero number of children.
164
     *
165
     * @return bool whether the token has children.
166
     */
167 17
    public function getHasChildren(): bool
168
    {
169 17
        return $this->getIsCollection() && !empty($this->children);
170
    }
171
172
    /**
173
     * Returns the SQL code representing the token.
174
     *
175
     * @return string SQL code.
176
     */
177 8
    public function getSql(): string
178
    {
179 8
        $sql = '';
180 8
        $code = $this;
181
182 8
        while ($code->parent !== null) {
183 8
            $code = $code->parent;
184
        }
185
186 8
        if ($code->content !== null) {
187 8
            $sql = mb_substr(
188 8
                $code->content,
189 8
                (int) $this->startOffset,
190 8
                (int) $this->endOffset - (int) $this->startOffset,
191
                'UTF-8',
192
            );
193
        }
194
195 8
        return $sql;
196
    }
197
198
    /**
199
     * Returns whether this token (including its children) matches the specified "pattern" SQL code.
200
     *
201
     * Usage Example:
202
     *
203
     * ```php
204
     * $patternToken = (new \Yiisoft\Db\Sqlite\SqlTokenizer('SELECT any FROM any'))->tokenize();
205
     * if ($sqlToken->matches($patternToken, 0, $firstMatchIndex, $lastMatchIndex)) {
206
     *     // ...
207
     * }
208
     * ```
209
     *
210
     * @param SqlToken $patternToken tokenized SQL codes to match against. In addition to normal SQL, the `any` keyword
211
     * is supported which will match any number of keywords, identifiers, whitespaces.
212
     * @param int $offset token children offset to start lookup with.
213
     * @param int|null $firstMatchIndex token children offset where a successful match begins.
214
     * @param int|null $lastMatchIndex  token children offset where a successful match ends.
215
     *
216
     * @return bool whether this token matches the pattern SQL code.
217
     */
218 12
    public function matches(
219
        self $patternToken,
220
        int $offset = 0,
221
        ?int &$firstMatchIndex = null,
222
        ?int &$lastMatchIndex = null
223
    ): bool {
224 12
        $result = false;
225
226 12
        if ($patternToken->getHasChildren() && ($patternToken[0] instanceof self)) {
227 12
            $result = $this->tokensMatch($patternToken[0], $this, $offset, $firstMatchIndex, $lastMatchIndex);
0 ignored issues
show
Bug introduced by
It seems like $patternToken[0] can also be of type null; however, parameter $patternToken of Yiisoft\Db\Sqlite\SqlToken::tokensMatch() does only seem to accept Yiisoft\Db\Sqlite\SqlToken, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
            $result = $this->tokensMatch(/** @scrutinizer ignore-type */ $patternToken[0], $this, $offset, $firstMatchIndex, $lastMatchIndex);
Loading history...
228
        }
229
230 12
        return $result;
231
    }
232
233
    /**
234
     * Tests the given token to match the specified pattern token.
235
     *
236
     * @param SqlToken $patternToken
237
     * @param SqlToken $token
238
     * @param int $offset
239
     * @param int|null $firstMatchIndex
240
     * @param int|null $lastMatchIndex
241
     *
242
     * @return bool
243
     */
244 12
    private function tokensMatch(
245
        self $patternToken,
246
        self $token,
247
        int $offset = 0,
248
        ?int &$firstMatchIndex = null,
249
        ?int &$lastMatchIndex = null
250
    ): bool {
251
        if (
252 12
            $patternToken->getIsCollection() !== $token->getIsCollection() ||
253 12
            (!$patternToken->getIsCollection() && $patternToken->content !== $token->content)
254
        ) {
255 12
            return false;
256
        }
257
258 12
        if ($patternToken->children === $token->children) {
259 12
            $firstMatchIndex = $lastMatchIndex = $offset;
260
261 12
            return true;
262
        }
263
264 12
        $firstMatchIndex = $lastMatchIndex = null;
265 12
        $wildcard = false;
266
267 12
        for ($index = 0, $count = count($patternToken->children); $index < $count; $index++) {
268
            /**
269
             *  Here we iterate token by token with an exception to "any" that toggles an iteration until we matched
270
             *  with a next pattern token or EOF.
271
             */
272 12
            if ($patternToken[$index] instanceof self && $patternToken[$index]->content === 'any') {
273 12
                $wildcard = true;
274 12
                continue;
275
            }
276
277 12
            for ($limit = $wildcard ? count($token->children) : $offset + 1; $offset < $limit; $offset++) {
278 12
                if (!$wildcard && !isset($token[$offset])) {
279
                    break;
280
                }
281
282
                if (
283 12
                    $patternToken[$index] instanceof self &&
284 12
                    $token[$offset] instanceof self  &&
285 12
                    !$this->tokensMatch($patternToken[$index], $token[$offset])
0 ignored issues
show
Bug introduced by
It seems like $patternToken[$index] can also be of type null; however, parameter $patternToken of Yiisoft\Db\Sqlite\SqlToken::tokensMatch() does only seem to accept Yiisoft\Db\Sqlite\SqlToken, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

285
                    !$this->tokensMatch(/** @scrutinizer ignore-type */ $patternToken[$index], $token[$offset])
Loading history...
Bug introduced by
It seems like $token[$offset] can also be of type null; however, parameter $token of Yiisoft\Db\Sqlite\SqlToken::tokensMatch() does only seem to accept Yiisoft\Db\Sqlite\SqlToken, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

285
                    !$this->tokensMatch($patternToken[$index], /** @scrutinizer ignore-type */ $token[$offset])
Loading history...
286
                ) {
287 12
                    continue;
288
                }
289
290 12
                if ($firstMatchIndex === null) {
291 12
                    $firstMatchIndex = $offset;
292
                }
293
294 12
                $lastMatchIndex = $offset;
295 12
                $wildcard = false;
296 12
                $offset++;
297
298 12
                continue 2;
299
            }
300
301 12
            return false;
302
        }
303
304 12
        return true;
305
    }
306
307
    /**
308
     * Returns an absolute offset in the children array.
309
     *
310
     * @param int $offset
311
     *
312
     * @return int
313
     */
314 17
    private function calculateOffset(int $offset): int
315
    {
316 17
        if ($offset >= 0) {
317 17
            return $offset;
318
        }
319
320 17
        return count($this->children) + $offset;
321
    }
322
323
    /**
324
     * Updates token SQL code start and end offsets based on its children.
325
     */
326 17
    private function updateCollectionOffsets(): void
327
    {
328 17
        if (!empty($this->children)) {
329 17
            $this->startOffset = reset($this->children)->startOffset;
330 17
            $this->endOffset = end($this->children)->endOffset;
331
        }
332
333 17
        $this->parent?->updateCollectionOffsets();
334
    }
335
336
    /**
337
     * Set token type. It has to be one of the following constants:
338
     *
339
     * - {@see TYPE_CODE}
340
     * - {@see TYPE_STATEMENT}
341
     * - {@see TYPE_TOKEN}
342
     * - {@see TYPE_PARENTHESIS}
343
     * - {@see TYPE_KEYWORD}
344
     * - {@see TYPE_OPERATOR}
345
     * - {@see TYPE_IDENTIFIER}
346
     * - {@see TYPE_STRING_LITERAL}
347
     *
348
     * @param int $value token type. It has to be one of the following constants:
349
     *
350
     * @return self
351
     */
352 17
    public function type(int $value): self
353
    {
354 17
        $this->type = $value;
355
356 17
        return $this;
357
    }
358
359
    /**
360
     * Set token content.
361
     *
362
     * @param string|null $value
363
     *
364
     * @return self
365
     */
366 17
    public function content(?string $value): self
367
    {
368 17
        $this->content = $value;
369
370 17
        return $this;
371
    }
372
373
    /**
374
     * Set original SQL token start position.
375
     *
376
     * @param int $value original SQL token start position.
377
     *
378
     * @return self
379
     */
380 17
    public function startOffset(int $value): self
381
    {
382 17
        $this->startOffset = $value;
383
384 17
        return $this;
385
    }
386
387
    /**
388
     * Set original SQL token end position.
389
     *
390
     * @param int $value original SQL token end position.
391
     *
392
     * @return self
393
     */
394 17
    public function endOffset(int $value): self
395
    {
396 17
        $this->endOffset = $value;
397
398 17
        return $this;
399
    }
400
401
    /**
402
     * Set parent token.
403
     *
404
     * @param SqlToken $value parent token.
405
     *
406
     * @return self
407
     */
408
    public function parent(self $value): self
409
    {
410
        $this->parent = $value;
411
412
        return $this;
413
    }
414
415
    public function getContent(): ?string
416
    {
417
        return $this->content;
418
    }
419
420
    public function getType(): int
421
    {
422
        return $this->type;
423
    }
424
}
425