Skip to content

Commit

Permalink
Upgrade to PHPStan 2
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Nov 17, 2024
1 parent 8966b76 commit 689532c
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 30 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"require-dev": {
"awobaz/compoships": "^2.3",
"barryvdh/laravel-ide-helper": "^3.0",
"illuminate/pagination": "^11.0",
"korridor/laravel-has-many-merged": "^1.1",
"larastan/larastan": "^2.9",
"larastan/larastan": "^3.0",
"laravel/framework": "^11.0",
"mockery/mockery": "^1.6",
"orchestra/testbench": "^9.0",
"orchestra/testbench-core": "^9.5",
"phpunit/phpunit": "^11.0",
"staudenmeir/eloquent-json-relations": "^1.11",
"staudenmeir/laravel-adjacency-list": "^1.21"
Expand Down
9 changes: 6 additions & 3 deletions src/Eloquent/Relations/Traits/ExecutesQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function get($columns = ['*'])
* @param string $pageName
* @param int|null $page
* @param int|null|\Closure $total
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, TRelatedModel>
*
* @throws \InvalidArgumentException
*/
Expand All @@ -65,6 +65,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',

$this->query->addSelect($columns);

/** @var \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, TRelatedModel> $paginator */
$paginator = $this->query->paginate($perPage, $columns, $pageName, $page, $total);

$this->hydrateIntermediateRelations(
Expand All @@ -81,7 +82,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
* @param list<string> $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\Paginator
* @return \Illuminate\Contracts\Pagination\Paginator<int, TRelatedModel>
*/
public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
Expand All @@ -92,6 +93,7 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p

$this->query->addSelect($columns);

/** @var \Illuminate\Contracts\Pagination\Paginator<int, TRelatedModel> $paginator */
$paginator = $this->query->simplePaginate($perPage, $columns, $pageName, $page);

$this->hydrateIntermediateRelations(
Expand All @@ -108,7 +110,7 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
* @param list<string> $columns
* @param string $cursorName
* @param string|null $cursor
* @return \Illuminate\Contracts\Pagination\CursorPaginator
* @return \Illuminate\Contracts\Pagination\CursorPaginator<int, TRelatedModel>
*/
public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null)
{
Expand All @@ -119,6 +121,7 @@ public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName =

$this->query->addSelect($columns);

/** @var \Illuminate\Contracts\Pagination\CursorPaginator<int, TRelatedModel> $paginator */
$paginator = $this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor);

$this->hydrateIntermediateRelations(
Expand Down
5 changes: 4 additions & 1 deletion src/Eloquent/Relations/Traits/HasEagerLoading.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public function addEagerConstraints(array $models)
parent::addEagerConstraints($models);

if (is_array($this->foreignKeys[0])) {
/** @var string $foreignKey */
$foreignKey = $this->foreignKeys[0][0];

$this->query->where(
$this->throughParent->qualifyColumn($this->foreignKeys[0][0]),
$this->throughParent->qualifyColumn($foreignKey),
'=',
$this->farParent->getMorphClass()
);
Expand Down
5 changes: 4 additions & 1 deletion src/Eloquent/Relations/Traits/HasExistenceQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
$query = parent::getRelationExistenceQuery($query, $parentQuery, $columns);

if (is_array($this->foreignKeys[0])) {
$column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);
/** @var string $foreignKey */
$foreignKey = $this->foreignKeys[0][0];

$column = $this->throughParent->qualifyColumn($foreignKey);

$query->where($column, '=', $this->farParent->getMorphClass());
} elseif ($this->hasLeadingCompositeKey()) {
Expand Down
3 changes: 3 additions & 0 deletions src/Eloquent/Relations/Traits/JoinsThroughParents.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ protected function throughParentJoins(Builder $query, Model $throughParent, Mode
$joins[] = [$column, $foreignKey->columns[$i]];
}
} else {
/** @var array{0: string, 1: string}|string $localKey */
/** @var array{0: string, 1: string}|string $foreignKey */

if (is_array($localKey)) {
$query->where($throughParent->qualifyColumn($localKey[0]), '=', $predecessor->getMorphClass());

Expand Down
14 changes: 9 additions & 5 deletions src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Str;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait RetrievesIntermediateTables
{
/**
* The intermediate tables to retrieve.
*
* @var array<string, array{table: string, columns: list<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null}>
* @var array<string, array{table: string, columns: array<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null}>
*/
protected $intermediateTables = [];

Expand Down Expand Up @@ -71,7 +75,7 @@ public function withPivot(
/**
* Get the intermediate columns for the relation.
*
* @return list<string>
* @return array<string>
*/
protected function intermediateColumns()
{
Expand All @@ -91,7 +95,7 @@ protected function intermediateColumns()
/**
* Hydrate the intermediate table relationships on the models.
*
* @param list<\Illuminate\Database\Eloquent\Model> $models
* @param array<TRelatedModel> $models
* @return void
*/
protected function hydrateIntermediateRelations(array $models)
Expand Down Expand Up @@ -127,7 +131,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: callable|null} $intermediateTable
* @param array{table: string, columns: array<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null} $intermediateTable
* @param string $prefix
* @return \Illuminate\Database\Eloquent\Model
*/
Expand Down Expand Up @@ -191,7 +195,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: callable|null}>
* @return array<string, array{table: string, columns: array<string>, class: class-string<\Illuminate\Database\Eloquent\Model>, postProcessor: callable|null}>
*/
public function getIntermediateTables(): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Eloquent/Relations/Traits/SupportsCompositeKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function addConstraintsWithCompositeKey(): void
/**
* Set the constraints for an eager load of the relation for a leading composite key.
*
* @param list<\Illuminate\Database\Eloquent\Model> $models
* @param array<\Illuminate\Database\Eloquent\Model> $models
* @return void
*/
protected function addEagerConstraintsWithCompositeKey(array $models): void
Expand Down Expand Up @@ -159,7 +159,7 @@ protected function buildDictionaryWithCompositeKey(Collection $results): array
/**
* Get the columns to select for a leading composite key.
*
* @return list<string>
* @return array<string>
*/
protected function shouldSelectWithCompositeKey(): array
{
Expand Down
5 changes: 4 additions & 1 deletion src/Eloquent/Traits/ConcatenatesNativeRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use RuntimeException;
use Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey;

/**
* @phpstan-ignore trait.unused
*/
trait ConcatenatesNativeRelationships
{
/**
Expand Down Expand Up @@ -232,7 +235,7 @@ protected function hasOneOrManyDeepFromMorphToMany(
/**
* Get the relationship method name.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $relation
* @return string
*/
protected function hasOneOrManyDeepRelationMethod(Relation $relation)
Expand Down
25 changes: 14 additions & 11 deletions src/Eloquent/Traits/ConcatenatesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
use Staudenmeir\EloquentHasManyDeepContracts\Interfaces\ConcatenableRelation;

/**
* @phpstan-ignore trait.unused
*/
trait ConcatenatesRelationships
{
use ConcatenatesNativeRelationships;

/**
* Define a has-many-deep relationship from existing relationships.
*
* @param callable|\Illuminate\Database\Eloquent\Relations\Relation<*> ...$relations
* @param callable|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> ...$relations
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep<\Illuminate\Database\Eloquent\Model, $this>
*/
public function hasManyDeepFromRelations(...$relations)
Expand Down Expand Up @@ -49,7 +52,7 @@ public function hasManyDeepFromRelations(...$relations)
/**
* Define a has-one-deep relationship from existing relationships.
*
* @param callable|\Illuminate\Database\Eloquent\Relations\Relation<*> ...$relations
* @param callable|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *> ...$relations
* @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep<\Illuminate\Database\Eloquent\Model, $this>
*/
public function hasOneDeepFromRelations(...$relations)
Expand Down Expand Up @@ -81,7 +84,7 @@ public function hasOneDeepFromRelations(...$relations)
/**
* Prepare a has-one-deep or has-many-deep relationship from existing relationships.
*
* @param list<callable|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model>> $relations
* @param list<callable|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>> $relations
* @return array{0: class-string<\Illuminate\Database\Eloquent\Model>,
* 1: list<string>,
* 2: list<array{0: string, 1: string}|callable|string|\Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey>,
Expand Down Expand Up @@ -173,8 +176,8 @@ protected function hasOneOrManyDeepFromRelations(array $relations)
/**
* Prepare the through parent class from an existing relationship and its successor.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $successor
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $successor
* @return string
*/
protected function hasOneOrManyThroughParent(Relation $relation, Relation $successor)
Expand Down Expand Up @@ -207,7 +210,7 @@ protected function hasOneOrManyThroughParent(Relation $relation, Relation $succe
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
* TODO
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, TDeclaringModel> $relation
* @param list<callable> $postGetCallbacks
* @param callable|null $customThroughKeyCallback
Expand Down Expand Up @@ -274,7 +277,7 @@ public function hasOneDeepFromRelationsWithConstraints(...$relations): HasOneDee
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
* TODO
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, TDeclaringModel> $deepRelation
* @param list<callable> $relations
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, TDeclaringModel>|\Staudenmeir\EloquentHasManyDeep\HasOneDeep<TRelatedModel, TDeclaringModel>
Expand Down Expand Up @@ -308,9 +311,9 @@ protected function addConstraintsToHasOneOrManyDeepRelationship(
*
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
* TODO
*
* @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel, TDeclaringModel> $deepRelation
* @param \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *> $relation
* @param bool $isLastRelation
* @return void
*/
Expand Down Expand Up @@ -350,8 +353,8 @@ protected function addRemovedScopesToHasOneOrManyDeepRelationship(
/**
* Normalize the relations from a variadic parameter.
*
* @param list<callable|list<callable|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model>>|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model>> $relations
* @return list<callable|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model>>
* @param list<callable|list<callable|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>>|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>> $relations
* @return list<callable|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>>
*/
protected function normalizeVariadicRelations(array $relations): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Eloquent/Traits/ReversesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;

/**
* @phpstan-ignore trait.unused
*/
trait ReversesRelationships
{
/**
Expand Down
10 changes: 7 additions & 3 deletions src/HasManyDeep.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class HasManyDeep extends HasManyThrough implements ConcatenableRelation
use IsConcatenable;
use IsCustomizable;
use JoinsThroughParents;
/** @use \Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits\RetrievesIntermediateTables<TRelatedModel, TDeclaringModel> */
use RetrievesIntermediateTables;
/** @use \Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits\SupportsCompositeKeys<TRelatedModel, TDeclaringModel> */
use SupportsCompositeKeys;
Expand Down Expand Up @@ -99,8 +100,11 @@ public function addConstraints()
} elseif ($this->localKey instanceof Closure) {
($this->localKey)($this->query);
} elseif (is_array($this->foreignKeys[0])) {
/** @var string $foreignKey */
$foreignKey = $this->foreignKeys[0][0];

$this->query->where(
$this->throughParent->qualifyColumn($this->foreignKeys[0][0]),
$this->throughParent->qualifyColumn($foreignKey),
'=',
$this->farParent->getMorphClass()
);
Expand Down Expand Up @@ -139,7 +143,7 @@ protected function performJoin(?Builder $query = null)
* Set the select clause for the relation query.
*
* @param list<string> $columns
* @return list<string>
* @return array<string>
*/
protected function shouldSelect(array $columns = ['*'])
{
Expand Down Expand Up @@ -242,8 +246,8 @@ public function getLocalKeys()
/**
* Convert the relationship to a "has one deep" relationship.
*
* @phpstan-ignore-next-line
* @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep<TRelatedModel, TDeclaringModel>
* @phpstan-ignore method.childReturnType
*/
public function one()
{
Expand Down
3 changes: 3 additions & 0 deletions src/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Staudenmeir\EloquentHasManyDeep\Eloquent\Traits\ConcatenatesRelationships;
use Staudenmeir\EloquentHasManyDeep\Eloquent\Traits\ReversesRelationships;

/**
* @phpstan-ignore trait.unused
*/
trait HasRelationships
{
use ConcatenatesRelationships;
Expand Down
3 changes: 3 additions & 0 deletions src/HasTableAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Staudenmeir\EloquentHasManyDeep;

/**
* @phpstan-ignore trait.unused
*/
trait HasTableAlias
{
/** @inheritDoc */
Expand Down

0 comments on commit 689532c

Please sign in to comment.