@@ -194,17 +194,32 @@ function parseFunctionLike(string $name, Node\FunctionLike $func, ?string $cond)
194
194
return new FuncInfo ($ name , $ args , $ return , $ numRequiredArgs , $ cond );
195
195
}
196
196
197
- function parseClass (Stmt \Class_ $ class ): ClassInfo {
198
- $ funcs = [];
199
- $ className = $ class ->name ->toString ();
200
- foreach ($ class as $ stmt ) {
201
- if (!$ stmt instanceof Stmt \ClassMethod) {
202
- throw new Exception ("Not implemented class statement " );
197
+ function handlePreprocessorConditions (array &$ conds , Stmt $ stmt ): ?string {
198
+ foreach ($ stmt ->getComments () as $ comment ) {
199
+ $ text = trim ($ comment ->getText ());
200
+ if (preg_match ('/^#\s*if\s+(.+)$/ ' , $ text , $ matches )) {
201
+ $ conds [] = $ matches [1 ];
202
+ } else if (preg_match ('/^#\s*ifdef\s+(.+)$/ ' , $ text , $ matches )) {
203
+ $ conds [] = "defined( $ matches [1 ]) " ;
204
+ } else if (preg_match ('/^#\s*ifndef\s+(.+)$/ ' , $ text , $ matches )) {
205
+ $ conds [] = "!defined( $ matches [1 ]) " ;
206
+ } else if (preg_match ('/^#\s*else$/ ' , $ text )) {
207
+ if (empty ($ conds )) {
208
+ throw new Exception ("Encountered else without corresponding #if " );
209
+ }
210
+ $ cond = array_pop ($ conds );
211
+ $ conds [] = "!( $ cond) " ;
212
+ } else if (preg_match ('/^#\s*endif$/ ' , $ text )) {
213
+ if (empty ($ conds )) {
214
+ throw new Exception ("Encountered #endif without corresponding #if " );
215
+ }
216
+ array_pop ($ conds );
217
+ } else if ($ text [0 ] === '# ' ) {
218
+ throw new Exception ("Unrecognized preprocessor directive \"$ text \"" );
203
219
}
204
-
205
- $ funcs [] = parseFunctionLike ($ className . '_ ' . $ stmt ->name ->toString (), $ stmt );
206
220
}
207
- return new ClassInfo ($ className , $ funcs );
221
+
222
+ return empty ($ conds ) ? null : implode (' && ' , $ conds );
208
223
}
209
224
210
225
/** @return FuncInfo[] */
@@ -226,32 +241,7 @@ function parseStubFile(string $fileName) {
226
241
$ funcInfos = [];
227
242
$ conds = [];
228
243
foreach ($ stmts as $ stmt ) {
229
- foreach ($ stmt ->getComments () as $ comment ) {
230
- $ text = trim ($ comment ->getText ());
231
- if (preg_match ('/^#\s*if\s+(.+)$/ ' , $ text , $ matches )) {
232
- $ conds [] = $ matches [1 ];
233
- } else if (preg_match ('/^#\s*ifdef\s+(.+)$/ ' , $ text , $ matches )) {
234
- $ conds [] = "defined( $ matches [1 ]) " ;
235
- } else if (preg_match ('/^#\s*ifndef\s+(.+)$/ ' , $ text , $ matches )) {
236
- $ conds [] = "!defined( $ matches [1 ]) " ;
237
- } else if (preg_match ('/^#\s*else$/ ' , $ text )) {
238
- if (empty ($ conds )) {
239
- throw new Exception ("Encountered else without corresponding #if " );
240
- }
241
- $ cond = array_pop ($ conds );
242
- $ conds [] = "!( $ cond) " ;
243
- } else if (preg_match ('/^#\s*endif$/ ' , $ text )) {
244
- if (empty ($ conds )) {
245
- throw new Exception ("Encountered #endif without corresponding #if " );
246
- }
247
- array_pop ($ conds );
248
- } else if ($ text [0 ] === '# ' ) {
249
- throw new Exception ("Unrecognized preprocessor directive \"$ text \"" );
250
- }
251
- }
252
-
253
- $ cond = empty ($ conds ) ? null : implode (' && ' , $ conds );
254
-
244
+ $ cond = handlePreprocessorConditions ($ conds , $ stmt );
255
245
if ($ stmt instanceof Stmt \Nop) {
256
246
continue ;
257
247
}
@@ -264,6 +254,7 @@ function parseStubFile(string $fileName) {
264
254
if ($ stmt instanceof Stmt \ClassLike) {
265
255
$ className = $ stmt ->name ->toString ();
266
256
foreach ($ stmt ->stmts as $ classStmt ) {
257
+ $ cond = handlePreprocessorConditions ($ conds , $ classStmt );
267
258
if ($ classStmt instanceof Stmt \Nop) {
268
259
continue ;
269
260
}
0 commit comments