Skip to content

Commit df088f6

Browse files
committed
feat: add generics to eloquent Attribute cast
1 parent ebd115f commit df088f6

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/Illuminate/Database/Eloquent/Casts/Attribute.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
namespace Illuminate\Database\Eloquent\Casts;
44

5+
/**
6+
* @template TGet
7+
* @template TSet
8+
*/
59
class Attribute
610
{
711
/**
812
* The attribute accessor.
913
*
10-
* @var callable
14+
* @var (callable(mixed=, array<string, mixed>=): TGet)|null
1115
*/
1216
public $get;
1317

1418
/**
1519
* The attribute mutator.
1620
*
17-
* @var callable
21+
* @var (callable(TSet, array<string, mixed>=): mixed)|null
1822
*/
1923
public $set;
2024

@@ -35,8 +39,8 @@ class Attribute
3539
/**
3640
* Create a new attribute accessor / mutator.
3741
*
38-
* @param callable|null $get
39-
* @param callable|null $set
42+
* @param (callable(mixed=, array<string, mixed>=): TGet)|null $get
43+
* @param (callable(TSet, array<string, mixed>=): mixed)|null $set
4044
* @return void
4145
*/
4246
public function __construct(?callable $get = null, ?callable $set = null)
@@ -48,9 +52,12 @@ public function __construct(?callable $get = null, ?callable $set = null)
4852
/**
4953
* Create a new attribute accessor / mutator.
5054
*
51-
* @param callable|null $get
52-
* @param callable|null $set
53-
* @return static
55+
* @template TMakeGet
56+
* @template TMakeSet
57+
*
58+
* @param (callable(mixed=, array<string, mixed>=): TMakeGet)|null $get
59+
* @param (callable(TMakeSet, array<string, mixed>=): mixed)|null $set
60+
* @return static<TMakeGet, TMakeSet>
5461
*/
5562
public static function make(?callable $get = null, ?callable $set = null): static
5663
{
@@ -60,8 +67,10 @@ public static function make(?callable $get = null, ?callable $set = null): stati
6067
/**
6168
* Create a new attribute accessor.
6269
*
63-
* @param callable $get
64-
* @return static
70+
* @template T
71+
*
72+
* @param callable(mixed=, array<string, mixed>=): T $get
73+
* @return static<T, never>
6574
*/
6675
public static function get(callable $get)
6776
{
@@ -71,8 +80,10 @@ public static function get(callable $get)
7180
/**
7281
* Create a new attribute mutator.
7382
*
74-
* @param callable $set
75-
* @return static
83+
* @template T
84+
*
85+
* @param callable(T, array<string, mixed>=): mixed $set
86+
* @return static<never, T>
7687
*/
7788
public static function set(callable $set)
7889
{
@@ -82,7 +93,7 @@ public static function set(callable $set)
8293
/**
8394
* Disable object caching for the attribute.
8495
*
85-
* @return static
96+
* @return $this
8697
*/
8798
public function withoutObjectCaching()
8899
{
@@ -94,7 +105,7 @@ public function withoutObjectCaching()
94105
/**
95106
* Enable caching for the attribute.
96107
*
97-
* @return static
108+
* @return $this
98109
*/
99110
public function shouldCache()
100111
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Database\Eloquent\Casts\Attribute;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function test(): void
8+
{
9+
assertType(
10+
'Illuminate\Database\Eloquent\Casts\Attribute<int, never>',
11+
Attribute::get(fn () => 1),
12+
);
13+
assertType(
14+
'Illuminate\Database\Eloquent\Casts\Attribute<never, string>',
15+
Attribute::set(fn (string $v) => ['foo' => $v]),
16+
);
17+
assertType(
18+
'Illuminate\Database\Eloquent\Casts\Attribute<int, string>',
19+
new Attribute(fn () => 1, fn (string $v) => ['foo' => $v]),
20+
);
21+
}

0 commit comments

Comments
 (0)