@@ -13,8 +13,6 @@ import (
1313
1414var phpClassRegex = regexp .MustCompile (`//\s*export_php:class\s+(\w+)` )
1515var phpMethodRegex = regexp .MustCompile (`//\s*export_php:method\s+(\w+)::([^{}\n]+)(?:\s*{\s*})?` )
16- var methodSignatureRegex = regexp .MustCompile (`(\w+)\s*\(([^)]*)\)\s*:\s*(\??[\w|]+)` )
17- var methodParamTypeNameRegex = regexp .MustCompile (`(\??[\w|]+)\s+\$?(\w+)` )
1816
1917type exportDirective struct {
2018 line int
@@ -292,79 +290,22 @@ func (cp *classParser) parseMethods(filename string) (methods []phpClassMethod,
292290}
293291
294292func (cp * classParser ) parseMethodSignature (className , signature string ) (* phpClassMethod , error ) {
295- matches := methodSignatureRegex .FindStringSubmatch (signature )
296-
297- if len (matches ) != 4 {
298- return nil , fmt .Errorf ("invalid method signature format" )
299- }
300-
301- methodName := matches [1 ]
302- paramsStr := strings .TrimSpace (matches [2 ])
303- returnTypeStr := strings .TrimSpace (matches [3 ])
304-
305- isReturnNullable := strings .HasPrefix (returnTypeStr , "?" )
306- returnType := strings .TrimPrefix (returnTypeStr , "?" )
307-
308- var params []phpParameter
309- if paramsStr != "" {
310- paramParts := strings .SplitSeq (paramsStr , "," )
311- for part := range paramParts {
312- param , err := cp .parseMethodParameter (strings .TrimSpace (part ))
313- if err != nil {
314- return nil , fmt .Errorf ("parsing parameter '%s': %w" , part , err )
315- }
316-
317- params = append (params , param )
318- }
293+ name , params , returnType , nullable , err := parseSignatureParams (signature )
294+ if err != nil {
295+ return nil , err
319296 }
320297
321298 return & phpClassMethod {
322- Name : methodName ,
323- PhpName : methodName ,
299+ Name : name ,
300+ PhpName : name ,
324301 ClassName : className ,
325302 Signature : signature ,
326303 Params : params ,
327304 ReturnType : phpType (returnType ),
328- isReturnNullable : isReturnNullable ,
305+ isReturnNullable : nullable ,
329306 }, nil
330307}
331308
332- func (cp * classParser ) parseMethodParameter (paramStr string ) (phpParameter , error ) {
333- parts := strings .Split (paramStr , "=" )
334- typePart := strings .TrimSpace (parts [0 ])
335-
336- param := phpParameter {HasDefault : len (parts ) > 1 }
337-
338- if param .HasDefault {
339- param .DefaultValue = cp .sanitizeDefaultValue (strings .TrimSpace (parts [1 ]))
340- }
341-
342- matches := methodParamTypeNameRegex .FindStringSubmatch (typePart )
343-
344- if len (matches ) < 3 {
345- return phpParameter {}, fmt .Errorf ("invalid parameter format: %s" , paramStr )
346- }
347-
348- typeStr := strings .TrimSpace (matches [1 ])
349- param .Name = strings .TrimSpace (matches [2 ])
350- param .IsNullable = strings .HasPrefix (typeStr , "?" )
351- param .PhpType = phpType (strings .TrimPrefix (typeStr , "?" ))
352-
353- return param , nil
354- }
355-
356- func (cp * classParser ) sanitizeDefaultValue (value string ) string {
357- if strings .HasPrefix (value , "[" ) && strings .HasSuffix (value , "]" ) {
358- return value
359- }
360-
361- if strings .ToLower (value ) == "null" {
362- return "null"
363- }
364-
365- return strings .Trim (value , `'"` )
366- }
367-
368309func (cp * classParser ) extractGoMethodFunction (scanner * bufio.Scanner , firstLine string ) (string , error ) {
369310 goFunc := firstLine + "\n "
370311 braceCount := 1
0 commit comments