Skip to content

Commit

Permalink
Merge pull request #37 from Halleck45/fix_36
Browse files Browse the repository at this point in the history
fixes #36 - error parsing PHP code without namespace
  • Loading branch information
Halleck45 committed Mar 27, 2024
2 parents 6e3cb79 + b768e3d commit 1a30588
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/Engine/Php/PhpRunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,41 @@ function bar() {
func2 := result.Stmts.StmtFunction[1]
assert.Equal(t, 4, len(func2.Stmts.StmtDecisionIf), "Incorrect number of if statements")
}

func TestNamesapceWithoutName(t *testing.T) {
phpSource := `
<?php
namespace {
class Foo
{
public function __construct()
{
echo 'Foo::__construct()';
}
}
}
`
// Create a temporary file
tmpFile := t.TempDir() + "/test.php"
if _, err := os.Create(tmpFile); err != nil {
t.Error(err)
}
if err := os.WriteFile(tmpFile, []byte(phpSource), 0644); err != nil {
t.Error(err)
}

result, err := parsePhpFile(tmpFile)

// Ensure no error
assert.Nil(t, err, "Expected no error, got %s", err)

// Ensure path
assert.Equal(t, tmpFile, result.Path, "Expected path to be %s, got %s", tmpFile, result.Path)

// Ensure classes
assert.Equal(t, 1, len(result.Stmts.StmtClass), "Incorrect number of classes")
class1 := result.Stmts.StmtClass[0]
assert.Equal(t, "Foo", class1.Name.Short, "Expected class name to be 'Foo', got %s", class1.Name)

}
10 changes: 7 additions & 3 deletions src/Engine/Php/PhpVisitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ func (v *PhpVisitor) StmtFunction(node *ast.StmtFunction) {
}

func (v *PhpVisitor) StmtNamespace(node *ast.StmtNamespace) {
parts := node.Name.(*ast.Name).Parts

name := ""
for _, part := range parts {
name += string(part.(*ast.NamePart).Value) + "\\"
if node.Name != nil {
// if namespace has no name, it is global namespace
parts := node.Name.(*ast.Name).Parts
for _, part := range parts {
name += string(part.(*ast.NamePart).Value) + "\\"
}
}

namespace := &pb.StmtNamespace{
Expand Down

0 comments on commit 1a30588

Please sign in to comment.