Skip to content

Commit

Permalink
Increase PHPStan level to 7
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 25, 2024
1 parent e33ebb3 commit 7e77531
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 85 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
includes:
- ./vendor/larastan/larastan/extension.neon
parameters:
level: 6
level: 7
paths:
- src
treatPhpDocTypesAsCertain: false
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/CompositeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class CompositeKey
*/
public function __construct(...$columns)
{
$this->columns = $columns;
$this->columns = array_values($columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ class HasManyMerged extends Base implements ConcatenableRelation
/**
* Append the relation's through parents, foreign and local keys to a deep relationship.
*
* @param list<\Illuminate\Database\Eloquent\Model> $through
* @param list<array{0: string,
* 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey> $foreignKeys
* @param list<array{0: string,
* 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey> $localKeys
* @param non-empty-list<string> $through
* @param non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null> $foreignKeys
* @param non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null> $localKeys
* @param int $position
* @return array{0: list<string>,
* 1: list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>,
* 2: list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>}
* @return array{0: non-empty-list<string>,
* 1: non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null>,
* 2: non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null>}
*/
public function appendToDeepRelationship(array $through, array $foreignKeys, array $localKeys, int $position): array
{
Expand Down
15 changes: 2 additions & 13 deletions src/Eloquent/Relations/Traits/ExecutesQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ public function getResults()
return parent::getResults();
}

/**
* Execute the query as a "select" statement.
*
* @param string|list<string> $columns
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model>
*/
/** @inheritDoc */
public function get($columns = ['*'])
{
$models = parent::get($columns);
Expand Down Expand Up @@ -111,13 +106,7 @@ public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName =
});
}

/**
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @return bool
*/
/** @inheritDoc */
public function chunk($count, callable $callback)
{
return $this->prepareQueryBuilder()->chunk($count, function (Collection $results) use ($callback) {
Expand Down
13 changes: 12 additions & 1 deletion src/Eloquent/Relations/Traits/HasEagerLoading.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Illuminate\Database\Eloquent\Collection;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait HasEagerLoading
{
/** @inheritDoc */
Expand All @@ -29,7 +33,14 @@ public function addEagerConstraints(array $models)
}
}

/** @inheritDoc */
/**
* Match the eagerly loaded results to their parents.
*
* @param array<int, TDeclaringModel> $models
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @param string $relation
* @return array<int, TDeclaringModel>
*/
public function match(array $models, Collection $results, $relation)
{
if ($this->customEagerMatchingCallbacks) {
Expand Down
9 changes: 8 additions & 1 deletion src/Eloquent/Relations/Traits/HasExistenceQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
if ($this->firstKey instanceof Closure || $this->localKey instanceof Closure) {
$this->performJoin($query);

/** @var callable $closureKey */
$closureKey = $this->firstKey instanceof Closure ? $this->firstKey : $this->localKey;

$closureKey($query, $parentQuery);
Expand Down Expand Up @@ -48,8 +49,14 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder

$query->getModel()->setTable($hash);

$from = is_string($parentQuery->getQuery()->from)
? $parentQuery->getQuery()->from
: (string) $parentQuery->getQuery()->from->getValue(
$parentQuery->getQuery()->getGrammar()
);

return $query->select($columns)->whereColumn(
$parentQuery->getQuery()->from.'.'.$this->localKey,
"$from.$this->localKey",
'=',
$this->getQualifiedFirstKeyName()
);
Expand Down
12 changes: 6 additions & 6 deletions src/Eloquent/Relations/Traits/IsConcatenable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ trait IsConcatenable
/**
* Append the relation's through parents, foreign and local keys to a deep relationship.
*
* @param list<\Illuminate\Database\Eloquent\Model> $through
* @param list<string|callable|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey> $foreignKeys
* @param list<string|callable|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey> $localKeys
* @param non-empty-list<string> $through
* @param non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null> $foreignKeys
* @param non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null> $localKeys
* @param int $position
* @return array{0: list<string>,
* 1: list<string|callable|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>,
* 2: list<string|callable|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>}
* @return array{0: non-empty-list<string>,
* 1: non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null>,
* 2: non-empty-list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey|null>}
*/
public function appendToDeepRelationship(array $through, array $foreignKeys, array $localKeys, int $position): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/Relations/Traits/JoinsThroughParents.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function throughParentJoins(Builder $query, Model $throughParent, Mode
{
$joins = [];

if ($localKey instanceof CompositeKey) {
if ($localKey instanceof CompositeKey && $foreignKey instanceof CompositeKey) {
foreach ($localKey->columns as $i => $column) {
$joins[] = [$column, $foreignKey->columns[$i]];
}
Expand Down
11 changes: 7 additions & 4 deletions src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait RetrievesIntermediateTables
/**
* The intermediate tables to retrieve.
*
* @var array<string, array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: list<string>}>
* @var array<string, array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null}>
*/
protected $intermediateTables = [];

Expand Down Expand Up @@ -104,7 +104,10 @@ protected function hydrateIntermediateRelations(array $models)
$prefix = $this->prefix($accessor);

if (str_contains($accessor, '.')) {
[$path, $key] = preg_split('/\.(?=[^.]*$)/', $accessor);
/** @var array{0: string, 1: string} $segments */
$segments = preg_split('/\.(?=[^.]*$)/', $accessor);

[$path, $key] = $segments;
} else {
[$path, $key] = [null, $accessor];
}
Expand All @@ -121,7 +124,7 @@ protected function hydrateIntermediateRelations(array $models)
* Get the intermediate relationship from the query.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: list<string>} $intermediateTable
* @param array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null} $intermediateTable
* @param string $prefix
* @return \Illuminate\Database\Eloquent\Model
*/
Expand Down Expand Up @@ -185,7 +188,7 @@ protected function prefix($accessor)
/**
* Get the intermediate tables.
*
* @return array<string, array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: list<string>}>
* @return array<string, array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null}>
*/
public function getIntermediateTables(): array
{
Expand Down
62 changes: 46 additions & 16 deletions src/Eloquent/Relations/Traits/SupportsCompositeKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Illuminate\Support\Collection as BaseCollection;
use Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait SupportsCompositeKeys
{
/**
Expand All @@ -27,13 +31,19 @@ protected function hasLeadingCompositeKey(): bool
*/
protected function addConstraintsWithCompositeKey(): void
{
$columns = array_slice($this->foreignKeys[0]->columns, 1, null, true);
/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $foreignKey */
$foreignKey = $this->foreignKeys[0];

/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $localKey */
$localKey = $this->localKeys[0];

$columns = array_slice($foreignKey->columns, 1, null, true);

foreach ($columns as $i => $column) {
$this->query->where(
$this->throughParent->qualifyColumn($column),
'=',
$this->farParent[$this->localKeys[0]->columns[$i]]
$this->farParent[$localKey->columns[$i]]
);
}
}
Expand All @@ -46,21 +56,27 @@ protected function addConstraintsWithCompositeKey(): void
*/
protected function addEagerConstraintsWithCompositeKey(array $models): void
{
/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $foreignKey */
$foreignKey = $this->foreignKeys[0];

/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $localKey */
$localKey = $this->localKeys[0];

$keys = (new BaseCollection($models))->map(
function (Model $model) {
function (Model $model) use ($localKey) {
return array_map(
fn (string $column) => $model[$column],
$this->localKeys[0]->columns
$localKey->columns
);
}
)->values()->unique(null, true)->all();

$this->query->where(
function (Builder $query) use ($keys) {
function (Builder $query) use ($foreignKey, $keys) {
foreach ($keys as $key) {
$query->orWhere(
function (Builder $query) use ($key) {
foreach ($this->foreignKeys[0]->columns as $i => $column) {
function (Builder $query) use ($foreignKey, $key) {
foreach ($foreignKey->columns as $i => $column) {
$query->where(
$this->throughParent->qualifyColumn($column),
'=',
Expand All @@ -77,19 +93,21 @@ function (Builder $query) use ($key) {
/**
* Match the eagerly loaded results to their parents for a leading composite key.
*
* @param list<\Illuminate\Database\Eloquent\Model> $models
* @param \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model> $results
* @param string $relation
* @return list<\Illuminate\Database\Eloquent\Model>
* @param array<int, TDeclaringModel> $models
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @param string $relation
* @return array<int, TDeclaringModel>
*/
protected function matchWithCompositeKey(array $models, Collection $results, string $relation): array
{
$dictionary = $this->buildDictionaryWithCompositeKey($results);

foreach ($models as $model) {
$values = [];
/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $localKey */
$localKey = $this->localKeys[0];

foreach ($this->localKeys[0]->columns as $column) {
foreach ($localKey->columns as $column) {
$values[] = $this->getDictionaryKey(
$model->getAttribute($column)
);
Expand Down Expand Up @@ -118,10 +136,13 @@ protected function buildDictionaryWithCompositeKey(Collection $results): array
{
$dictionary = [];

/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $foreignKey */
$foreignKey = $this->foreignKeys[0];

foreach ($results as $result) {
$values = [];

foreach ($this->foreignKeys[0]->columns as $i => $column) {
foreach ($foreignKey->columns as $i => $column) {
$alias = 'laravel_through_key' . ($i > 0 ? "_$i" : '');

$values[] = $result->$alias;
Expand All @@ -142,7 +163,10 @@ protected function buildDictionaryWithCompositeKey(Collection $results): array
*/
protected function shouldSelectWithCompositeKey(): array
{
$columns = array_slice($this->foreignKeys[0]->columns, 1, null, true);
/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $foreignKey */
$foreignKey = $this->foreignKeys[0];

$columns = array_slice($foreignKey->columns, 1, null, true);

return array_map(
fn ($column, $i) => $this->throughParent->qualifyColumn($column) . " as laravel_through_key_$i",
Expand All @@ -159,13 +183,19 @@ protected function shouldSelectWithCompositeKey(): array
*/
public function getRelationExistenceQueryWithCompositeKey(Builder $query): void
{
$columns = array_slice($this->localKeys[0]->columns, 1, null, true);
/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $foreignKey */
$foreignKey = $this->foreignKeys[0];

/** @var \Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey $localKey */
$localKey = $this->localKeys[0];

$columns = array_slice($localKey->columns, 1, null, true);

foreach ($columns as $i => $column) {
$query->whereColumn(
$this->farParent->qualifyColumn($column),
'=',
$this->throughParent->qualifyColumn($this->foreignKeys[0]->columns[$i])
$this->throughParent->qualifyColumn($foreignKey->columns[$i])
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Eloquent/Traits/ReversesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait ReversesRelationships
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
* TODO
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TDeclaringModel, TRelatedModel> $relation
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, $this>
*/
Expand All @@ -30,7 +30,7 @@ public function hasManyDeepFromReverse(HasManyDeep $relation): HasManyDeep
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
* TODO
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TDeclaringModel, TRelatedModel> $relation
* @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep<TRelatedModel, $this>
*/
Expand All @@ -46,12 +46,12 @@ public function hasOneDeepFromReverse(HasManyDeep $relation): HasOneDeep
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
* TODO
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, TDeclaringModel> $relation
* @return array{0: class-string<TDeclaringModel>,
* 1: list<string>,
* 2: list<string|callable|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>,
* 3: list<string|callable|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>}
* 2: list<callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>,
* 3: list<callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>}
*/
protected function hasOneOrManyDeepFromReverse(HasManyDeep $relation): array
{
Expand Down
Loading

0 comments on commit 7e77531

Please sign in to comment.