Skip to content

Commit ff3c934

Browse files
authored
Merge pull request #17 from joe-pritchard/master
Get the Class name from the first namespace declaration in the model file
2 parents d9f655d + e550444 commit ff3c934

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/ModelFinder.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\NodeTraverser;
66
use PhpParser\ParserFactory;
77
use PhpParser\Node\Stmt\Class_;
8+
use PhpParser\Node\Stmt\Namespace_;
89
use Illuminate\Support\Collection;
910
use Illuminate\Filesystem\Filesystem;
1011
use PhpParser\NodeVisitor\NameResolver;
@@ -42,10 +43,14 @@ protected function getFullyQualifiedClassNameFromFile(string $path): string
4243
$code = file_get_contents($path);
4344

4445
$statements = $parser->parse($code);
45-
4646
$statements = $traverser->traverse($statements);
47+
48+
// get the first namespace declaration in the file
49+
$root_statement = collect($statements)->filter(function ($statement) {
50+
return $statement instanceof Namespace_;
51+
})->first();
4752

48-
return collect($statements[0]->stmts)
53+
return collect($root_statement->stmts)
4954
->filter(function ($statement) {
5055
return $statement instanceof Class_;
5156
})
@@ -55,4 +60,4 @@ protected function getFullyQualifiedClassNameFromFile(string $path): string
5560
->first() ?? '';
5661
}
5762

58-
}
63+
}

0 commit comments

Comments
 (0)