stmt: support static methods

This commit is contained in:
mappu 2020-04-05 18:05:32 +12:00
parent cdeea4c09c
commit cdfa7df4fa
2 changed files with 67 additions and 15 deletions

View File

@ -0,0 +1,22 @@
<?php
class Base {
protected $mX;
protected function __construct($x) {
$this->mX = $x;
}
static function TheStatic() {}
}
class Child extends Base {
protected $mY;
public function __construct($x) {
super($x);
$this->mY = 0;
}
}

60
node.go
View File

@ -166,13 +166,23 @@ func convert(n_ node.Node) (string, error) {
} else { } else {
// Check if this is a static method
hasStatic, err := hasModifier(s.Modifiers, `static`)
if err != nil {
return "", parseErr{s, err}
}
// Method body // Method body
funcStmt, err := convertFunctionCommon(s.Params, s.ReturnType, s.ReturnsRef, s.Stmt.(*stmt.StmtList).Stmts) funcStmt, err := convertFunctionCommon(s.Params, s.ReturnType, s.ReturnsRef, s.Stmt.(*stmt.StmtList).Stmts)
if err != nil { if err != nil {
return "", parseErr{s, err} 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 func hasModifier(modifiers []node.Node, search string) (bool, error) {
// letter based on PHP visibility modifiers.
func applyVisibilityModifier(funcName string, modifiers []node.Node) (string, error) {
isPublic := true
for _, mod := range modifiers { for _, mod := range modifiers {
ident, ok := mod.(*node.Identifier) ident, ok := mod.(*node.Identifier)
if !ok { if !ok {
return "", parseErr{mod, fmt.Errorf("expected node.Identifier")} return false, parseErr{mod, fmt.Errorf("expected node.Identifier")}
} }
switch ident.Value { if strings.ToLower(ident.Value) == strings.ToLower(search) {
case "public": return true, nil
isPublic = true
case "private", "protected":
isPublic = false
} }
} }
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 return toPublic(funcName), nil
} else {
} else if !hasPublic && (hasPrivate || hasProtected) {
return toPrivate(funcName), nil return toPrivate(funcName), nil
} else {
return "", fmt.Errorf("unexpected combination of modifiers")
} }
} }