From cdfa7df4fad90972c46832d15f42c4c0b3e48a07 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 5 Apr 2020 18:05:32 +1200 Subject: [PATCH] stmt: support static methods --- fixtures/0005-inheritance.php | 22 +++++++++++++ node.go | 60 ++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 fixtures/0005-inheritance.php diff --git a/fixtures/0005-inheritance.php b/fixtures/0005-inheritance.php new file mode 100644 index 0000000..af0404d --- /dev/null +++ b/fixtures/0005-inheritance.php @@ -0,0 +1,22 @@ +mX = $x; + } + + static function TheStatic() {} +} + +class Child extends Base { + + protected $mY; + + public function __construct($x) { + super($x); + $this->mY = 0; + } + +} diff --git a/node.go b/node.go index 3b2e0a8..fc9082a 100644 --- a/node.go +++ b/node.go @@ -166,13 +166,23 @@ func convert(n_ node.Node) (string, error) { } else { + // Check if this is a static method + hasStatic, err := hasModifier(s.Modifiers, `static`) + if err != nil { + return "", parseErr{s, err} + } + // Method body funcStmt, err := convertFunctionCommon(s.Params, s.ReturnType, s.ReturnsRef, s.Stmt.(*stmt.StmtList).Stmts) if err != nil { return "", parseErr{s, err} } - memberFuncStmt := "func (this *" + className + ") " + funcName + funcStmt + "\n" - memberFuncs = append(memberFuncs, memberFuncStmt) + + if hasStatic { + memberFuncs = append(memberFuncs, "func "+className+funcName+funcStmt+"\n") + } else { + memberFuncs = append(memberFuncs, "func (this *"+className+") "+funcName+funcStmt+"\n") + } } @@ -688,30 +698,50 @@ func convert(n_ node.Node) (string, error) { } } -// applyVisibilityModifier renames a function to use an upper/lowercase first -// letter based on PHP visibility modifiers. -func applyVisibilityModifier(funcName string, modifiers []node.Node) (string, error) { - isPublic := true +func hasModifier(modifiers []node.Node, search string) (bool, error) { for _, mod := range modifiers { ident, ok := mod.(*node.Identifier) if !ok { - return "", parseErr{mod, fmt.Errorf("expected node.Identifier")} + return false, parseErr{mod, fmt.Errorf("expected node.Identifier")} } - switch ident.Value { - case "public": - isPublic = true - case "private", "protected": - isPublic = false + if strings.ToLower(ident.Value) == strings.ToLower(search) { + return true, nil } - } - if isPublic { + return false, nil +} + +// applyVisibilityModifier renames a function to use an upper/lowercase first +// letter based on PHP visibility modifiers. +func applyVisibilityModifier(funcName string, modifiers []node.Node) (string, error) { + + hasPublic, err := hasModifier(modifiers, "public") + if err != nil { + return "", err + } + + hasPrivate, err := hasModifier(modifiers, "private") + if err != nil { + return "", err + } + + hasProtected, err := hasModifier(modifiers, "protected") + if err != nil { + return "", err + } + + if (hasPublic && !hasPrivate && !hasProtected) /* explicitly public */ || + (!hasPublic && !hasPrivate && !hasProtected) /* no modifiers defaults to public */ { return toPublic(funcName), nil - } else { + + } else if !hasPublic && (hasPrivate || hasProtected) { return toPrivate(funcName), nil + + } else { + return "", fmt.Errorf("unexpected combination of modifiers") } }