|
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; |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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, |
|
|
|
|
|
|
56
|
148 |
|
public string $regex, |
|
|
|
|
|
|
57
|
|
|
public int $priority |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths