1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Lexer\Driver; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\Lexer\Driver\NativeStateful\PCRECompiler; |
14
|
|
|
use Railt\Lexer\Iterator\RegexNamedGroupsIterator; |
15
|
|
|
use Railt\Lexer\Result\Eoi; |
16
|
|
|
use Railt\Lexer\Result\Token; |
17
|
|
|
use Railt\Lexer\Result\Unknown; |
18
|
|
|
use Railt\Lexer\TokenInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class NativeRegex |
22
|
|
|
*/ |
23
|
|
|
class NativeRegex extends SimpleLexer |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* NativeRegex constructor. |
27
|
|
|
* @param array $tokens |
28
|
|
|
* @param array $skip |
29
|
|
|
*/ |
30
|
3 |
|
public function __construct(array $tokens = [], array $skip = []) |
31
|
|
|
{ |
32
|
3 |
|
$this->tokens = $tokens; |
33
|
3 |
|
$this->skipped = $skip; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Readable $file |
38
|
|
|
* @return \Traversable |
39
|
|
|
* @throws \InvalidArgumentException |
40
|
|
|
* @throws \RuntimeException |
41
|
|
|
*/ |
42
|
6 |
|
protected function exec(Readable $file): \Traversable |
43
|
|
|
{ |
44
|
6 |
|
$offset = 0; |
45
|
6 |
|
$regex = new RegexNamedGroupsIterator($this->getPattern(), $file->getContents()); |
46
|
|
|
|
47
|
6 |
|
$iterator = $regex->getIterator(); |
48
|
|
|
|
49
|
6 |
|
while ($iterator->valid()) { |
50
|
|
|
/** @var TokenInterface $token */ |
51
|
6 |
|
$token = $iterator->key() === Unknown::T_NAME |
52
|
1 |
|
? $this->unknown($iterator, $offset) |
53
|
6 |
|
: $this->token($iterator, $offset); |
54
|
|
|
|
55
|
6 |
|
$offset += $token->getBytes(); |
56
|
|
|
|
57
|
6 |
|
yield $token; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
yield new Eoi($offset); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
6 |
|
private function getPattern(): string |
67
|
|
|
{ |
68
|
6 |
|
return (new PCRECompiler($this->tokens))->compile(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param \Traversable|\Generator $iterator |
73
|
|
|
* @param int $offset |
74
|
|
|
* @return Unknown |
75
|
|
|
*/ |
76
|
1 |
|
private function unknown(\Traversable $iterator, int $offset): TokenInterface |
77
|
|
|
{ |
78
|
1 |
|
$body = $iterator->current()[0]; |
|
|
|
|
79
|
1 |
|
$iterator->next(); |
|
|
|
|
80
|
|
|
|
81
|
1 |
|
$body .= $this->reduce($iterator, Unknown::T_NAME); |
82
|
|
|
|
83
|
1 |
|
return new Unknown($body, $offset); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Traversable|\Iterator $iterator |
88
|
|
|
* @param string $key |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
protected function reduce(\Traversable $iterator, string $key): string |
92
|
|
|
{ |
93
|
1 |
|
$body = ''; |
94
|
|
|
|
95
|
1 |
|
while ($iterator->valid()) { |
|
|
|
|
96
|
1 |
|
if ($iterator->key() !== $key) { |
|
|
|
|
97
|
1 |
|
break; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$body .= $iterator->current()[0]; |
|
|
|
|
101
|
|
|
|
102
|
1 |
|
$iterator->next(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return $body; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \Traversable|\Iterator $iterator |
110
|
|
|
* @param int $offset |
111
|
|
|
* @return Token |
112
|
|
|
*/ |
113
|
6 |
|
private function token(\Traversable $iterator, int $offset): TokenInterface |
114
|
|
|
{ |
115
|
6 |
|
[$name, $context] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
116
|
|
|
|
117
|
6 |
|
$iterator->next(); |
|
|
|
|
118
|
|
|
|
119
|
6 |
|
return new Token($name, $context, $offset); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: