Skip to content

Commit

Permalink
Upgrade to PHPStan 2 (#119)
Browse files Browse the repository at this point in the history
* Upgrade to PHPStan 2

* Upgrade to PHPStan 2

* Increase PHPStan level

* Increase PHPStan level
  • Loading branch information
staudenmeir authored Dec 10, 2024
1 parent f220226 commit 076bb0b
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 29 deletions.
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-json-relations/actions/workflows/ci.yml/badge.svg)](https://github.com/staudenmeir/eloquent-json-relations/actions/workflows/ci.yml?query=branch%3Amain)
[![Code Coverage](https://codecov.io/gh/staudenmeir/eloquent-json-relations/graph/badge.svg?token=T41IX53I5U)](https://codecov.io/gh/staudenmeir/eloquent-json-relations)
[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-brightgreen.svg?style=flat)](https://github.com/staudenmeir/eloquent-json-relations/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-json-relations/actions/workflows/static-analysis.yml?query=branch%3Amain)
[![Latest Stable Version](https://poser.pugx.org/staudenmeir/eloquent-json-relations/v/stable)](https://packagist.org/packages/staudenmeir/eloquent-json-relations)
[![Total Downloads](https://poser.pugx.org/staudenmeir/eloquent-json-relations/downloads)](https://packagist.org/packages/staudenmeir/eloquent-json-relations/stats)
[![License](https://poser.pugx.org/staudenmeir/eloquent-json-relations/license)](https://github.com/staudenmeir/eloquent-json-relations/blob/main/LICENSE)
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^3.0",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^9.0",
"larastan/larastan": "^3.0",
"laravel/framework": "^11.0",
"mockery/mockery": "^1.5.1",
"orchestra/testbench-core": "^9.5",
"phpunit/phpunit": "^11.0",
"staudenmeir/eloquent-has-many-deep": "^1.20"
},
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 9
level: 10
paths:
- src
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
2 changes: 2 additions & 0 deletions src/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function compileJsonObject($column, $levels)
*/
public function compileJsonValueSelect(string $column): string
{
/** @var string $field */
/** @var string $path */
[$field, $path] = $this->wrapJsonFieldAndPath($column);

return "json_query($field$path)";
Expand Down
2 changes: 2 additions & 0 deletions src/Grammars/Traits/CompilesMySqlJsonQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function compileMemberOf(string $column, ?string $objectKey, mixed $value
{
$columnWithKey = $objectKey ? $column . (str_contains($column, '->') ? '[*]' : '') . "->$objectKey" : $column;

/** @var string $field */
/** @var string $path */
[$field, $path] = $this->wrapJsonFieldAndPath($columnWithKey);

if ($objectKey && !str_contains($column, '->')) {
Expand Down
3 changes: 3 additions & 0 deletions src/HasJsonRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphMany as MorphManyPostgres;
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphOne as MorphOnePostgres;

/**
* @phpstan-ignore trait.unused
*/
trait HasJsonRelationships
{
/** @inheritDoc */
Expand Down
14 changes: 10 additions & 4 deletions src/Relations/BelongsToJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ protected function relationExistenceQueryOwnerKey(Builder $query, string $ownerK
*
* @param TRelatedModel $model
* @param TDeclaringModel $parent
* @param list<array<string, mixed>> $records
* @param array<int, array<string, mixed>> $records
* @return array<string, mixed>
*/
public function pivotAttributes(Model $model, Model $parent, array $records)
Expand All @@ -301,22 +301,28 @@ public function pivotAttributes(Model $model, Model $parent, array $records)
return Arr::get($value, $key) == $model->$ownerKey;
})->first();

return Arr::except($record, $key);
/** @var array<string, mixed> $result */
$result = Arr::except($record, $key);

return $result;
}

/**
* Get the foreign key values.
*
* @param \Illuminate\Database\Eloquent\Model|null $model
* @return list<mixed>
* @return array<int, mixed>
*/
public function getForeignKeys(?Model $model = null)
{
$model = $model ?: $this->child;

$foreignKey = $this->hasCompositeKey() ? $this->foreignKey[0] : $this->foreignKey;

return (new BaseCollection($model->$foreignKey))->filter(fn ($key) => $key !== null)->all();
/** @var list<int|string|null> $foreignKeys */
$foreignKeys = $model->$foreignKey;

return (new BaseCollection($foreignKeys))->filter(fn ($key) => $key !== null)->all();
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/Relations/HasManyJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ protected function matchOneOrMany(array $models, Collection $results, $relation,

if ($this->key) {
foreach ($models as $model) {
/** @var \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $relatedModels */
$relatedModels = $model->$relation;

$this->hydratePivotRelation(
$model->$relation,
$relatedModels,
$model,
fn (Model $model) => $model->{$this->getPathName()}
);
Expand All @@ -182,7 +185,10 @@ protected function buildDictionary(Collection $results)
$dictionary = [];

foreach ($results as $result) {
foreach ($result->{$foreign} as $value) {
/** @var list<int|string|null> $foreignKeys */
$foreignKeys = $result->$foreign;

foreach ($foreignKeys as $value) {
$dictionary[$value][] = $result;
}
}
Expand Down Expand Up @@ -297,7 +303,7 @@ protected function relationExistenceQueryParentKey(Builder $query): array
*
* @param TRelatedModel $model
* @param TDeclaringModel $parent
* @param list<array<string, mixed>> $records
* @param array<int, array<string, mixed>> $records
* @return array<string, mixed>
*/
public function pivotAttributes(Model $model, Model $parent, array $records)
Expand All @@ -315,7 +321,10 @@ public function pivotAttributes(Model $model, Model $parent, array $records)
return Arr::get($value, $key) == $parent->$localKey;
})->first();

return Arr::except($record, $key);
/** @var array<string, mixed> $result */
$result = Arr::except($record, $key);

return $result;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Relations/HasOneJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ public function matchOne(array $models, Collection $results, $relation)

if ($this->key) {
foreach ($models as $model) {
/** @var \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $relatedModel */
$relatedModel = new Collection(
array_filter([$model->$relation])
/** @var TRelatedModel|null $relatedModel */
$relatedModel = $model->$relation;

/** @var \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $relatedModels */
$relatedModels = new Collection(
array_filter([$relatedModel])
);

$model->setRelation(
$relation,
$this->hydratePivotRelation(
$relatedModel,
$relatedModels,
$model,
fn (Model $model) => $model->{$this->getPathName()}
)->first()
Expand Down
7 changes: 6 additions & 1 deletion src/Relations/InteractsWithPivotRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ protected function decodeRecords()
continue;
}

/** @var array<string, mixed> $record */

$foreignKey = Arr::get($record, $key);

if (!is_null($foreignKey)) {
Expand Down Expand Up @@ -167,7 +169,10 @@ protected function encodeRecords(array $records, array $others)
protected function parseIds($value)
{
if ($value instanceof Model) {
return [$value->{$this->ownerKey}];
/** @var int|string $id */
$id = $value->{$this->ownerKey};

return [$id];
}

if ($value instanceof Collection) {
Expand Down
10 changes: 8 additions & 2 deletions src/Relations/Postgres/HasOneOrManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ protected function performJoin(?Builder $query = null)

if ($this->throughParentSoftDeletes()
&& method_exists($this->throughParent, 'getQualifiedDeletedAtColumn')) {
$query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
/** @var string $deletedAtColumn */
$deletedAtColumn = $this->throughParent->getQualifiedDeletedAtColumn();

$query->whereNull($deletedAtColumn);
}
}

Expand Down Expand Up @@ -114,7 +117,10 @@ public function getRelationExistenceQueryForThroughSelfRelation(Builder $query,

if ($this->throughParentSoftDeletes()
&& method_exists($this->throughParent, 'getDeletedAtColumn')) {
$query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn());
/** @var string $deletedAtColumn */
$deletedAtColumn = $this->throughParent->getDeletedAtColumn();

$query->whereNull("$hash.$deletedAtColumn");
}

/** @var string $parentFrom */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function addConstraintsWithCompositeKey(): void
/**
* Set the constraints for an eager load of the relation for a composite key.
*
* @param list<TDeclaringModel> $models
* @param array<int, TDeclaringModel> $models
* @return void
*/
protected function addEagerConstraintsWithCompositeKey(array $models): void
Expand Down Expand Up @@ -99,10 +99,10 @@ function (Builder $query) use ($key, $ownerKey) {
/**
* Match the eagerly loaded results to their parents for a composite key.
*
* @param list<TDeclaringModel> $models
* @param array<int, TDeclaringModel> $models
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @param string $relation
* @return list<TDeclaringModel>
* @return array<int, TDeclaringModel>
*/
protected function matchWithCompositeKey(array $models, Collection $results, string $relation): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function addConstraintsWithCompositeKey(): void
/**
* Set the constraints for an eager load of the relation for a composite key.
*
* @param list<TDeclaringModel> $models
* @param array<int, TDeclaringModel> $models
* @return void
*/
protected function addEagerConstraintsWithCompositeKey(array $models): void
Expand Down Expand Up @@ -101,11 +101,11 @@ function (Builder $query) use ($foreignKey, $key) {
/**
* Match the eagerly loaded results to their parents for a composite key.
*
* @param list<TDeclaringModel> $models
* @param array<int, TDeclaringModel> $models
* @param \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $results
* @param string $relation
* @param string $type
* @return list<TDeclaringModel>
* @return array<int, TDeclaringModel>
*/
protected function matchWithCompositeKey(array $models, Collection $results, string $relation, string $type): array
{
Expand Down Expand Up @@ -153,7 +153,10 @@ protected function buildDictionaryWithCompositeKey(Collection $results): array
$additionalColumns
);

foreach($result->$foreignKey as $value) {
/** @var list<int|string> $values */
$values = $result->$foreignKey;

foreach($values as $value) {
$values = [$value, ...$additionalValues];

$key = implode("\0", $values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ protected function buildDictionaryForDeepRelationship(Collection $results): arra
$key = $this->key ? str_replace('->', '.', $this->key) : null;

foreach ($results as $result) {
/** @var string|null $throughKey */
$throughKey = $result->getAttribute('laravel_through_key');

/** @var array<string, mixed> $values */
$values = json_decode($result->laravel_through_key ?? '[]', true);
$values = json_decode($throughKey ?? '[]', true);

if ($key) {
$values = array_filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function addEagerConstraintsToDeepRelationship(Builder $query, array $mod
*/
public function mergeWhereConstraints(Builder $query, Builder $from): Builder
{
/** @var array<int, mixed> $whereBindings */
$whereBindings = $from->getQuery()->getRawBindings()['where'] ?? [];

$wheres = $from->getQuery()->wheres;
Expand Down
3 changes: 2 additions & 1 deletion src/Relations/Traits/IsJsonRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function hydratePivotRelation(Collection $models, Model $parent, callable
*/
protected function pivotRelation(Model $model, Model $parent, callable $callback)
{
/** @var list<array<string, mixed>>|\Illuminate\Contracts\Support\Arrayable<int, array<string, mixed>> $records */
$records = $callback($model, $parent);

if ($records instanceof Arrayable) {
Expand All @@ -86,7 +87,7 @@ protected function pivotRelation(Model $model, Model $parent, callable $callback
*
* @param TRelatedModel $model
* @param TDeclaringModel $parent
* @param list<array<string, mixed>> $records
* @param array<int, array<string, mixed>> $records
* @return array<string, mixed>
*/
abstract public function pivotAttributes(Model $model, Model $parent, array $records);
Expand Down

0 comments on commit 076bb0b

Please sign in to comment.