Skip to content

Commit 076bb0b

Browse files
authored
Upgrade to PHPStan 2 (#119)
* Upgrade to PHPStan 2 * Upgrade to PHPStan 2 * Increase PHPStan level * Increase PHPStan level
1 parent f220226 commit 076bb0b

17 files changed

+75
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![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)
44
[![Code Coverage](https://codecov.io/gh/staudenmeir/eloquent-json-relations/graph/badge.svg?token=T41IX53I5U)](https://codecov.io/gh/staudenmeir/eloquent-json-relations)
5-
[![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)
5+
[![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)
66
[![Latest Stable Version](https://poser.pugx.org/staudenmeir/eloquent-json-relations/v/stable)](https://packagist.org/packages/staudenmeir/eloquent-json-relations)
77
[![Total Downloads](https://poser.pugx.org/staudenmeir/eloquent-json-relations/downloads)](https://packagist.org/packages/staudenmeir/eloquent-json-relations/stats)
88
[![License](https://poser.pugx.org/staudenmeir/eloquent-json-relations/license)](https://github.com/staudenmeir/eloquent-json-relations/blob/main/LICENSE)

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
},
1616
"require-dev": {
1717
"barryvdh/laravel-ide-helper": "^3.0",
18-
"larastan/larastan": "^2.9",
19-
"orchestra/testbench": "^9.0",
18+
"larastan/larastan": "^3.0",
19+
"laravel/framework": "^11.0",
20+
"mockery/mockery": "^1.5.1",
21+
"orchestra/testbench-core": "^9.5",
2022
"phpunit/phpunit": "^11.0",
2123
"staudenmeir/eloquent-has-many-deep": "^1.20"
2224
},

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parameters:
2-
level: 9
2+
level: 10
33
paths:
44
- src

phpstan.types.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
includes:
22
- ./vendor/larastan/larastan/extension.neon
33
parameters:
4-
level: 9
4+
level: 10
55
paths:
66
- types

src/Grammars/SqlServerGrammar.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function compileJsonObject($column, $levels)
3939
*/
4040
public function compileJsonValueSelect(string $column): string
4141
{
42+
/** @var string $field */
43+
/** @var string $path */
4244
[$field, $path] = $this->wrapJsonFieldAndPath($column);
4345

4446
return "json_query($field$path)";

src/Grammars/Traits/CompilesMySqlJsonQueries.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function compileMemberOf(string $column, ?string $objectKey, mixed $value
7171
{
7272
$columnWithKey = $objectKey ? $column . (str_contains($column, '->') ? '[*]' : '') . "->$objectKey" : $column;
7373

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

7678
if ($objectKey && !str_contains($column, '->')) {

src/HasJsonRelationships.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphMany as MorphManyPostgres;
2525
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphOne as MorphOnePostgres;
2626

27+
/**
28+
* @phpstan-ignore trait.unused
29+
*/
2730
trait HasJsonRelationships
2831
{
2932
/** @inheritDoc */

src/Relations/BelongsToJson.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ protected function relationExistenceQueryOwnerKey(Builder $query, string $ownerK
283283
*
284284
* @param TRelatedModel $model
285285
* @param TDeclaringModel $parent
286-
* @param list<array<string, mixed>> $records
286+
* @param array<int, array<string, mixed>> $records
287287
* @return array<string, mixed>
288288
*/
289289
public function pivotAttributes(Model $model, Model $parent, array $records)
@@ -301,22 +301,28 @@ public function pivotAttributes(Model $model, Model $parent, array $records)
301301
return Arr::get($value, $key) == $model->$ownerKey;
302302
})->first();
303303

304-
return Arr::except($record, $key);
304+
/** @var array<string, mixed> $result */
305+
$result = Arr::except($record, $key);
306+
307+
return $result;
305308
}
306309

307310
/**
308311
* Get the foreign key values.
309312
*
310313
* @param \Illuminate\Database\Eloquent\Model|null $model
311-
* @return list<mixed>
314+
* @return array<int, mixed>
312315
*/
313316
public function getForeignKeys(?Model $model = null)
314317
{
315318
$model = $model ?: $this->child;
316319

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

319-
return (new BaseCollection($model->$foreignKey))->filter(fn ($key) => $key !== null)->all();
322+
/** @var list<int|string|null> $foreignKeys */
323+
$foreignKeys = $model->$foreignKey;
324+
325+
return (new BaseCollection($foreignKeys))->filter(fn ($key) => $key !== null)->all();
320326
}
321327

322328
/**

src/Relations/HasManyJson.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ protected function matchOneOrMany(array $models, Collection $results, $relation,
163163

164164
if ($this->key) {
165165
foreach ($models as $model) {
166+
/** @var \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $relatedModels */
167+
$relatedModels = $model->$relation;
168+
166169
$this->hydratePivotRelation(
167-
$model->$relation,
170+
$relatedModels,
168171
$model,
169172
fn (Model $model) => $model->{$this->getPathName()}
170173
);
@@ -182,7 +185,10 @@ protected function buildDictionary(Collection $results)
182185
$dictionary = [];
183186

184187
foreach ($results as $result) {
185-
foreach ($result->{$foreign} as $value) {
188+
/** @var list<int|string|null> $foreignKeys */
189+
$foreignKeys = $result->$foreign;
190+
191+
foreach ($foreignKeys as $value) {
186192
$dictionary[$value][] = $result;
187193
}
188194
}
@@ -297,7 +303,7 @@ protected function relationExistenceQueryParentKey(Builder $query): array
297303
*
298304
* @param TRelatedModel $model
299305
* @param TDeclaringModel $parent
300-
* @param list<array<string, mixed>> $records
306+
* @param array<int, array<string, mixed>> $records
301307
* @return array<string, mixed>
302308
*/
303309
public function pivotAttributes(Model $model, Model $parent, array $records)
@@ -315,7 +321,10 @@ public function pivotAttributes(Model $model, Model $parent, array $records)
315321
return Arr::get($value, $key) == $parent->$localKey;
316322
})->first();
317323

318-
return Arr::except($record, $key);
324+
/** @var array<string, mixed> $result */
325+
$result = Arr::except($record, $key);
326+
327+
return $result;
319328
}
320329

321330
/**

src/Relations/HasOneJson.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ public function matchOne(array $models, Collection $results, $relation)
5454

5555
if ($this->key) {
5656
foreach ($models as $model) {
57-
/** @var \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $relatedModel */
58-
$relatedModel = new Collection(
59-
array_filter([$model->$relation])
57+
/** @var TRelatedModel|null $relatedModel */
58+
$relatedModel = $model->$relation;
59+
60+
/** @var \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> $relatedModels */
61+
$relatedModels = new Collection(
62+
array_filter([$relatedModel])
6063
);
6164

6265
$model->setRelation(
6366
$relation,
6467
$this->hydratePivotRelation(
65-
$relatedModel,
68+
$relatedModels,
6669
$model,
6770
fn (Model $model) => $model->{$this->getPathName()}
6871
)->first()

0 commit comments

Comments
 (0)