Skip to content

Commit 4718c93

Browse files
committed
Making sure PowerJoinClause can be type-hinted
1 parent e55fa70 commit 4718c93

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

src/FakeJoinCallback.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
/**
66
* @method static as(string $alias)
77
*/
8-
class FakeJoinCallback
8+
class FakeJoinCallback extends PowerJoinClause
99
{
10-
protected ?string $alias = null;
11-
1210
public function getAlias(): ?string
1311
{
1412
return $this->alias;

src/JoinsHelper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ public function generateAliasForRelationship(Relation $relation, string $relatio
7070

7171
/**
7272
* Get the join alias name from all the different options.
73-
*
74-
* @return string|null
7573
*/
76-
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback)
74+
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): null|string|array
7775
{
7876
if ($callback) {
7977
if (is_callable($callback)) {
80-
$fakeJoinCallback = new FakeJoinCallback();
78+
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $tableName);
8179
$callback($fakeJoinCallback);
8280

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

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

9290
if ($fakeJoinCallback->getAlias()) {

src/Mixins/JoinRelationship.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,22 @@ public function joinRelationship(): Closure
117117

118118
$relation = $this->getModel()->{$relationName}();
119119
$relationQuery = $relation->getQuery();
120-
$alias = $joinHelper->getAliasName($useAlias, $relation, $relationName,
121-
$relationQuery->getModel()->getTable(), $callback);
120+
$alias = $joinHelper->getAliasName(
121+
$useAlias,
122+
$relation,
123+
$relationName,
124+
$relationQuery->getModel()->getTable(),
125+
$callback
126+
);
122127

123128
if ($relation instanceof BelongsToMany && !is_array($alias)) {
124-
$extraAlias = $joinHelper->getAliasName($useAlias, $relation, $relationName,
129+
$extraAlias = $joinHelper->getAliasName(
130+
$useAlias,
131+
$relation,
132+
$relationName,
125133
$relation->getTable(),
126-
$callback);
134+
$callback
135+
);
127136
$alias = [$extraAlias, $alias];
128137
}
129138

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

263-
$alias = $joinHelper->getAliasName($useAlias, $relation, $relationName,
264-
$relation->getQuery()->getModel()->getTable(), $relationCallback);
272+
$alias = $joinHelper->getAliasName(
273+
$useAlias,
274+
$relation,
275+
$relationName,
276+
$relation->getQuery()->getModel()->getTable(),
277+
$relationCallback
278+
);
279+
265280
if ($alias && $relation instanceof BelongsToMany && !is_array($alias)) {
266-
$extraAlias = $joinHelper->getAliasName($useAlias, $relation, $relationName, $relation->getTable(),
267-
$relationCallback);
281+
$extraAlias = $joinHelper->getAliasName(
282+
$useAlias,
283+
$relation,
284+
$relationName,
285+
$relation->getTable(),
286+
$relationCallback
287+
);
288+
268289
$alias = [$extraAlias, $alias];
269290
}
270291

src/PowerJoinClause.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ class PowerJoinClause extends JoinClause
3737

3838
/**
3939
* Create a new join clause instance.
40-
*
41-
* @param \Illuminate\Database\Query\Builder $parentQuery
42-
* @param string $type
43-
* @param string $table
44-
* @param \Illuminate\Database\Eloquent\Model $model
45-
* @return void
4640
*/
47-
public function __construct(Builder $parentQuery, $type, $table, Model $model = null)
41+
public function __construct(Builder $parentQuery, $type, string $table, Model $model = null)
4842
{
4943
parent::__construct($parentQuery, $type, $table);
5044

@@ -205,7 +199,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
205199
*/
206200
public function withTrashed(): self
207201
{
208-
if (! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
202+
if (! $this->getModel() || ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
209203
return $this;
210204
}
211205

@@ -225,7 +219,7 @@ public function withTrashed(): self
225219
*/
226220
public function onlyTrashed(): self
227221
{
228-
if (! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
222+
if (! $this->getModel() || ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
229223
return $this;
230224
}
231225

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

241+
if (! $this->getModel()) {
242+
return;
243+
}
244+
247245
if (method_exists($this->getModel(), $scope)) {
248246
return $this->getModel()->{$scope}($this, ...$arguments);
249247
} else {

tests/JoinRelationshipTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kirschbaum\PowerJoins\Tests;
44

5+
use Kirschbaum\PowerJoins\PowerJoinClause;
56
use Kirschbaum\PowerJoins\PowerJoins;
67
use Kirschbaum\PowerJoins\Tests\Models\Group;
78
use Kirschbaum\PowerJoins\Tests\Models\Post;
@@ -705,6 +706,22 @@ public function test_scope_inside_nested_where()
705706
);
706707
}
707708

709+
public function test_it_can_type_hint_power_join_clause()
710+
{
711+
Comment::query()->joinRelationship('post', function ($join) {
712+
$join->where(fn (PowerJoinClause $query) => $query->published());
713+
})->get();
714+
715+
$sql = Comment::query()->joinRelationship('post', function ($join) {
716+
$join->where(fn (PowerJoinClause $query) => $query->published());
717+
})->toSql();
718+
719+
$this->assertStringContainsString(
720+
'inner join "posts" on "comments"."post_id" = "posts"."id" and "posts"."deleted_at" is null and ("posts"."published" = ?)',
721+
$sql
722+
);
723+
}
724+
708725
public function test_it_can_alias_belongs_to_many()
709726
{
710727
Group::query()->joinRelationship('posts', [

0 commit comments

Comments
 (0)