Skip to content

Commit e550444

Browse files
Get the Class name from the first namespace declaration in the model file, instead of just using the first element in the statements array
When the model file contains a strict_types declaration above the namespace declaration, it will not be detected as a Model class, because the $statements array's first element is always checked for the class name which, in the case of a file with the strict types declaration in it, will always fail since the class name structure will be in the next element in the array
1 parent d9f655d commit e550444

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)