Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to PHPStan 2 #251

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/staudenmeir/eloquent-has-many-deep/actions/workflows/ci.yml/badge.svg)](https://github.com/staudenmeir/eloquent-has-many-deep/actions/workflows/ci.yml?query=branch%3Amain)
[![Code Coverage](https://codecov.io/gh/staudenmeir/eloquent-has-many-deep/graph/badge.svg?token=H59fIf4mG6)](https://codecov.io/gh/staudenmeir/eloquent-has-many-deep)
[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-brightgreen.svg?style=flat)](https://github.com/staudenmeir/eloquent-has-many-deep/actions/workflows/static-analysis.yml?query=branch%3Amain)
[![PHPStan](https://img.shields.io/badge/PHPStan-level%2010-brightgreen.svg?style=flat)](https://github.com/staudenmeir/eloquent-has-many-deep/actions/workflows/static-analysis.yml?query=branch%3Amain)
[![Latest Stable Version](https://poser.pugx.org/staudenmeir/eloquent-has-many-deep/v/stable)](https://packagist.org/packages/staudenmeir/eloquent-has-many-deep)
[![Total Downloads](https://poser.pugx.org/staudenmeir/eloquent-has-many-deep/downloads)](https://packagist.org/packages/staudenmeir/eloquent-has-many-deep/stats)
[![License](https://poser.pugx.org/staudenmeir/eloquent-has-many-deep/license)](https://github.com/staudenmeir/eloquent-has-many-deep/blob/main/LICENSE)
Expand Down
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
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: 9
level: 10
paths:
- src
treatPhpDocTypesAsCertain: false
Expand Down
2 changes: 1 addition & 1 deletion phpstan.types.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
includes:
- ./vendor/larastan/larastan/extension.neon
parameters:
level: 9
level: 10
paths:
- types
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
20 changes: 12 additions & 8 deletions src/Eloquent/Relations/Traits/IsCustomizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@

namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
*/
trait IsCustomizable
{
/**
* The custom callbacks to run at the end of the get() method.
*
* @var list<callable>
* @var list<callable(\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>): void>
*/
protected array $postGetCallbacks = [];

/**
* The custom through key callback for an eager load of the relation.
*
* @var callable
* @var callable(string): string
*/
protected $customThroughKeyCallback = null;

/**
* The custom constraints callback for an eager load of the relation.
*
* @var callable
* @var callable(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, array<int, \Illuminate\Database\Eloquent\Model>): void
*/
protected $customEagerConstraintsCallback = null;

/**
* The custom matching callbacks for the eagerly loaded results.
*
* @var list<callable>
* @var list<callable(array<int, TDeclaringModel>, \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>, string, string=): array<int, TDeclaringModel>>
*/
protected array $customEagerMatchingCallbacks = [];

/**
* Set custom callbacks to run at the end of the get() method.
*
* @param list<callable> $callbacks
* @param list<callable(\Illuminate\Database\Eloquent\Collection<int, TRelatedModel>): void> $callbacks
* @return $this
*/
public function withPostGetCallbacks(array $callbacks): static
Expand All @@ -48,7 +52,7 @@ public function withPostGetCallbacks(array $callbacks): static
/**
* Set the custom through key callback for an eager load of the relation.
*
* @param callable $callback
* @param callable(string): string $callback
* @return $this
*/
public function withCustomThroughKeyCallback(callable $callback): static
Expand All @@ -61,7 +65,7 @@ public function withCustomThroughKeyCallback(callable $callback): static
/**
* Set the custom constraints callback for an eager load of the relation.
*
* @param callable $callback
* @param callable(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, array<int, \Illuminate\Database\Eloquent\Model>): void $callback
* @return $this
*/
public function withCustomEagerConstraintsCallback(callable $callback): static
Expand All @@ -74,7 +78,7 @@ public function withCustomEagerConstraintsCallback(callable $callback): static
/**
* Set a custom matching callback for the eagerly loaded results.
*
* @param callable $callback
* @param callable(array<int, TDeclaringModel>, \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>, string, string=): array<int, TDeclaringModel> $callback
* @return $this
*/
public function withCustomEagerMatchingCallback(callable $callback): static
Expand Down
4 changes: 4 additions & 0 deletions src/Eloquent/Relations/Traits/JoinsThroughParents.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function (JoinClause $join) use ($joins) {

if ($this->throughParentInstanceSoftDeletes($throughParent)
&& method_exists($throughParent, 'getQualifiedDeletedAtColumn')) {
/** @var string $column */
$column = $throughParent->getQualifiedDeletedAtColumn();

$query->withGlobalScope(__CLASS__ . ":$column", function (Builder $query) use ($column) {
Expand Down Expand Up @@ -84,6 +85,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
23 changes: 17 additions & 6 deletions src/Eloquent/Relations/Traits/RetrievesIntermediateTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
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<int, string>,
* class: class-string<\Illuminate\Database\Eloquent\Model>,
* postProcessor: callable(\Illuminate\Database\Eloquent\Model, array<string, mixed>): array<string, mixed>|null}>
*/
protected $intermediateTables = [];

Expand Down Expand Up @@ -40,7 +47,7 @@ public function withIntermediate($class, array $columns = ['*'], $accessor = nul
* @param list<string> $columns
* @param class-string<\Illuminate\Database\Eloquent\Model> $class
* @param string|null $accessor
* @param callable|null $postProcessor
* @param callable(\Illuminate\Database\Eloquent\Model, array<string, mixed>): array<string, mixed>|null $postProcessor
* @return $this
*/
public function withPivot(
Expand All @@ -54,6 +61,7 @@ public function withPivot(
/** @var \Illuminate\Database\Connection $connection */
$connection = $this->query->getConnection();

/** @var array<int, string> $columns */
$columns = $connection->getSchemaBuilder()->getColumnListing($table);
}

Expand All @@ -71,7 +79,7 @@ public function withPivot(
/**
* Get the intermediate columns for the relation.
*
* @return list<string>
* @return array<int, string>
*/
protected function intermediateColumns()
{
Expand All @@ -91,7 +99,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 +135,10 @@ 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<int, string>,
* class: class-string<\Illuminate\Database\Eloquent\Model>,
* postProcessor: callable(\Illuminate\Database\Eloquent\Model, array<string, mixed>): array<string, mixed>|null} $intermediateTable
* @param string $prefix
* @return \Illuminate\Database\Eloquent\Model
*/
Expand Down Expand Up @@ -191,7 +202,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<int, 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<int, \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<int, 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
Loading