Skip to content

Commit aa5201a

Browse files
author
Jakub Gawron
committed
Updated to use more types where possible.
1 parent d1f7a6e commit aa5201a

11 files changed

+68
-104
lines changed

src/ChildResourceHandler.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@
77

88
class ChildResourceHandler
99
{
10-
/** @var JsonResource|ResourceCollection */
11-
public $childResource;
10+
public JsonResource|ResourceCollection $childResource;
1211

1312
/** @var callable */
1413
public $resolver;
1514

16-
/**
17-
* ChildResourceHandler constructor.
18-
* @param JsonResource|ResourceCollection $childResource
19-
* @param callable $resolver
20-
*/
21-
public function __construct($childResource, callable $resolver)
15+
public function __construct(JsonResource|ResourceCollection $childResource, callable $resolver)
2216
{
2317
$this->childResource = $childResource;
2418
$this->resolver = $resolver;

src/DeferredValue.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,21 @@
77

88
abstract class DeferredValue
99
{
10-
/**
11-
* @var JsonResource|ResourceCollection
12-
*/
13-
public $resource;
10+
public JsonResource|ResourceCollection $resource;
1411

1512
/** @var callable|null */
1613
public $callback;
1714

1815
/** @var callable|null */
1916
public $resolver;
2017

21-
/**
22-
* DeferredValue constructor.
23-
* @param JsonResource $resource
24-
* @param callable|null $callback
25-
*/
26-
public function __construct($resource, ?callable $callback = null)
18+
public function __construct(JsonResource $resource, ?callable $callback = null)
2719
{
2820
$this->resource = $resource;
2921
$this->callback = $callback;
3022
}
3123

32-
/**
33-
* @param callable $resolver
34-
* @return $this
35-
*/
36-
public function useResolver(callable $resolver): self
24+
public function useResolver(callable $resolver): static
3725
{
3826
$this->resolver = $resolver;
3927

@@ -43,5 +31,5 @@ public function useResolver(callable $resolver): self
4331
/**
4432
* @param static[] $deferredValues
4533
*/
46-
abstract public static function resolve(array $deferredValues);
34+
abstract public static function resolve(array $deferredValues): void;
4735
}

src/Eloquent/EloquentDeferredValue.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ abstract class EloquentDeferredValue extends DeferredValue
1111
/**
1212
* @var string[]
1313
*/
14-
public $relations;
14+
public array $relations;
1515

16-
/**
17-
* EloquentDeferredValue constructor.
18-
* @param JsonResource|ResourceCollection $resource
19-
* @param array $relations
20-
* @param callable|null $callback
21-
*/
22-
public function __construct($resource, array $relations, ?callable $callback = null)
16+
public function __construct(JsonResource|ResourceCollection $resource, array $relations, ?callable $callback = null)
2317
{
2418
parent::__construct($resource, $callback);
2519
$this->relations = $relations;
@@ -28,7 +22,7 @@ public function __construct($resource, array $relations, ?callable $callback = n
2822
/**
2923
* @param static[] $deferredValues
3024
*/
31-
public static function resolve(array $deferredValues)
25+
public static function resolve(array $deferredValues): void
3226
{
3327
static::loadEloquentRelations($deferredValues);
3428

@@ -48,7 +42,6 @@ public static function resolve(array $deferredValues)
4842
/**
4943
* Begins eager loading eloquent model relations.
5044
* @param static[] $deferredValues
51-
* @return string
5245
*/
53-
abstract protected static function loadEloquentRelations(array $deferredValues);
46+
abstract protected static function loadEloquentRelations(array $deferredValues): void;
5447
}

src/Eloquent/Load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Load extends EloquentDeferredValue
1010
/**
1111
* @inheritDoc
1212
*/
13-
protected static function loadEloquentRelations(array $deferredValues)
13+
protected static function loadEloquentRelations(array $deferredValues): void
1414
{
1515
collect($deferredValues)->groupBy('relations')
1616
->each(function (Collection $collection, $relation) {

src/Eloquent/LoadCount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LoadCount extends EloquentDeferredValue
1010
/**
1111
* @inheritDoc
1212
*/
13-
protected static function loadEloquentRelations(array $deferredValues)
13+
protected static function loadEloquentRelations(array $deferredValues): void
1414
{
1515
collect($deferredValues)->groupBy('relations')
1616
->each(function (Collection $collection, $relation) {

src/Eloquent/LoadMissing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LoadMissing extends EloquentDeferredValue
1010
/**
1111
* @inheritDoc
1212
*/
13-
protected static function loadEloquentRelations(array $deferredValues)
13+
protected static function loadEloquentRelations(array $deferredValues): void
1414
{
1515
collect($deferredValues)->groupBy('relations')
1616
->each(function (Collection $collection, $relation) {

src/Eloquent/ResolvesEloquentResources.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
namespace Netsells\Http\Resources\Eloquent;
44

5+
use Netsells\Http\Resources\DeferredValue;
6+
use Netsells\Http\Resources\Json\JsonResource;
7+
58
trait ResolvesEloquentResources
69
{
710
/**
8-
* @var string
11+
* @var class-string<DeferredValue>
912
*/
10-
protected $defaultDeferredValueClass = LoadMissing::class;
13+
protected string $defaultDeferredValueClass = LoadMissing::class;
1114

1215
/**
1316
* @param string|string[] $relations
1417
* @param callable|null $fn
1518
* @return EloquentDeferredValue
1619
*/
17-
protected function load($relations, ?callable $fn = null): EloquentDeferredValue
20+
protected function load(string|array $relations, ?callable $fn = null): EloquentDeferredValue
1821
{
1922
return new Load($this, (array) $relations, $fn);
2023
}
@@ -24,7 +27,7 @@ protected function load($relations, ?callable $fn = null): EloquentDeferredValue
2427
* @param callable|null $fn
2528
* @return EloquentDeferredValue
2629
*/
27-
protected function loadMissing($relations, ?callable $fn = null): EloquentDeferredValue
30+
protected function loadMissing(string|array $relations, ?callable $fn = null): EloquentDeferredValue
2831
{
2932
return new LoadMissing($this, (array) $relations, $fn);
3033
}
@@ -34,7 +37,7 @@ protected function loadMissing($relations, ?callable $fn = null): EloquentDeferr
3437
* @param callable|null $fn
3538
* @return EloquentDeferredValue
3639
*/
37-
protected function loadCount($relations, ?callable $fn = null): EloquentDeferredValue
40+
protected function loadCount(string|array $relations, ?callable $fn = null): EloquentDeferredValue
3841
{
3942
return new LoadCount($this, (array) $relations, $fn);
4043
}
@@ -44,7 +47,7 @@ protected function loadCount($relations, ?callable $fn = null): EloquentDeferred
4447
* @param callable|null $fn
4548
* @return EloquentDeferredValue
4649
*/
47-
protected function use($relations, ?callable $fn = null): EloquentDeferredValue
50+
protected function use(string|array $relations, ?callable $fn = null): EloquentDeferredValue
4851
{
4952
return new $this->defaultDeferredValueClass($this, (array) $relations, $fn);
5053
}
@@ -53,31 +56,31 @@ protected function use($relations, ?callable $fn = null): EloquentDeferredValue
5356
* @param string ...$relations
5457
* @return EloquentDeferredValue
5558
*/
56-
protected function preload(...$relations): EloquentDeferredValue
59+
protected function preload(string ...$relations): EloquentDeferredValue
5760
{
5861
return new $this->defaultDeferredValueClass($this, $relations);
5962
}
6063

6164
/**
62-
* @param string $relationship
63-
* @param string $resourceClass
65+
* @param string $relation
66+
* @param class-string<JsonResource> $resourceClass
6467
* @return EloquentDeferredValue
6568
*/
66-
protected function many(string $relationship, string $resourceClass): EloquentDeferredValue
69+
protected function many(string $relation, string $resourceClass): EloquentDeferredValue
6770
{
68-
return $this->use($relationship, function ($model) use ($resourceClass) {
71+
return $this->use($relation, function ($model) use ($resourceClass) {
6972
return $resourceClass::collection($model);
7073
});
7174
}
7275

7376
/**
74-
* @param string $relationship
75-
* @param string $resourceClass
77+
* @param string $relation
78+
* @param class-string<JsonResource> $resourceClass
7679
* @return EloquentDeferredValue
7780
*/
78-
protected function one(string $relationship, string $resourceClass): EloquentDeferredValue
81+
protected function one(string $relation, string $resourceClass): EloquentDeferredValue
7982
{
80-
return $this->use($relationship, function ($model) use ($resourceClass) {
83+
return $this->use($relation, function ($model) use ($resourceClass) {
8184
return $resourceClass::make($model);
8285
});
8386
}

src/Json/AnonymousResourceCollection.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,7 @@
44

55
class AnonymousResourceCollection extends ResourceCollection
66
{
7-
/**
8-
* The name of the resource being collected.
9-
*
10-
* @var string
11-
*/
12-
public $collects;
13-
14-
/**
15-
* Create a new anonymous resource collection.
16-
*
17-
* @param mixed $resource
18-
* @param string $collects
19-
* @return void
20-
*/
21-
public function __construct($resource, $collects)
7+
public function __construct(mixed $resource, string $collects)
228
{
239
$this->collects = $collects;
2410

src/Json/JsonResource.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@
22

33
namespace Netsells\Http\Resources\Json;
44

5+
use Illuminate\Http\Request;
56
use Illuminate\Support\Collection;
7+
use Netsells\Http\Resources\DeferredValue;
68
use Netsells\Http\Resources\Eloquent\ResolvesEloquentResources;
79
use Netsells\Http\Resources\ResolvesResources;
810

911
class JsonResource extends \Illuminate\Http\Resources\Json\JsonResource
1012
{
11-
use ResolvesResources, ResolvesEloquentResources;
13+
use ResolvesResources;
14+
use ResolvesEloquentResources;
1215

1316
/**
14-
* Create new anonymous resource collection.
15-
*
16-
* @param mixed $resource
17-
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
17+
* @return DeferredValue|DeferredValue[]
1818
*/
19-
public static function collection($resource)
19+
public function preloads(/** @param Request $request */)
20+
{
21+
return [];
22+
}
23+
24+
public static function collection(mixed $resource): AnonymousResourceCollection
2025
{
2126
return tap(new AnonymousResourceCollection($resource, static::class), function ($collection) {
2227
if (property_exists(static::class, 'preserveKeys')) {
@@ -25,7 +30,7 @@ public static function collection($resource)
2530
});
2631
}
2732

28-
protected function beforeResolveRoot($request)
33+
protected function beforeResolveRoot(Request $request): void
2934
{
3035
$this->collectAndResolvePreloads($request, Collection::make([$this]));
3136
}

src/Json/ResourceCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Netsells\Http\Resources\Json;
44

5+
use Illuminate\Http\Request;
56
use Netsells\Http\Resources\ResolvesResources;
67

78
class ResourceCollection extends \Illuminate\Http\Resources\Json\ResourceCollection
89
{
910
use ResolvesResources;
1011

11-
protected function beforeResolveRoot($request)
12+
protected function beforeResolveRoot(Request $request): void
1213
{
1314
$this->collectAndResolvePreloads($request, $this->collection);
1415
}

0 commit comments

Comments
 (0)