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") } }