Skip to content

Commit deed185

Browse files
committed
chore: add strict types
1 parent e55fa70 commit deed185

8 files changed

+351
-265
lines changed

src/EloquentJoins.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class EloquentJoins
1515
/**
1616
* Register macros with Eloquent.
1717
*/
18-
public static function registerEloquentMacros()
18+
public static function registerEloquentMacros(): void
1919
{
2020
EloquentQueryBuilder::mixin(new JoinRelationship);
2121
EloquentQueryBuilder::mixin(new QueryRelationshipExistence);

src/JoinsHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class JoinsHelper
1010
{
11+
/**
12+
* @var array<int, static>
13+
*/
1114
static array $instances = [];
1215

1316
protected function __construct()
@@ -25,7 +28,6 @@ public static function make(): static
2528
/**
2629
* Cache to not join the same relationship twice.
2730
*
28-
* @var array
2931
*/
3032
private array $joinRelationshipCache = [];
3133

@@ -71,7 +73,7 @@ public function generateAliasForRelationship(Relation $relation, string $relatio
7173
/**
7274
* Get the join alias name from all the different options.
7375
*
74-
* @return string|null
76+
* @return array<string>|string|null
7577
*/
7678
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback)
7779
{

src/Mixins/JoinRelationship.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ public function powerJoin(): Closure
2828
{
2929
return function ($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): static {
3030
$model = $operator instanceof Model ? $operator : null;
31-
$join = $this->newPowerJoinClause($this->query, $type, $table, $model);
31+
/** @var PowerJoinClause $join */
32+
$join = $this->newPowerJoinClause($this->getQuery(), $type, $table, $model);
3233

3334
// If the first "column" of the join is really a Closure instance the developer
3435
// is trying to build a join with a complex "on" clause containing more than
3536
// one condition, so we'll add the join and call a Closure with the query.
3637
if ($first instanceof Closure) {
3738
$first($join);
3839

39-
$this->query->joins[] = $join;
40+
$this->getQuery()->joins[] = $join;
4041

41-
$this->query->addBinding($join->getBindings(), 'join');
42+
$this->getQuery()->addBinding($join->getBindings(), 'join');
4243
}
4344

4445
// If the column is simply a string, we can assume the join simply has a basic
@@ -47,9 +48,9 @@ public function powerJoin(): Closure
4748
else {
4849
$method = $where ? 'where' : 'on';
4950

50-
$this->query->joins[] = $join->$method($first, $operator, $second);
51+
$this->getQuery()->joins[] = $join->$method($first, $operator, $second);
5152

52-
$this->query->addBinding($join->getBindings(), 'join');
53+
$this->getQuery()->addBinding($join->getBindings(), 'join');
5354
}
5455

5556
return $this;
@@ -200,28 +201,28 @@ public function joinRelation(): Closure
200201

201202
public function leftJoinRelationship(): Closure
202203
{
203-
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
204+
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
204205
return $this->joinRelationship($relation, $callback, 'leftJoin', $useAlias, $disableExtraConditions);
205206
};
206207
}
207208

208209
public function leftJoinRelation(): Closure
209210
{
210-
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
211+
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {https://github.com/kirschbaum-development/eloquent-power-joins
211212
return $this->joinRelationship($relation, $callback, 'leftJoin', $useAlias, $disableExtraConditions);
212213
};
213214
}
214215

215216
public function rightJoinRelationship(): Closure
216217
{
217-
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
218+
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
218219
return $this->joinRelationship($relation, $callback, 'rightJoin', $useAlias, $disableExtraConditions);
219220
};
220221
}
221222

222223
public function rightJoinRelation(): Closure
223224
{
224-
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
225+
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
225226
return $this->joinRelationship($relation, $callback, 'rightJoin', $useAlias, $disableExtraConditions);
226227
};
227228
}
@@ -314,7 +315,7 @@ public function joinNestedRelationship(): Closure
314315
*/
315316
public function orderByPowerJoins(): Closure
316317
{
317-
return function ($sort, $direction = 'asc', $aggregation = null, $joinType = 'join') {
318+
return function ($sort, string $direction = 'asc', $aggregation = null, $joinType = 'join') {
318319
if (is_array($sort)) {
319320
$relationships = explode('.', $sort[0]);
320321
$column = $sort[1];
@@ -366,7 +367,7 @@ public function orderByPowerJoins(): Closure
366367

367368
public function orderByLeftPowerJoins(): Closure
368369
{
369-
return function ($sort, $direction = 'asc') {
370+
return function ($sort, string $direction = 'asc') {
370371
return $this->orderByPowerJoins($sort, $direction, null, 'leftJoin');
371372
};
372373
}
@@ -376,14 +377,14 @@ public function orderByLeftPowerJoins(): Closure
376377
*/
377378
public function orderByPowerJoinsCount(): Closure
378379
{
379-
return function ($sort, $direction = 'asc') {
380+
return function ($sort, string $direction = 'asc') {
380381
return $this->orderByPowerJoins($sort, $direction, 'COUNT');
381382
};
382383
}
383384

384385
public function orderByLeftPowerJoinsCount(): Closure
385386
{
386-
return function ($sort, $direction = 'asc') {
387+
return function ($sort, string $direction = 'asc') {
387388
return $this->orderByPowerJoins($sort, $direction, 'COUNT', 'leftJoin');
388389
};
389390
}
@@ -393,14 +394,14 @@ public function orderByLeftPowerJoinsCount(): Closure
393394
*/
394395
public function orderByPowerJoinsSum(): Closure
395396
{
396-
return function ($sort, $direction = 'asc') {
397+
return function ($sort, string $direction = 'asc') {
397398
return $this->orderByPowerJoins($sort, $direction, 'SUM');
398399
};
399400
}
400401

401402
public function orderByLeftPowerJoinsSum(): Closure
402403
{
403-
return function ($sort, $direction = 'asc') {
404+
return function ($sort, string $direction = 'asc') {
404405
return $this->orderByPowerJoins($sort, $direction, 'SUM', 'leftJoin');
405406
};
406407
}
@@ -410,14 +411,14 @@ public function orderByLeftPowerJoinsSum(): Closure
410411
*/
411412
public function orderByPowerJoinsAvg(): Closure
412413
{
413-
return function ($sort, $direction = 'asc') {
414+
return function ($sort, string $direction = 'asc') {
414415
return $this->orderByPowerJoins($sort, $direction, 'AVG');
415416
};
416417
}
417418

418419
public function orderByLeftPowerJoinsAvg(): Closure
419420
{
420-
return function ($sort, $direction = 'asc') {
421+
return function ($sort, string $direction = 'asc') {
421422
return $this->orderByPowerJoins($sort, $direction, 'AVG', 'leftJoin');
422423
};
423424
}
@@ -427,14 +428,14 @@ public function orderByLeftPowerJoinsAvg(): Closure
427428
*/
428429
public function orderByPowerJoinsMin(): Closure
429430
{
430-
return function ($sort, $direction = 'asc') {
431+
return function ($sort, string $direction = 'asc') {
431432
return $this->orderByPowerJoins($sort, $direction, 'MIN');
432433
};
433434
}
434435

435436
public function orderByLeftPowerJoinsMin(): Closure
436437
{
437-
return function ($sort, $direction = 'asc') {
438+
return function ($sort, string $direction = 'asc') {
438439
return $this->orderByPowerJoins($sort, $direction, 'MIN', 'leftJoin');
439440
};
440441
}
@@ -444,14 +445,14 @@ public function orderByLeftPowerJoinsMin(): Closure
444445
*/
445446
public function orderByPowerJoinsMax(): Closure
446447
{
447-
return function ($sort, $direction = 'asc') {
448+
return function ($sort, string $direction = 'asc') {
448449
return $this->orderByPowerJoins($sort, $direction, 'MAX');
449450
};
450451
}
451452

452453
public function orderByLeftPowerJoinsMax(): Closure
453454
{
454-
return function ($sort, $direction = 'asc') {
455+
return function ($sort, string $direction = 'asc') {
455456
return $this->orderByPowerJoins($sort, $direction, 'MAX', 'leftJoin');
456457
};
457458
}
@@ -487,7 +488,7 @@ public function powerJoinHas(): Closure
487488

488489
public function hasNestedUsingJoins(): Closure
489490
{
490-
return function ($relations, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null): static {
491+
return function (string $relations, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null): static {
491492
$relations = explode('.', $relations);
492493

493494
/** @var Relation */
@@ -516,15 +517,15 @@ public function hasNestedUsingJoins(): Closure
516517

517518
public function powerJoinDoesntHave(): Closure
518519
{
519-
return function ($relation, $boolean = 'and', Closure $callback = null) {
520+
return function (string $relation, $boolean = 'and', Closure $callback = null) {
520521
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
521522
};
522523

523524
}
524525

525526
public function powerJoinWhereHas(): Closure
526527
{
527-
return function ($relation, $callback = null, $operator = '>=', $count = 1) {
528+
return function (string $relation, $callback = null, $operator = '>=', $count = 1) {
528529
return $this->powerJoinHas($relation, $operator, $count, 'and', $callback);
529530
};
530531
}

src/Mixins/QueryBuilderExtraMethods.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
namespace Kirschbaum\PowerJoins\Mixins;
44

5+
use Illuminate\Database\Query\Builder;
6+
7+
/**
8+
* @mixin Builder
9+
*/
510
class QueryBuilderExtraMethods
611
{
7-
public function getGroupBy()
12+
public function getGroupBy(): \Closure
813
{
9-
return function () {
14+
return function (): ?array {
1015
return $this->groups;
1116
};
1217
}
1318

14-
public function getSelect()
19+
public function getSelect(): \Closure
1520
{
16-
return function () {
21+
return function (): ?array {
1722
return $this->columns;
1823
};
1924
}

src/Mixins/QueryRelationshipExistence.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
namespace Kirschbaum\PowerJoins\Mixins;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Relations\Relation;
67

8+
/**
9+
* @mixin Builder
10+
*/
711
class QueryRelationshipExistence
812
{
9-
public function getGroupBy()
13+
public function getGroupBy(): \Closure
1014
{
11-
return function () {
15+
return function (): ?array {
1216
return $this->getQuery()->getGroupBy();
1317
};
1418
}
1519

16-
public function getSelect()
20+
public function getSelect(): \Closure
1721
{
18-
return function () {
22+
return function (): ?array {
1923
return $this->getQuery()->getSelect();
2024
};
2125
}
2226

23-
protected function getRelationWithoutConstraintsProxy()
27+
protected function getRelationWithoutConstraintsProxy(): \Closure
2428
{
25-
return function ($relation) {
26-
return Relation::noConstraints(function () use ($relation) {
27-
return $this->getModel()->{$relation}();
28-
});
29+
return function (string $relation): ?Relation {
30+
return Relation::noConstraints(fn () => $this->getModel()->{$relation}());
2931
};
3032
}
3133
}

0 commit comments

Comments
 (0)