Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/visitor/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ func (formatter *formatter) StmtClass(node *ast.StmtClass) {

node.ClassTkn = formatter.newToken(token.T_CLASS, []byte("class"))

formatter.addFreeFloating(token.T_WHITESPACE, []byte(" "))
node.Name.Accept(formatter)
if node.Name != nil {
formatter.addFreeFloating(token.T_WHITESPACE, []byte(" "))
node.Name.Accept(formatter)
}

node.OpenParenthesisTkn = nil
node.CloseParenthesisTkn = nil
Expand Down
64 changes: 64 additions & 0 deletions pkg/visitor/formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6800,3 +6800,67 @@ func TestFormatter_NameNamePart(test *testing.T) {
test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}

func TestFormatter_ExprNew_AnonymousClass(test *testing.T) {
buffer := bytes.NewBufferString("")

node := &ast.ExprNew{
Class: &ast.StmtClass{
Args: []ast.Vertex{
&ast.Argument{
Expr: &ast.ExprVariable{
Name: &ast.Identifier{
Value: []byte("$a"),
},
},
},
},
Extends: &ast.Name{
Parts: []ast.Vertex{
&ast.NamePart{
Value: []byte("bar"),
},
},
},
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
}

formatterValue := formatter.NewFormatter().WithState(formatter.FormatterStatePHP).WithIndent(1)
node.Accept(formatterValue)

printerValue := printer.NewPrinter(buffer).WithState(printer.PrinterStatePHP)
node.Accept(printerValue)

expected := `new class($a) extends bar {
;
}`
actual := buffer.String()

if expected != actual {
test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}

func TestFormatter_ExprNew_EmptyAnonymousClass(test *testing.T) {
buffer := bytes.NewBufferString("")

node := &ast.ExprNew{
Class: &ast.StmtClass{},
}

formatterValue := formatter.NewFormatter().WithState(formatter.FormatterStatePHP).WithIndent(1)
node.Accept(formatterValue)

printerValue := printer.NewPrinter(buffer).WithState(printer.PrinterStatePHP)
node.Accept(printerValue)

expected := `new class {}`
actual := buffer.String()

if expected != actual {
test.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}