Skip to content

Commit

Permalink
Fix phpstan errors by removing TIntermediateModels
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMuller committed Sep 9, 2024
1 parent d88c9de commit 0eaa1e0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
7 changes: 3 additions & 4 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TIntermediateModels of list<\Illuminate\Database\Eloquent\Model>
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*
* @extends \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel>
Expand All @@ -33,7 +32,7 @@ class BelongsToThrough extends Relation
/**
* The "through" parent model instances.
*
* @var TIntermediateModels
* @var list<\Illuminate\Database\Eloquent\Model>
*/
protected $throughParents;

Expand Down Expand Up @@ -63,7 +62,7 @@ class BelongsToThrough extends Relation
*
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param TDeclaringModel $parent
* @param TIntermediateModels $throughParents
* @param list<\Illuminate\Database\Eloquent\Model> $throughParents
* @param string|null $localKey
* @param string $prefix
* @param array<string, string> $foreignKeyLookup
Expand Down Expand Up @@ -325,7 +324,7 @@ public function withTrashed(...$columns)
/**
* Get the "through" parent model instances.
*
* @return TIntermediateModels
* @return list<\Illuminate\Database\Eloquent\Model>
*/
public function getThroughParents()
{
Expand Down
16 changes: 6 additions & 10 deletions src/Traits/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ trait BelongsToThrough
* Define a belongs-to-through relationship.
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TIntermediateModel of \Illuminate\Database\Eloquent\Model
* @template TIntermediateModels of list<TIntermediateModel>
*
* @param class-string<TRelatedModel> $related
* @param class-string<TIntermediateModel>[]|array{0: class-string<TIntermediateModel>, 1: string}[]|class-string<TIntermediateModel> $through
* @param class-string<\Illuminate\Database\Eloquent\Model>[]|array{0: class-string<\Illuminate\Database\Eloquent\Model>, 1: string}[]|class-string<\Illuminate\Database\Eloquent\Model> $through
* @param string|null $localKey
* @param string $prefix
* @param array<class-string<\Illuminate\Database\Eloquent\Model>, string> $foreignKeyLookup
* @param array<class-string<\Illuminate\Database\Eloquent\Model>, string> $localKeyLookup
* @return \Znck\Eloquent\Relations\BelongsToThrough<TRelatedModel, TIntermediateModels, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<TRelatedModel, $this>
*/
public function belongsToThrough(
$related,
Expand All @@ -34,7 +32,7 @@ public function belongsToThrough(
/** @var TRelatedModel $relatedInstance */
$relatedInstance = $this->newRelatedInstance($related);

/** @var TIntermediateModels $throughParents */
/** @var list<\Illuminate\Database\Eloquent\Model> $throughParents */
$throughParents = [];
$foreignKeys = [];

Expand All @@ -45,11 +43,10 @@ public function belongsToThrough(
/** @var string $foreignKey */
$foreignKey = $model[1];

/** @var class-string<TIntermediateModel> $model */
/** @var class-string<\Illuminate\Database\Eloquent\Model> $model */
$model = $model[0];
}

/** @var TIntermediateModel $instance */
$instance = $this->belongsToThroughParentInstance($model);

if ($foreignKey) {
Expand Down Expand Up @@ -123,17 +120,16 @@ protected function belongsToThroughParentInstance($model)
* Instantiate a new BelongsToThrough relationship.
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TIntermediateModels of list<\Illuminate\Database\Eloquent\Model>
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param TDeclaringModel $parent
* @param TIntermediateModels $throughParents
* @param list<\Illuminate\Database\Eloquent\Model> $throughParents
* @param string|null $localKey
* @param string $prefix
* @param array<string, string> $foreignKeyLookup
* @param array<string, string> $localKeyLookup
* @return \Znck\Eloquent\Relations\BelongsToThrough<TRelatedModel, TIntermediateModels, TDeclaringModel>
* @return \Znck\Eloquent\Relations\BelongsToThrough<TRelatedModel, TDeclaringModel>
*/
protected function newBelongsToThrough(
Builder $query,
Expand Down
2 changes: 1 addition & 1 deletion tests/IdeHelper/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Comment extends Model
use BelongsToThroughTrait;

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\IdeHelper\Models\Country, list<\Tests\IdeHelper\Models\User|\Tests\IdeHelper\Models\Post>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\IdeHelper\Models\Country, $this>
*/
public function country(): BelongsToThroughRelation
{
Expand Down
14 changes: 7 additions & 7 deletions tests/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Comment extends Model
use HasTableAlias;

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, list<\Tests\Models\User|\Tests\Models\Post>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, $this>
*/
public function country(): BelongsToThrough
{
return $this->belongsToThrough(Country::class, [User::class, Post::class])->withDefault();
}

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, list<\Tests\Models\User|\Tests\Models\Post>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, $this>
*/
public function countryWithCustomForeignKeys(): BelongsToThrough
{
Expand All @@ -37,7 +37,7 @@ public function countryWithCustomForeignKeys(): BelongsToThrough
}

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, list<\Tests\Models\User|\Tests\Models\Post>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, $this>
*/
public function countryWithTrashedUser(): BelongsToThrough
{
Expand All @@ -46,24 +46,24 @@ public function countryWithTrashedUser(): BelongsToThrough
}

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, list<\Tests\Models\User|\Tests\Models\Post>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, $this>
*/
public function countryWithPrefix(): BelongsToThrough
{
return $this->belongsToThrough(Country::class, [User::class, Post::class], null, 'custom_');
}

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<self, list<self>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<self, $this>
*/
public function grandparent(): BelongsToThrough
{
/* @phpstan-ignore argument.type, return.type */
/* @phpstan-ignore argument.type */
return $this->belongsToThrough(self::class, self::class.' as alias', null, '', [self::class => 'parent_id']);
}

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\User, list<\Tests\Models\Post>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\User, $this>
*/
public function user(): BelongsToThrough
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/CustomerAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class CustomerAddress extends Model
{
/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\VendorCustomer, list<\Tests\Models\VendorCustomerAddress>, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\VendorCustomer, $this>
*/
public function vendorCustomer(): BelongsToThrough
{
Expand Down

0 comments on commit 0eaa1e0

Please sign in to comment.