Skip to content

Commit

Permalink
chore: add strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Nov 2, 2023
1 parent e55fa70 commit deed185
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 265 deletions.
2 changes: 1 addition & 1 deletion src/EloquentJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EloquentJoins
/**
* Register macros with Eloquent.
*/
public static function registerEloquentMacros()
public static function registerEloquentMacros(): void
{
EloquentQueryBuilder::mixin(new JoinRelationship);
EloquentQueryBuilder::mixin(new QueryRelationshipExistence);
Expand Down
6 changes: 4 additions & 2 deletions src/JoinsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class JoinsHelper
{
/**
* @var array<int, static>
*/
static array $instances = [];

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

Expand Down Expand Up @@ -71,7 +73,7 @@ public function generateAliasForRelationship(Relation $relation, string $relatio
/**
* Get the join alias name from all the different options.
*
* @return string|null
* @return array<string>|string|null
*/
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback)
{
Expand Down
49 changes: 25 additions & 24 deletions src/Mixins/JoinRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ public function powerJoin(): Closure
{
return function ($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): static {
$model = $operator instanceof Model ? $operator : null;
$join = $this->newPowerJoinClause($this->query, $type, $table, $model);
/** @var PowerJoinClause $join */
$join = $this->newPowerJoinClause($this->getQuery(), $type, $table, $model);

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

$this->query->joins[] = $join;
$this->getQuery()->joins[] = $join;

$this->query->addBinding($join->getBindings(), 'join');
$this->getQuery()->addBinding($join->getBindings(), 'join');
}

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

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

$this->query->addBinding($join->getBindings(), 'join');
$this->getQuery()->addBinding($join->getBindings(), 'join');
}

return $this;
Expand Down Expand Up @@ -200,28 +201,28 @@ public function joinRelation(): Closure

public function leftJoinRelationship(): Closure
{
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
return $this->joinRelationship($relation, $callback, 'leftJoin', $useAlias, $disableExtraConditions);
};
}

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

public function rightJoinRelationship(): Closure
{
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
return $this->joinRelationship($relation, $callback, 'rightJoin', $useAlias, $disableExtraConditions);
};
}

public function rightJoinRelation(): Closure
{
return function ($relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
return function (string $relation, $callback = null, $useAlias = false, bool $disableExtraConditions = false) {
return $this->joinRelationship($relation, $callback, 'rightJoin', $useAlias, $disableExtraConditions);
};
}
Expand Down Expand Up @@ -314,7 +315,7 @@ public function joinNestedRelationship(): Closure
*/
public function orderByPowerJoins(): Closure
{
return function ($sort, $direction = 'asc', $aggregation = null, $joinType = 'join') {
return function ($sort, string $direction = 'asc', $aggregation = null, $joinType = 'join') {
if (is_array($sort)) {
$relationships = explode('.', $sort[0]);
$column = $sort[1];
Expand Down Expand Up @@ -366,7 +367,7 @@ public function orderByPowerJoins(): Closure

public function orderByLeftPowerJoins(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, null, 'leftJoin');
};
}
Expand All @@ -376,14 +377,14 @@ public function orderByLeftPowerJoins(): Closure
*/
public function orderByPowerJoinsCount(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'COUNT');
};
}

public function orderByLeftPowerJoinsCount(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'COUNT', 'leftJoin');
};
}
Expand All @@ -393,14 +394,14 @@ public function orderByLeftPowerJoinsCount(): Closure
*/
public function orderByPowerJoinsSum(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'SUM');
};
}

public function orderByLeftPowerJoinsSum(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'SUM', 'leftJoin');
};
}
Expand All @@ -410,14 +411,14 @@ public function orderByLeftPowerJoinsSum(): Closure
*/
public function orderByPowerJoinsAvg(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'AVG');
};
}

public function orderByLeftPowerJoinsAvg(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'AVG', 'leftJoin');
};
}
Expand All @@ -427,14 +428,14 @@ public function orderByLeftPowerJoinsAvg(): Closure
*/
public function orderByPowerJoinsMin(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'MIN');
};
}

public function orderByLeftPowerJoinsMin(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'MIN', 'leftJoin');
};
}
Expand All @@ -444,14 +445,14 @@ public function orderByLeftPowerJoinsMin(): Closure
*/
public function orderByPowerJoinsMax(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'MAX');
};
}

public function orderByLeftPowerJoinsMax(): Closure
{
return function ($sort, $direction = 'asc') {
return function ($sort, string $direction = 'asc') {
return $this->orderByPowerJoins($sort, $direction, 'MAX', 'leftJoin');
};
}
Expand Down Expand Up @@ -487,7 +488,7 @@ public function powerJoinHas(): Closure

public function hasNestedUsingJoins(): Closure
{
return function ($relations, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null): static {
return function (string $relations, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null): static {
$relations = explode('.', $relations);

/** @var Relation */
Expand Down Expand Up @@ -516,15 +517,15 @@ public function hasNestedUsingJoins(): Closure

public function powerJoinDoesntHave(): Closure
{
return function ($relation, $boolean = 'and', Closure $callback = null) {
return function (string $relation, $boolean = 'and', Closure $callback = null) {
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
};

}

public function powerJoinWhereHas(): Closure
{
return function ($relation, $callback = null, $operator = '>=', $count = 1) {
return function (string $relation, $callback = null, $operator = '>=', $count = 1) {
return $this->powerJoinHas($relation, $operator, $count, 'and', $callback);
};
}
Expand Down
13 changes: 9 additions & 4 deletions src/Mixins/QueryBuilderExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

namespace Kirschbaum\PowerJoins\Mixins;

use Illuminate\Database\Query\Builder;

/**
* @mixin Builder
*/
class QueryBuilderExtraMethods
{
public function getGroupBy()
public function getGroupBy(): \Closure
{
return function () {
return function (): ?array {
return $this->groups;
};
}

public function getSelect()
public function getSelect(): \Closure
{
return function () {
return function (): ?array {
return $this->columns;
};
}
Expand Down
20 changes: 11 additions & 9 deletions src/Mixins/QueryRelationshipExistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

namespace Kirschbaum\PowerJoins\Mixins;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
* @mixin Builder
*/
class QueryRelationshipExistence
{
public function getGroupBy()
public function getGroupBy(): \Closure
{
return function () {
return function (): ?array {
return $this->getQuery()->getGroupBy();
};
}

public function getSelect()
public function getSelect(): \Closure
{
return function () {
return function (): ?array {
return $this->getQuery()->getSelect();
};
}

protected function getRelationWithoutConstraintsProxy()
protected function getRelationWithoutConstraintsProxy(): \Closure
{
return function ($relation) {
return Relation::noConstraints(function () use ($relation) {
return $this->getModel()->{$relation}();
});
return function (string $relation): ?Relation {
return Relation::noConstraints(fn () => $this->getModel()->{$relation}());
};
}
}
Loading

0 comments on commit deed185

Please sign in to comment.