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\Iterator\RegexNamedGroupsIterator; |
14
|
|
|
use Railt\Lexer\Result\Eoi; |
15
|
|
|
use Railt\Lexer\Result\Token; |
16
|
|
|
use Railt\Lexer\Result\Unknown; |
17
|
|
|
use Railt\Lexer\Stateful; |
18
|
|
|
use Railt\Lexer\TokenInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class NativeStateful |
22
|
|
|
*/ |
23
|
|
|
class NativeStateful extends Lexer implements Stateful |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $pattern; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* NativeStateful constructor. |
32
|
|
|
* @param string $pattern |
33
|
|
|
* @param array $skipped |
34
|
|
|
*/ |
35
|
11 |
|
public function __construct(string $pattern, array $skipped = []) |
36
|
|
|
{ |
37
|
11 |
|
$this->pattern = $pattern; |
38
|
11 |
|
$this->skip = $skipped; |
39
|
11 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Readable $file |
43
|
|
|
* @return \Traversable |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
* @throws \RuntimeException |
46
|
|
|
*/ |
47
|
17 |
|
protected function exec(Readable $file): \Traversable |
48
|
|
|
{ |
49
|
17 |
|
$offset = 0; |
50
|
17 |
|
$regex = new RegexNamedGroupsIterator($this->pattern, $file->getContents()); |
51
|
|
|
|
52
|
17 |
|
$iterator = $regex->getIterator(); |
53
|
|
|
|
54
|
17 |
|
while ($iterator->valid()) { |
55
|
|
|
/** @var TokenInterface $token */ |
56
|
17 |
|
$token = $iterator->key() === Unknown::T_NAME |
57
|
9 |
|
? $this->unknown($iterator, $offset) |
58
|
17 |
|
: $this->token($iterator, $offset); |
59
|
|
|
|
60
|
17 |
|
$offset += $token->bytes(); |
61
|
|
|
|
62
|
17 |
|
yield $token; |
63
|
|
|
} |
64
|
|
|
|
65
|
17 |
|
yield new Eoi($offset); |
66
|
17 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Traversable|\Generator $iterator |
70
|
|
|
* @param int $offset |
71
|
|
|
* @return Unknown |
72
|
|
|
*/ |
73
|
9 |
|
private function unknown(\Traversable $iterator, int $offset): TokenInterface |
74
|
|
|
{ |
75
|
9 |
|
$body = $iterator->current()[0]; |
|
|
|
|
76
|
9 |
|
$iterator->next(); |
|
|
|
|
77
|
|
|
|
78
|
9 |
|
$body .= $this->reduce($iterator, Unknown::T_NAME); |
79
|
|
|
|
80
|
9 |
|
return new Unknown($body, $offset); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \Traversable|\Iterator $iterator |
85
|
|
|
* @param string $key |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
9 |
|
protected function reduce(\Traversable $iterator, string $key): string |
89
|
|
|
{ |
90
|
9 |
|
$body = ''; |
91
|
|
|
|
92
|
9 |
|
while ($iterator->valid()) { |
|
|
|
|
93
|
9 |
|
if ($iterator->key() !== $key) { |
|
|
|
|
94
|
9 |
|
break; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
$body .= $iterator->current()[0]; |
|
|
|
|
98
|
|
|
|
99
|
3 |
|
$iterator->next(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
9 |
|
return $body; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Traversable|\Iterator $iterator |
107
|
|
|
* @param int $offset |
108
|
|
|
* @return Token |
109
|
|
|
*/ |
110
|
17 |
|
private function token(\Traversable $iterator, int $offset): TokenInterface |
111
|
|
|
{ |
112
|
17 |
|
[$name, $context] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
113
|
|
|
|
114
|
17 |
|
$iterator->next(); |
|
|
|
|
115
|
|
|
|
116
|
17 |
|
return new Token($name, $context, $offset); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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: