Skip to content

Commit

Permalink
fix: check relation parent instead of string in name for many/morph (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Feb 11, 2024
1 parent a777f4f commit 52dd428
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Properties/ModelRelationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
namespace Larastan\Larastan\Properties;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use Larastan\Larastan\Concerns;
Expand Down Expand Up @@ -114,7 +120,13 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
$relatedModelClassNames = $relatedModel->getObjectClassNames();
}

if (Str::contains($type->getObjectClassNames()[0], 'Many')) {
if (
(new ObjectType(BelongsToMany::class))->isSuperTypeOf($type)->yes()
|| (new ObjectType(HasMany::class))->isSuperTypeOf($type)->yes()
|| (new ObjectType(HasManyThrough::class))->isSuperTypeOf($type)->yes()
|| (new ObjectType(MorphMany::class))->isSuperTypeOf($type)->yes()
|| (new ObjectType(MorphToMany::class))->isSuperTypeOf($type)->yes()
) {
$types = [];

foreach ($relatedModelClassNames as $relatedModelClassName) {
Expand All @@ -126,7 +138,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
}
}

if (Str::endsWith($type->getObjectClassNames()[0], 'MorphTo')) {
if ((new ObjectType(MorphTo::class))->isSuperTypeOf($type)->yes()) {
// There was no generic type, or it was just Model
// so we will return mixed to avoid errors.
if ($relatedModel->getObjectClassNames()[0] === Model::class) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Type/data/model-properties-relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function foo(Foo $foo, Bar $bar, Account $account): void
assertType('App\User|null', $account->ownerRelation);
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model|null', $foo->relationReturningUnion);
assertType('Illuminate\Database\Eloquent\Collection<int, ModelPropertiesRelations\Bar>|ModelPropertiesRelations\Baz|null', $foo->relationReturningUnion2);
assertType('Illuminate\Database\Eloquent\Collection<int, ModelPropertiesRelations\Foo>', $foo->ancestors);
}

/** @property string $name */
Expand Down Expand Up @@ -51,6 +52,12 @@ public function relationReturningUnion2(): HasMany|BelongsTo
{
return $this->name === 'foo' ? $this->hasMany(Bar::class) : $this->belongsTo(Baz::class);
}

/** @return Ancestors<Foo> */
public function ancestors(): Ancestors
{
//
}
}

/**
Expand Down Expand Up @@ -80,3 +87,7 @@ public function morphToUnionRelation(): MorphTo
class Baz extends Model
{
}

class Ancestors extends HasMany
{
}

0 comments on commit 52dd428

Please sign in to comment.