| Total Complexity | 11 | 
| Total Lines | 83 | 
| Duplicated Lines | 0 % | 
| Coverage | 100% | 
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1);  | 
            ||
| 15 | |||
| 16 | final class Tokenizer extends TokenizerInterface  | 
            ||
| 17 | { | 
            ||
| 18 | public function __construct(  | 
            ||
| 19 | public Grammar $grammar,  | 
            ||
| 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 |