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