Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 4, 2024
1 parent 3a13fa3 commit 949c7d9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/IdeHelper/BelongsToThroughRelationsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function run(ModelsCommand $command, Model $model): void
}

/**
* @param Relation<\Illuminate\Database\Eloquent\Model> $relationship
* @param \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $relationship
*/
protected function addRelationship(ModelsCommand $command, ReflectionMethod $method, Relation $relationship): void
{
Expand Down
3 changes: 1 addition & 2 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Staudenmeir\BelongsToThrough;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Console\Command;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Staudenmeir\BelongsToThrough\IdeHelper\BelongsToThroughRelationsHook;
Expand All @@ -25,7 +24,7 @@ public function register(): void
}

/**
* @return class-string<Command>[]
* @return class-string<\Illuminate\Console\Command>[]
*/
public function provides(): array
{
Expand Down
10 changes: 6 additions & 4 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,20 @@ public function get($columns = ['*'])
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model> $query
* @param \Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model> $parent
* @param \Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model> $parentQuery
* @param string[]|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>
*/
public function getRelationExistenceQuery(Builder $query, Builder $parent, $columns = ['*'])
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
$this->performJoins($query);

$from = $parent->getQuery()->from;
$from = $parentQuery->getQuery()->from;

if ($from instanceof Expression) {
$from = $from->getValue($query->getGrammar());
$from = $from->getValue(
$parentQuery->getGrammar()
);
}

$foreignKey = $from . '.' . $this->getFirstForeignKeyName();
Expand Down
4 changes: 2 additions & 2 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testExistenceQueryWithPrefix(): void

public function testWithTrashed(): void
{
/** @var User $user */
/** @var \Tests\Models\User $user */
$user = Comment::findOrFail(33)->user()
->withTrashed()
->first();
Expand All @@ -115,7 +115,7 @@ public function testWithTrashed(): void

public function testWithTrashedIntermediate(): void
{
/** @var Country $country */
/** @var \Tests\Models\Country $country */
$country = Comment::findOrFail(33)->country()
->withTrashed(['users.deleted_at'])
->first();
Expand Down
9 changes: 5 additions & 4 deletions tests/IdeHelper/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
namespace Tests\IdeHelper\Models;

use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
use Znck\Eloquent\Relations\BelongsToThrough as BelongsToThroughRelation;
use Znck\Eloquent\Traits\BelongsToThrough as BelongsToThroughTrait;

class Comment extends Model
{
use BelongsToThrough;
use BelongsToThroughTrait;

/**
* @return \Znck\Eloquent\Relations\BelongsToThrough<Country, User|Post, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\IdeHelper\Models\Country, \Tests\IdeHelper\Models\User|\Tests\IdeHelper\Models\Post, $this>
*/
public function country()
public function country(): BelongsToThroughRelation
{
return $this->belongsToThrough(Country::class, [User::class, Post::class]);
}
Expand Down
16 changes: 7 additions & 9 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 BelongsToThrough<Country, User|Post, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\Country, \Tests\Models\User|\Tests\Models\Post, $this>
*/
public function country(): BelongsToThrough
{
return $this->belongsToThrough(Country::class, [User::class, Post::class])->withDefault();
}

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

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

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

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

/**
* @return BelongsToThrough<User, Post, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\User, \Tests\Models\Post, $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 BelongsToThrough<VendorCustomer, VendorCustomerAddress, $this>
* @return \Znck\Eloquent\Relations\BelongsToThrough<\Tests\Models\VendorCustomer, \Tests\Models\VendorCustomerAddress, $this>
*/
public function vendorCustomer(): BelongsToThrough
{
Expand Down

0 comments on commit 949c7d9

Please sign in to comment.