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
7 changes: 7 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
base_path('app' . DIRECTORY_SEPARATOR . 'Models'),
],

/*
* If you want to enable debug logging when a model method cannot be
* analyzed during relation discovery, set this to true.
* Useful for troubleshooting unexpected ERD generation issues.
*/
// 'debug_relations' => true,

/*
* If you want to ignore complete models or certain relations of a specific model,
* you can specify them here.
Expand Down
11 changes: 9 additions & 2 deletions src/RelationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;


class RelationFinder
Expand All @@ -31,7 +32,7 @@ public function getModelRelations(string $model)
$methods = Collection::make($class->getMethods(ReflectionMethod::IS_PUBLIC))
->merge($traitMethods)
->reject(function (ReflectionMethod $method) use ($model) {
return $method->class !== $model || $method->getNumberOfParameters() > 0 || $method->isStatic();;
return $method->class !== $model || $method->getNumberOfParameters() > 0 || $method->isStatic();
});

$relations = Collection::make();
Expand Down Expand Up @@ -95,7 +96,13 @@ protected function getRelationshipFromMethodAndModel(ReflectionMethod $method, s
)
];
}
} catch (\Throwable $e) {}
} catch (\Throwable $e) {
// Non-relation methods may throw when invoked; this is expected.
// To enable debug logging, set 'debug_relations' => true in config/erd-generator.php
if (config('erd-generator.debug_relations', false)) {
Log::debug("Could not analyze method {$method->getName()} on {$model}: {$e->getMessage()}");
}
}
return null;
}

Expand Down
24 changes: 22 additions & 2 deletions tests/GetModelRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
use BeyondCode\ErdGenerator\Tests\Models\Comment;
use BeyondCode\ErdGenerator\Tests\Models\Post;
use BeyondCode\ErdGenerator\Tests\Fixtures\ModelWithThrowingMethod;
use BeyondCode\ErdGenerator\Tests\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\Attributes\Test;


class GetModelRelationsTest extends TestCase
{

#[Test]
public function it_can_find_model_relations()
{
Expand Down Expand Up @@ -69,4 +69,24 @@ public function it_will_ignore_a_relation_if_it_is_excluded_on_config()
$this->assertNull(Arr::get($relations, 'posts'));
}

#[Test]
public function it_logs_debug_when_method_throws_exception()
{
Log::spy();

$finder = new RelationFinder();

$relations = $finder->getModelRelations(ModelWithThrowingMethod::class);

$this->assertCount(0, $relations);

Log::shouldHaveReceived('debug')
->withArgs(function (string $message) {
return str_contains($message, 'Could not analyze method')
&& str_contains($message, 'throwingMethod')
&& str_contains($message, 'Something went wrong');
})
->once();
}

}