Skip to content

Commit

Permalink
feat: add generics to eloquent Attribute cast
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jan 26, 2025
1 parent ebd115f commit df088f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Illuminate/Database/Eloquent/Casts/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace Illuminate\Database\Eloquent\Casts;

/**
* @template TGet
* @template TSet
*/
class Attribute
{
/**
* The attribute accessor.
*
* @var callable
* @var (callable(mixed=, array<string, mixed>=): TGet)|null
*/
public $get;

/**
* The attribute mutator.
*
* @var callable
* @var (callable(TSet, array<string, mixed>=): mixed)|null
*/
public $set;

Expand All @@ -35,8 +39,8 @@ class Attribute
/**
* Create a new attribute accessor / mutator.
*
* @param callable|null $get
* @param callable|null $set
* @param (callable(mixed=, array<string, mixed>=): TGet)|null $get
* @param (callable(TSet, array<string, mixed>=): mixed)|null $set
* @return void
*/
public function __construct(?callable $get = null, ?callable $set = null)
Expand All @@ -48,9 +52,12 @@ public function __construct(?callable $get = null, ?callable $set = null)
/**
* Create a new attribute accessor / mutator.
*
* @param callable|null $get
* @param callable|null $set
* @return static
* @template TMakeGet
* @template TMakeSet
*
* @param (callable(mixed=, array<string, mixed>=): TMakeGet)|null $get
* @param (callable(TMakeSet, array<string, mixed>=): mixed)|null $set
* @return static<TMakeGet, TMakeSet>
*/
public static function make(?callable $get = null, ?callable $set = null): static
{
Expand All @@ -60,8 +67,10 @@ public static function make(?callable $get = null, ?callable $set = null): stati
/**
* Create a new attribute accessor.
*
* @param callable $get
* @return static
* @template T
*
* @param callable(mixed=, array<string, mixed>=): T $get
* @return static<T, never>
*/
public static function get(callable $get)
{
Expand All @@ -71,8 +80,10 @@ public static function get(callable $get)
/**
* Create a new attribute mutator.
*
* @param callable $set
* @return static
* @template T
*
* @param callable(T, array<string, mixed>=): mixed $set
* @return static<never, T>
*/
public static function set(callable $set)
{
Expand All @@ -82,7 +93,7 @@ public static function set(callable $set)
/**
* Disable object caching for the attribute.
*
* @return static
* @return $this
*/
public function withoutObjectCaching()
{
Expand All @@ -94,7 +105,7 @@ public function withoutObjectCaching()
/**
* Enable caching for the attribute.
*
* @return static
* @return $this
*/
public function shouldCache()
{
Expand Down
21 changes: 21 additions & 0 deletions types/Database/Eloquent/Casts/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Eloquent\Casts\Attribute;

use function PHPStan\Testing\assertType;

function test(): void
{
assertType(
'Illuminate\Database\Eloquent\Casts\Attribute<int, never>',
Attribute::get(fn () => 1),
);
assertType(
'Illuminate\Database\Eloquent\Casts\Attribute<never, string>',
Attribute::set(fn (string $v) => ['foo' => $v]),
);
assertType(
'Illuminate\Database\Eloquent\Casts\Attribute<int, string>',
new Attribute(fn () => 1, fn (string $v) => ['foo' => $v]),
);
}

0 comments on commit df088f6

Please sign in to comment.