Skip to content

Commit

Permalink
Run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Oct 2, 2024
1 parent 03257fb commit 257ddee
Show file tree
Hide file tree
Showing 30 changed files with 136 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ vendor
coverage
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
.envrc
4 changes: 2 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

$rules = [
'@Symfony' => true,
'strict_param' => true,
Expand All @@ -25,8 +26,7 @@
->ignoreDotFiles(true)
->ignoreVCS(true);


return (new PhpCsFixer\Config())
return (new PhpCsFixer\Config())
->setRules($rules)
->setFinder($finder)
->setRiskyAllowed(true)
Expand Down
1 change: 0 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
* You can place your custom package configuration in here.
*/
return [
//
];
8 changes: 4 additions & 4 deletions src/EloquentJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class EloquentJoins
*/
public static function registerEloquentMacros()
{
EloquentQueryBuilder::mixin(new JoinRelationship);
EloquentQueryBuilder::mixin(new QueryRelationshipExistence);
QueryBuilder::mixin(new QueryBuilderExtraMethods);
EloquentQueryBuilder::mixin(new JoinRelationship());
EloquentQueryBuilder::mixin(new QueryRelationshipExistence());
QueryBuilder::mixin(new QueryBuilderExtraMethods());

Relation::mixin(new RelationshipsExtraMethods);
Relation::mixin(new RelationshipsExtraMethods());
}
}
12 changes: 2 additions & 10 deletions src/JoinsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

class JoinsHelper
{
static array $instances = [];
public static array $instances = [];

protected function __construct()
{

}

public static function make(): static
Expand All @@ -24,8 +23,6 @@ public static function make(): static

/**
* Cache to not join the same relationship twice.
*
* @var array
*/
private array $joinRelationshipCache = [];

Expand All @@ -38,12 +35,8 @@ public static function make(): static
'rightJoin' => 'rightPowerJoin',
];


/**
* Format the join callback.
*
* @param mixed $callback
* @return mixed
*/
public function formatJoinCallback($callback)
{
Expand Down Expand Up @@ -71,7 +64,7 @@ public function generateAliasForRelationship(Relation $relation, string $relatio
/**
* Get the join alias name from all the different options.
*/
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): null|string|array
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): string|array|null
{
if ($callback) {
if (is_callable($callback)) {
Expand All @@ -98,7 +91,6 @@ public function getAliasName(bool $useAlias, Relation $relation, string $relatio
: null;
}


/**
* Checks if the relationship was already joined.
*/
Expand Down
24 changes: 14 additions & 10 deletions src/Mixins/JoinRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

/**
* @mixin Builder
*
* @method \Illuminate\Database\Eloquent\Model getModel()
* @property \Illuminate\Database\Eloquent\Builder $query
*
* @property Builder $query
*/
class JoinRelationship
{
Expand Down Expand Up @@ -79,7 +81,7 @@ public function rightPowerJoin(): Closure

public function newPowerJoinClause(): Closure
{
return function (QueryBuilder $parentQuery, $type, $table, Model $model = null) {
return function (QueryBuilder $parentQuery, $type, $table, ?Model $model = null) {
return new PowerJoinClause($parentQuery, $type, $table, $model);
};
}
Expand All @@ -95,7 +97,7 @@ public function joinRelationship(): Closure
$joinType = 'join',
$useAlias = false,
bool $disableExtraConditions = false,
string $morphable = null
?string $morphable = null,
) {
$joinType = JoinsHelper::$joinMethodsMap[$joinType] ?? $joinType;
$useAlias = is_string($callback) ? false : $useAlias;
Expand Down Expand Up @@ -210,7 +212,7 @@ public function joinRelation(): Closure
$callback = null,
$joinType = 'join',
$useAlias = false,
bool $disableExtraConditions = false
bool $disableExtraConditions = false,
) {
return $this->joinRelationship($relationName, $callback, $joinType, $useAlias, $disableExtraConditions);
};
Expand Down Expand Up @@ -255,7 +257,7 @@ public function joinNestedRelationship(): Closure
$joinType = 'join',
$useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null
?string $morphable = null,
) {
$relations = explode('.', $relationships);
$joinHelper = JoinsHelper::make($this->getModel());
Expand All @@ -265,7 +267,7 @@ public function joinNestedRelationship(): Closure
$part = [];
foreach ($relations as $relationName) {
$part[] = $relationName;
$fullRelationName = join(".", $part);
$fullRelationName = join('.', $part);

$currentModel = $latestRelation ? $latestRelation->getModel() : $this->getModel();
$relation = $currentModel->{$relationName}();
Expand Down Expand Up @@ -316,7 +318,6 @@ public function joinNestedRelationship(): Closure
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}


if ($joinHelper->relationshipAlreadyJoined($this->getModel(), $relationJoinCache)) {
$latestRelation = $relation;

Expand All @@ -337,6 +338,7 @@ public function joinNestedRelationship(): Closure
}

StaticCache::clear();

return $this;
};
}
Expand Down Expand Up @@ -412,6 +414,7 @@ public function orderByPowerJoins(): Closure
);
}
}

return $this;
};
}
Expand Down Expand Up @@ -513,7 +516,7 @@ public function orderByLeftPowerJoinsMax(): Closure
*/
public function powerJoinHas(): Closure
{
return function ($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null, string $morphable = null): static {
return function ($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure|array|null $callback = null, ?string $morphable = null): static {
if (is_null($this->getSelect())) {
$this->select(sprintf('%s.*', $this->getModel()->getTable()));
}
Expand Down Expand Up @@ -541,7 +544,7 @@ public function powerJoinHas(): Closure

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

/** @var Relation */
Expand All @@ -564,13 +567,14 @@ public function hasNestedUsingJoins(): Closure

$latestRelation = $relation;
}

return $this;
};
}

public function powerJoinDoesntHave(): Closure
{
return function ($relation, $boolean = 'and', Closure $callback = null) {
return function ($relation, $boolean = 'and', ?Closure $callback = null) {
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
};
}
Expand Down
55 changes: 27 additions & 28 deletions src/Mixins/RelationshipsExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
namespace Kirschbaum\PowerJoins\Mixins;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\PostgresConnection;
use Stringable;
use Illuminate\Support\Str;
use Kirschbaum\PowerJoins\StaticCache;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\Tests\Models\Post;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\MySqlConnection;
use Illuminate\Support\Str;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\StaticCache;

/**
* @method \Illuminate\Database\Eloquent\Model getModel()
Expand All @@ -37,19 +34,21 @@
* @method string getFirstKeyName()
* @method string getQualifiedLocalKeyName()
* @method string getExistenceCompareKey()
*
* @mixin \Illuminate\Database\Eloquent\Relations\Relation
* @mixin \Illuminate\Database\Eloquent\Relations\HasOneOrMany
* @mixin \Illuminate\Database\Eloquent\Relations\BelongsToMany
*
* @property \Illuminate\Database\Eloquent\Builder $query
* @property \Illuminate\Database\Eloquent\Model $parent
* @property \Illuminate\Database\Eloquent\Model $throughParent
* @property Model $parent
* @property Model $throughParent
* @property string $foreignKey
* @property string $parentKey
* @property string $ownerKey
* @property string $localKey
* @property string $secondKey
* @property string $secondLocalKey
* @property \Illuminate\Database\Eloquent\Model $farParent
* @property Model $farParent
*/
class RelationshipsExtraMethods
{
Expand All @@ -58,7 +57,7 @@ class RelationshipsExtraMethods
*/
public function performJoinForEloquentPowerJoins()
{
return function ($builder, $joinType = 'leftJoin', $callback = null, $alias = null, bool $disableExtraConditions = false, string $morphable = null, bool $hasCheck = false) {
return function ($builder, $joinType = 'leftJoin', $callback = null, $alias = null, bool $disableExtraConditions = false, ?string $morphable = null, bool $hasCheck = false) {
return match (true) {
$this instanceof MorphToMany => $this->performJoinForEloquentPowerJoinsForMorphToMany($builder, $joinType, $callback, $alias, $disableExtraConditions),
$this instanceof BelongsToMany => $this->performJoinForEloquentPowerJoinsForBelongsToMany($builder, $joinType, $callback, $alias, $disableExtraConditions),
Expand Down Expand Up @@ -253,9 +252,9 @@ protected function performJoinForEloquentPowerJoinsForMorph()
*/
protected function performJoinForEloquentPowerJoinsForMorphTo()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, string $morphable = null) {
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, ?string $morphable = null) {
/** @var Model */
$modelInstance = new $morphable;
$modelInstance = new $morphable();

$builder->{$joinType}($modelInstance->getTable(), function ($join) use ($modelInstance, $callback, $disableExtraConditions) {
$join->on(
Expand Down Expand Up @@ -292,7 +291,7 @@ protected function performJoinForEloquentPowerJoinsForHasMany()
$parentTable = StaticCache::getTableOrAliasForModel($this->parent);
$isOneOfMany = method_exists($this, 'isOneOfMany') ? $this->isOneOfMany() : false;

if ($isOneOfMany && ! $hasCheck) {
if ($isOneOfMany && !$hasCheck) {
$column = $this->getOneOfManySubQuery()->getQuery()->columns[0];
$fkColumn = $this->getOneOfManySubQuery()->getQuery()->columns[1];

Expand All @@ -304,7 +303,7 @@ protected function performJoinForEloquentPowerJoinsForHasMany()
$columnName = Str::of($columnValue)->after('(')->before(')')->__toString();
$columnName = Str::replace(['"', "'", '`'], '', $columnName);

if ($builder->getConnection() instanceof MySqlConnection) {
if ($builder->getConnection() instanceof MySqlConnection) {
$query->select('*')->from(function ($query) use ($joinedModel, $columnName, $fkColumn, $direction, $builder) {
$query
->select($joinedModel->getQualifiedKeyName())
Expand Down Expand Up @@ -396,7 +395,7 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
}
}, $this->getThroughParent());

$builder->{$joinType}($this->getModel()->getTable(), function (PowerJoinClause $join) use ($callback, $throughTable, $farTable, $alias1, $alias2) {
$builder->{$joinType}($this->getModel()->getTable(), function (PowerJoinClause $join) use ($callback, $throughTable, $farTable, $alias2) {
if ($alias2) {
$join->as($alias2);
}
Expand Down Expand Up @@ -425,9 +424,9 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
*/
public function performHavingForEloquentPowerJoins()
{
return function ($builder, $operator, $count, string $morphable = null) {
return function ($builder, $operator, $count, ?string $morphable = null) {
if ($morphable) {
$modelInstance = new $morphable;
$modelInstance = new $morphable();

$builder
->selectRaw(sprintf('count(%s) as %s_count', $modelInstance->getQualifiedKeyName(), Str::replace('.', '_', $modelInstance->getTable())))
Expand All @@ -445,12 +444,12 @@ public function performHavingForEloquentPowerJoins()
*/
public function usesSoftDeletes()
{
/**
/*
* @param \Illuminate\Database\Eloquent\Model|array $model
*/
return function ($model) {
if ($model instanceof Model) {
return in_array(SoftDeletes::class, class_uses_recursive($model));
return in_array(SoftDeletes::class, class_uses_recursive($model), true);
}

return array_key_exists(SoftDeletingScope::class, $model);
Expand Down Expand Up @@ -485,7 +484,7 @@ public function applyExtraConditions()
continue;
}

if (!in_array($condition['type'], ['Basic', 'Null', 'NotNull', 'Nested'])) {
if (!in_array($condition['type'], ['Basic', 'Null', 'NotNull', 'Nested'], true)) {
continue;
}

Expand Down Expand Up @@ -535,7 +534,7 @@ public function shouldNotApplyExtraCondition()
return true;
}

if (! $key = $this->getPowerJoinExistenceCompareKey()) {
if (!$key = $this->getPowerJoinExistenceCompareKey()) {
return true;
}

Expand All @@ -544,7 +543,7 @@ public function shouldNotApplyExtraCondition()
}

if (is_array($key)) {
return in_array($condition['column'], $key);
return in_array($condition['column'], $key, true);
}

return $condition['column'] === $key;
Expand Down
Loading

0 comments on commit 257ddee

Please sign in to comment.