|
23 | 23 | { |
24 | 24 | public const string NAME = 'Reference'; |
25 | 25 |
|
26 | | - /** |
27 | | - * Validates a member name: letters, digits and "_". |
28 | | - */ |
29 | | - private NameValidator $names; |
30 | | - |
31 | | - /** |
32 | | - * Validates a class name: letters, digits, "_" and "\". |
33 | | - */ |
34 | | - private NameValidator $symbols; |
35 | | - |
36 | | - public function __construct() |
37 | | - { |
38 | | - $this->names = new NameValidator(); |
39 | | - $this->symbols = new NameValidator('\\'); |
40 | | - } |
41 | | - |
42 | 26 | public function __invoke(Cursor $cursor): CodeReference |
43 | 27 | { |
44 | | - $reference = $cursor->readWord(); |
| 28 | + $reference = $this->parse($cursor); |
45 | 29 |
|
46 | | - if ($reference === '') { |
| 30 | + // A reference is a single word: nothing but whitespace may follow it. |
| 31 | + if ($reference === null || $cursor->readWord() !== '') { |
47 | 32 | throw new NoMatchException('Expected a code reference'); |
48 | 33 | } |
49 | 34 |
|
50 | | - return $this->parse($reference) |
51 | | - ?? throw new NoMatchException(\sprintf('Invalid code reference "%s"', $reference)); |
| 35 | + return $reference; |
52 | 36 | } |
53 | 37 |
|
54 | | - private function parse(string $reference): ?CodeReference |
| 38 | + private function parse(Cursor $cursor): ?CodeReference |
55 | 39 | { |
56 | 40 | // A variable: "$name". |
57 | | - if ($reference[0] === '$') { |
58 | | - return $this->parseVariable($reference); |
| 41 | + if ($cursor->readLiteral('$')) { |
| 42 | + $name = $cursor->readPhpIdentifier(); |
| 43 | + |
| 44 | + return $name === '' ? null : new VariableReference($name); |
59 | 45 | } |
60 | 46 |
|
61 | | - // A class member: "Class::member". |
62 | | - $separator = \strpos($reference, '::'); |
| 47 | + $symbol = $cursor->readPhpQualifiedName(); |
63 | 48 |
|
64 | | - if ($separator !== false) { |
65 | | - return $this->parseClassMember( |
66 | | - \substr($reference, 0, $separator), |
67 | | - \substr($reference, $separator + 2), |
68 | | - ); |
| 49 | + if ($symbol === '') { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + // A class member: "Class::member". |
| 54 | + if ($cursor->readLiteral('::')) { |
| 55 | + return $this->parseMember($cursor, $symbol); |
69 | 56 | } |
70 | 57 |
|
71 | 58 | // A function: "name()". |
72 | | - if (\str_ends_with($reference, '()')) { |
73 | | - return $this->parseFunction($reference); |
| 59 | + if ($cursor->readLiteral('()')) { |
| 60 | + return new FunctionReference($symbol); |
74 | 61 | } |
75 | 62 |
|
76 | 63 | // A class or global constant: "name". |
77 | | - return $this->parseSymbol($reference); |
| 64 | + return new SymbolReference($symbol); |
78 | 65 | } |
79 | 66 |
|
80 | 67 | /** |
81 | | - * Parses a variable: "$name". |
82 | | - */ |
83 | | - private function parseVariable(string $reference): ?VariableReference |
84 | | - { |
85 | | - $name = $this->names->validate(\substr($reference, 1)); |
86 | | - |
87 | | - return $name === null ? null : new VariableReference($name); |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * Parses a function: "name()". |
92 | | - */ |
93 | | - private function parseFunction(string $reference): ?FunctionReference |
94 | | - { |
95 | | - $symbol = $this->symbols->validate(\substr($reference, 0, -2)); |
96 | | - |
97 | | - return $symbol === null ? null : new FunctionReference($symbol); |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * Parses a class or global constant: "name". |
| 68 | + * Parses the member of a "Class::member" reference. |
| 69 | + * |
| 70 | + * @param non-empty-string $class |
102 | 71 | */ |
103 | | - private function parseSymbol(string $reference): ?SymbolReference |
| 72 | + private function parseMember(Cursor $cursor, string $class): ?CodeReference |
104 | 73 | { |
105 | | - $symbol = $this->symbols->validate($reference); |
| 74 | + // A property: "$name". |
| 75 | + if ($cursor->readLiteral('$')) { |
| 76 | + $name = $cursor->readPhpIdentifier(); |
106 | 77 |
|
107 | | - return $symbol === null ? null : new SymbolReference($symbol); |
108 | | - } |
| 78 | + return $name === '' ? null : new ClassPropertyReference($class, $name); |
| 79 | + } |
109 | 80 |
|
110 | | - private function parseClassMember(string $class, string $member): ?CodeReference |
111 | | - { |
112 | | - $symbol = $this->symbols->validate($class); |
| 81 | + $member = $cursor->readPhpIdentifier(); |
113 | 82 |
|
114 | | - if ($symbol === null || $member === '') { |
| 83 | + if ($member === '') { |
115 | 84 | return null; |
116 | 85 | } |
117 | 86 |
|
118 | | - // A property: "$name". |
119 | | - if ($member[0] === '$') { |
120 | | - return $this->parseClassProperty($symbol, $member); |
121 | | - } |
122 | | - |
123 | 87 | // A method: "name()". |
124 | | - if (\str_ends_with($member, '()')) { |
125 | | - return $this->parseClassMethod($symbol, $member); |
| 88 | + if ($cursor->readLiteral('()')) { |
| 89 | + return new ClassMethodReference($class, $member); |
126 | 90 | } |
127 | 91 |
|
128 | 92 | // A constant: "name". |
129 | | - return $this->parseClassConstant($symbol, $member); |
130 | | - } |
131 | | - |
132 | | - /** |
133 | | - * Parses the "$name" part of a "Class::$name" property reference. |
134 | | - * |
135 | | - * @param non-empty-string $class |
136 | | - */ |
137 | | - private function parseClassProperty(string $class, string $member): ?ClassPropertyReference |
138 | | - { |
139 | | - $name = $this->names->validate(\substr($member, 1)); |
140 | | - |
141 | | - return $name === null ? null : new ClassPropertyReference($class, $name); |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * Parses the "name()" part of a "Class::name()" method reference. |
146 | | - * |
147 | | - * @param non-empty-string $class |
148 | | - */ |
149 | | - private function parseClassMethod(string $class, string $member): ?ClassMethodReference |
150 | | - { |
151 | | - $name = $this->names->validate(\substr($member, 0, -2)); |
152 | | - |
153 | | - return $name === null ? null : new ClassMethodReference($class, $name); |
154 | | - } |
155 | | - |
156 | | - /** |
157 | | - * Parses the "name" part of a "Class::name" constant reference. |
158 | | - * |
159 | | - * @param non-empty-string $class |
160 | | - */ |
161 | | - private function parseClassConstant(string $class, string $member): ?ClassConstantReference |
162 | | - { |
163 | | - $name = $this->names->validate($member); |
164 | | - |
165 | | - return $name === null ? null : new ClassConstantReference($class, $name); |
| 93 | + return new ClassConstantReference($class, $member); |
166 | 94 | } |
167 | 95 | } |
0 commit comments