Skip to content

Commit

Permalink
Increase PHPStan level
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 30, 2024
1 parent 5fb33a3 commit 247d7de
Show file tree
Hide file tree
Showing 32 changed files with 267 additions and 162 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: 6
paths:
- src
ignoreErrors:
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function toTree($childrenRelation = 'children')

$model = $this->first();

/** @var string $parentKeyName */
$parentKeyName = $model->getParentKeyName();

$localKeyName = $model->getLocalKeyName();
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Ancestors.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class Ancestors extends HasMany implements ConcatenableRelation
{
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\IsAncestorRelation<TRelatedModel, TDeclaringModel> */
use IsAncestorRelation;
use IsConcatenableAncestorsRelation;
}
46 changes: 36 additions & 10 deletions src/Eloquent/Relations/BelongsToManyOfDescendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class BelongsToManyOfDescendants extends BelongsToMany
{
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\IsOfDescendantsRelation<TRelatedModel, TDeclaringModel> */
use IsOfDescendantsRelation {
addConstraints as baseAddConstraints;
getRelationExistenceQuery as baseGetRelationExistenceQuery;
Expand Down Expand Up @@ -55,7 +56,12 @@ public function addConstraints()
$this->baseAddConstraints();
}

/** @inheritDoc */
/**
* Set the where clause on the recursive expression query.
*
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<\Illuminate\Database\Eloquent\Model> $query
* @return void
*/
protected function addExpressionWhereConstraints(Builder $query)
{
$column = $this->andSelf ? $this->parent->getLocalKeyName() : $this->parent->getParentKeyName();
Expand All @@ -67,19 +73,31 @@ protected function addExpressionWhereConstraints(Builder $query)
);
}

/** @inheritDoc */
/**
* Get the local key name for an eager load of the relation.
*
* @return string
*/
public function getEagerLoadingLocalKeyName()
{
return $this->parentKey;
}

/** @inheritDoc */
/**
* Get the foreign key name for an eager load of the relation.
*
* @return string
*/
public function getEagerLoadingForeignKeyName()
{
return $this->foreignPivotKey;
}

/** @inheritDoc */
/**
* Get the accessor for an eager load of the relation.
*
* @return string|null
*/
public function getEagerLoadingAccessor()
{
return $this->accessor;
Expand All @@ -88,10 +106,10 @@ public function getEagerLoadingAccessor()
/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parentQuery
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<TRelatedModel> $query
* @param \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<TDeclaringModel> $parentQuery
* @param list<string|\Illuminate\Database\Query\Expression>|string|\Illuminate\Database\Query\Expression $columns
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder<TRelatedModel>
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
Expand All @@ -100,13 +118,21 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
return $this->baseGetRelationExistenceQuery($query, $parentQuery, $columns);
}

/** @inheritDoc */
/**
* Get the local key name for the recursion expression.
*
* @return string
*/
public function getExpressionLocalKeyName()
{
return $this->parentKey;
}

/** @inheritDoc */
/**
* Get the foreign key name for the recursion expression.
*
* @return string
*/
public function getExpressionForeignKeyName()
{
return $this->foreignPivotKey;
Expand Down
9 changes: 8 additions & 1 deletion src/Eloquent/Relations/Bloodline.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $localKe
parent::__construct($query, $parent, $foreignKey, $localKey, true);
}

/** @inheritDoc */
/**
* Add a recursive expression to the query.
*
* @param callable $constraint
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel>|null $query
* @param string|null $from
* @return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
*/
protected function addExpression(callable $constraint, ?Builder $query = null, $from = null)
{
$query = $query ?: $this->query;
Expand Down
5 changes: 3 additions & 2 deletions src/Eloquent/Relations/Descendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class Descendants extends HasMany implements ConcatenableRelation
{
use IsConcatenableDescendantsRelation;
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\IsRecursiveRelation<TRelatedModel, TDeclaringModel> */
use IsRecursiveRelation {
buildDictionary as baseBuildDictionary;
}
Expand Down Expand Up @@ -155,9 +156,9 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder
* Add a recursive expression to the query.
*
* @param callable $constraint
* @param \Illuminate\Database\Eloquent\Builder|null $query
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel>|null $query
* @param string|null $from
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
*/
protected function addExpression(callable $constraint, ?Builder $query = null, $from = null)
{
Expand Down
12 changes: 7 additions & 5 deletions src/Eloquent/Relations/Graph/Ancestors.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
class Ancestors extends BelongsToMany implements ConcatenableRelation
{
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\Concatenation\IsConcatenableAncestorsRelation<TRelatedModel, TDeclaringModel> */
use IsConcatenableAncestorsRelation;
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\IsRecursiveRelation<TRelatedModel, TDeclaringModel> */
use IsRecursiveRelation {
buildDictionary as baseBuildDictionary;
}
Expand Down Expand Up @@ -113,9 +115,9 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
/**
* Add the constraints for a relationship query on the same table.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param list<string|\Illuminate\Database\Query\Expression>|string|\Illuminate\Database\Query\Expression $columns
* @return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
*/
public function getRelationExistenceQueryForSelfRelation(
Builder $query,
Expand Down Expand Up @@ -152,10 +154,10 @@ public function getRelationExistenceQueryForSelfRelation(
* Add a recursive expression to the query.
*
* @param callable $constraint
* @param \Illuminate\Database\Eloquent\Builder|null $query
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel>|null $query
* @param string|null $from
* @param string $union
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
*/
protected function addExpression(
callable $constraint,
Expand Down
12 changes: 7 additions & 5 deletions src/Eloquent/Relations/Graph/Descendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
class Descendants extends BelongsToMany implements ConcatenableRelation
{
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\Concatenation\IsConcatenableDescendantsRelation<TRelatedModel, TDeclaringModel> */
use IsConcatenableDescendantsRelation;
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\IsRecursiveRelation<TRelatedModel, TDeclaringModel> */
use IsRecursiveRelation {
buildDictionary as baseBuildDictionary;
}
Expand Down Expand Up @@ -113,9 +115,9 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
/**
* Add the constraints for a relationship query on the same table.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param list<string|\Illuminate\Database\Query\Expression>|string|\Illuminate\Database\Query\Expression $columns
* @return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
*/
public function getRelationExistenceQueryForSelfRelation(
Builder $query,
Expand Down Expand Up @@ -152,10 +154,10 @@ public function getRelationExistenceQueryForSelfRelation(
* Add a recursive expression to the query.
*
* @param callable $constraint
* @param \Illuminate\Database\Eloquent\Builder|null $query
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel>|null $query
* @param string|null $from
* @param string $union
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
*/
protected function addExpression(
callable $constraint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\Concatenation;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait IsConcatenableAncestorsRelation
{
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\Concatenation\IsConcatenableRelation<TRelatedModel, TDeclaringModel> */
use IsConcatenableRelation;

/**
* Get the custom through key for an eager load of the relation.
*
* @param string $alias
* @return array
* @return array{string, string}
*/
public function getThroughKeyForDeepRelationships(string $alias): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\Concatenation;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait IsConcatenableDescendantsRelation
{
/** @use \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\Concatenation\IsConcatenableRelation<TRelatedModel, TDeclaringModel> */
use IsConcatenableRelation;

/**
* Get the custom through key for an eager load of the relation.
*
* @param string $alias
* @return array
* @return array{string, string}
*/
public function getThroughKeyForDeepRelationships(string $alias): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@
use Illuminate\Database\PostgresConnection;
use RuntimeException;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait IsConcatenableRelation
{
/**
* Append the relation's through parents, foreign and local keys to a deep relationship.
*
* @param string[] $through
* @param array $foreignKeys
* @param array $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
* @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 Expand Up @@ -56,7 +62,7 @@ public function getTableForDeepRelationship(): string
/**
* The custom callback to run at the end of the get() method.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $models
* @return void
*/
public function postGetCallback(Collection $models): void
Expand All @@ -79,7 +85,7 @@ public function postGetCallback(Collection $models): void
/**
* Replace the separator in a PostgreSQL path column.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $models
* @param string $column
* @param string $separator
* @return void
Expand All @@ -98,8 +104,8 @@ protected function replacePathSeparator(Collection $models, string $column, stri
/**
* Set the constraints for an eager load of the deep relation.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model[] $models
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param list<\Illuminate\Database\Eloquent\Model> $models
* @return void
*/
public function addEagerConstraintsToDeepRelationship(Builder $query, array $models): void
Expand All @@ -114,9 +120,9 @@ public function addEagerConstraintsToDeepRelationship(Builder $query, array $mod
/**
* Merge the common table expressions from one query into another.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $from
* @return \Illuminate\Database\Eloquent\Builder
* @param \Illuminate\Database\Eloquent\Builder<*> $query
* @param \Illuminate\Database\Eloquent\Builder<*> $from
* @return \Illuminate\Database\Eloquent\Builder<*>
*/
protected function mergeExpressions(Builder $query, Builder $from): Builder
{
Expand All @@ -142,11 +148,11 @@ protected function mergeExpressions(Builder $query, Builder $from): Builder
/**
* Match the eagerly loaded results for a deep relationship to their parents.
*
* @param \Illuminate\Database\Eloquent\Model[] $models
* @param list<\Illuminate\Database\Eloquent\Model> $models
* @param \Illuminate\Database\Eloquent\Collection<array-key, \Illuminate\Database\Eloquent\Model> $results
* @param string $relation
* @param string $type
* @return array
* @return list<\Illuminate\Database\Eloquent\Model>
*/
public function matchResultsForDeepRelationship(
array $models,
Expand Down Expand Up @@ -177,7 +183,7 @@ public function matchResultsForDeepRelationship(
* Build the model dictionary for a deep relation.
*
* @param \Illuminate\Database\Eloquent\Collection<array-key, \Illuminate\Database\Eloquent\Model> $results
* @return array<string, \Illuminate\Database\Eloquent\Model[]>
* @return array<string, list<\Illuminate\Database\Eloquent\Model>>
*/
protected function buildDictionaryForDeepRelationship(Collection $results): array
{
Expand Down
Loading

0 comments on commit 247d7de

Please sign in to comment.