Passed
Pull Request — master (#22)
by Nico
14:21
created

Tokenizer.php$0 ➔ getQueue()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
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
namespace nicoSWD\Rule\Tokenizer;
9
10
use ArrayIterator;
11
use Iterator;
12
use nicoSWD\Rule\Grammar\Grammar;
13
use nicoSWD\Rule\TokenStream\Token\Token;
14
use nicoSWD\Rule\TokenStream\Token\TokenFactory;
15
16
final class Tokenizer extends TokenizerInterface
17
{
18
    public function __construct(
19
        public readonly Grammar $grammar,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 19 at column 24
Loading history...
20
        private readonly TokenFactory $tokenFactory,
21
    ) {
22
    }
23
24
    public function tokenize(string $string): Iterator
25
    {
26
        $regex = $this->grammar->buildRegex();
27 16
        $stack = [];
28
        $offset = 0;
29 16
30 16
        while (preg_match($regex, $string, $matches, offset: $offset)) {
31
            $token = $this->getMatchedToken($matches);
32 16
            $className = $this->tokenFactory->createFromToken($token);
33 16
34
            $stack[] = new $className($matches[$token->value], $offset);
35 16
            $offset += strlen($matches[0]);
36
        }
37 288
38
        return new ArrayIterator($stack);
39 288
    }
40 288
41 288
    private function getMatchedToken(array $matches): Token
42
    {
43 288
        foreach ($matches as $key => $value) {
44 286
            if ($value !== '' && !is_int($key)) {
45 286
                return Token::from($key);
46
            }
47 286
        }
48 286
49
        return Token::UNKNOWN;
50
    }
51
}
52