Issues (54)

src/Tokenizer/Tokenizer.php (7 issues)

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 nicoSWD\Rule\Grammar\Grammar;
12
use nicoSWD\Rule\TokenStream\Token\TokenFactory;
0 ignored issues
show
The type nicoSWD\Rule\TokenStream\Token\TokenFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SplPriorityQueue;
14
15
final class Tokenizer implements TokenizerInterface
16
{
17
    private array $tokens = [];
18
    private string $compiledRegex = '';
19
20
    public function __construct(
21
        private Grammar $grammar,
22
        private TokenFactory $tokenFactory
0 ignored issues
show
The parameter $tokenFactory is not used and could be removed. ( Ignorable by Annotation )

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

22
        /** @scrutinizer ignore-unused */ private TokenFactory $tokenFactory

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    ) {
24
        foreach ($grammar->getDefinition() as [$class, $regex, $priority]) {
25
            $this->registerToken($class, $regex, $priority);
26
        }
27 16
    }
28
29 16
    public function tokenize(string $string): ArrayIterator
30 16
    {
31
        $regex = $this->getRegex();
32 16
        $stack = [];
33 16
        $offset = 0;
34
35 16
        while (preg_match($regex, $string, $matches, 0, $offset)) {
36
            $token = $this->getMatchedToken($matches);
37 288
            $className = $this->tokenFactory->createFromTokenName($token);
0 ignored issues
show
Bug Best Practice introduced by
The property tokenFactory does not exist on nicoSWD\Rule\Tokenizer\Tokenizer. Did you maybe forget to declare it?
Loading history...
38
39 288
            $stack[] = new $className($matches[$token], $offset);
40 288
            $offset += strlen($matches[0]);
41 288
        }
42
43 288
        return new ArrayIterator($stack);
44 286
    }
45 286
46
    public function getGrammar(): Grammar
47 286
    {
48 286
        return $this->grammar;
0 ignored issues
show
Bug Best Practice introduced by
The property grammar does not exist on nicoSWD\Rule\Tokenizer\Tokenizer. Did you maybe forget to declare it?
Loading history...
49
    }
50
51 288
    private function registerToken(string $class, string $regex, int $priority): void
52
    {
53
        $this->tokens[$class] = new class($class, $regex, $priority) {
54 148
            public function __construct(
55
                public string $class,
0 ignored issues
show
The parameter $class is not used and could be removed. ( Ignorable by Annotation )

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

55
                /** @scrutinizer ignore-unused */ public string $class,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56 148
                public string $regex,
0 ignored issues
show
The parameter $regex is not used and could be removed. ( Ignorable by Annotation )

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

56
                /** @scrutinizer ignore-unused */ public string $regex,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
                public int $priority
0 ignored issues
show
The parameter $priority is not used and could be removed. ( Ignorable by Annotation )

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

57
                /** @scrutinizer ignore-unused */ public int $priority

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
            ) {
59 16
            }
60
        };
61 16
    }
62 16
63 16
    private function getMatchedToken(array $matches): string
64 16
    {
65
        foreach ($matches as $key => $value) {
66 16
            if ($value !== '' && !is_int($key)) {
67 16
                return $key;
68
            }
69 288
        }
70
71 288
        return 'Unknown';
72 286
    }
73 286
74
    private function getRegex(): string
75
    {
76
        if (!$this->compiledRegex) {
77 2
            $regex = [];
78
79
            foreach ($this->getQueue() as $token) {
80 288
                $regex[] = "(?<$token->class>$token->regex)";
81
            }
82 288
83 10
            $this->compiledRegex = '~(' . implode('|', $regex) . ')~As';
84
        }
85 10
86 10
        return $this->compiledRegex;
87
    }
88
89 10
    private function getQueue(): SplPriorityQueue
90
    {
91
        $queue = new SplPriorityQueue();
92 288
93
        foreach ($this->tokens as $class) {
94
            $queue->insert($class, $class->priority);
95 10
        }
96
97 10
        return $queue;
98
    }
99
}
100