Skip to content

Commit

Permalink
Making sure PowerJoinClause can be type-hinted
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdalmolin committed Dec 7, 2023
1 parent e55fa70 commit 4718c93
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/FakeJoinCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
/**
* @method static as(string $alias)
*/
class FakeJoinCallback
class FakeJoinCallback extends PowerJoinClause
{
protected ?string $alias = null;

public function getAlias(): ?string
{
return $this->alias;
Expand Down
8 changes: 3 additions & 5 deletions src/JoinsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ public function generateAliasForRelationship(Relation $relation, string $relatio

/**
* Get the join alias name from all the different options.
*
* @return string|null
*/
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback)
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): null|string|array
{
if ($callback) {
if (is_callable($callback)) {
$fakeJoinCallback = new FakeJoinCallback();
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $tableName);
$callback($fakeJoinCallback);

if ($fakeJoinCallback->getAlias()) {
Expand All @@ -86,7 +84,7 @@ public function getAliasName(bool $useAlias, Relation $relation, string $relatio
}

if (is_array($callback) && isset($callback[$tableName])) {
$fakeJoinCallback = new FakeJoinCallback();
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $tableName);
$callback[$tableName]($fakeJoinCallback);

if ($fakeJoinCallback->getAlias()) {
Expand Down
37 changes: 29 additions & 8 deletions src/Mixins/JoinRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,22 @@ public function joinRelationship(): Closure

$relation = $this->getModel()->{$relationName}();
$relationQuery = $relation->getQuery();
$alias = $joinHelper->getAliasName($useAlias, $relation, $relationName,
$relationQuery->getModel()->getTable(), $callback);
$alias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relationQuery->getModel()->getTable(),
$callback
);

if ($relation instanceof BelongsToMany && !is_array($alias)) {
$extraAlias = $joinHelper->getAliasName($useAlias, $relation, $relationName,
$extraAlias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relation->getTable(),
$callback);
$callback
);
$alias = [$extraAlias, $alias];
}

Expand Down Expand Up @@ -260,11 +269,23 @@ public function joinNestedRelationship(): Closure
$relationCallback = $callback[$fullRelationName];
}

$alias = $joinHelper->getAliasName($useAlias, $relation, $relationName,
$relation->getQuery()->getModel()->getTable(), $relationCallback);
$alias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relation->getQuery()->getModel()->getTable(),
$relationCallback
);

if ($alias && $relation instanceof BelongsToMany && !is_array($alias)) {
$extraAlias = $joinHelper->getAliasName($useAlias, $relation, $relationName, $relation->getTable(),
$relationCallback);
$extraAlias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relation->getTable(),
$relationCallback
);

$alias = [$extraAlias, $alias];
}

Expand Down
16 changes: 7 additions & 9 deletions src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ class PowerJoinClause extends JoinClause

/**
* Create a new join clause instance.
*
* @param \Illuminate\Database\Query\Builder $parentQuery
* @param string $type
* @param string $table
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function __construct(Builder $parentQuery, $type, $table, Model $model = null)
public function __construct(Builder $parentQuery, $type, string $table, Model $model = null)
{
parent::__construct($parentQuery, $type, $table);

Expand Down Expand Up @@ -205,7 +199,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
*/
public function withTrashed(): self
{
if (! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
if (! $this->getModel() || ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
return $this;
}

Expand All @@ -225,7 +219,7 @@ public function withTrashed(): self
*/
public function onlyTrashed(): self
{
if (! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
if (! $this->getModel() || ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
return $this;
}

Expand All @@ -244,6 +238,10 @@ public function __call($name, $arguments)
{
$scope = 'scope' . ucfirst($name);

if (! $this->getModel()) {
return;
}

if (method_exists($this->getModel(), $scope)) {
return $this->getModel()->{$scope}($this, ...$arguments);
} else {
Expand Down
17 changes: 17 additions & 0 deletions tests/JoinRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirschbaum\PowerJoins\Tests;

use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\PowerJoins;
use Kirschbaum\PowerJoins\Tests\Models\Group;
use Kirschbaum\PowerJoins\Tests\Models\Post;
Expand Down Expand Up @@ -705,6 +706,22 @@ public function test_scope_inside_nested_where()
);
}

public function test_it_can_type_hint_power_join_clause()
{
Comment::query()->joinRelationship('post', function ($join) {
$join->where(fn (PowerJoinClause $query) => $query->published());
})->get();

$sql = Comment::query()->joinRelationship('post', function ($join) {
$join->where(fn (PowerJoinClause $query) => $query->published());
})->toSql();

$this->assertStringContainsString(
'inner join "posts" on "comments"."post_id" = "posts"."id" and "posts"."deleted_at" is null and ("posts"."published" = ?)',
$sql
);
}

public function test_it_can_alias_belongs_to_many()
{
Group::query()->joinRelationship('posts', [
Expand Down

0 comments on commit 4718c93

Please sign in to comment.