From 451496e4272c11d87b404791acdd352c606f29c0 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Thu, 20 Feb 2025 20:20:43 +0100 Subject: [PATCH] Find BelongsToThrough relationships by return type (#114) --- src/IdeHelper/BelongsToThroughRelationsHook.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/IdeHelper/BelongsToThroughRelationsHook.php b/src/IdeHelper/BelongsToThroughRelationsHook.php index 33c036e..b49d447 100644 --- a/src/IdeHelper/BelongsToThroughRelationsHook.php +++ b/src/IdeHelper/BelongsToThroughRelationsHook.php @@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Relations\Relation; use ReflectionClass; use ReflectionMethod; -use Throwable; +use ReflectionNamedType; use Znck\Eloquent\Relations\BelongsToThrough as BelongsToThroughRelation; use Znck\Eloquent\Traits\BelongsToThrough as BelongsToThroughTrait; @@ -30,13 +30,11 @@ public function run(ModelsCommand $command, Model $model): void continue; } - try { + if ($method->getReturnType() instanceof ReflectionNamedType + && $method->getReturnType()->getName() === BelongsToThroughRelation::class) { + /** @var \Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $relationship */ $relationship = $method->invoke($model); - } catch (Throwable) { // @codeCoverageIgnore - continue; // @codeCoverageIgnore - } - if ($relationship instanceof BelongsToThroughRelation) { $this->addRelationship($command, $method, $relationship); } }