Skip to content

Commit e4bbe32

Browse files
authored
fix: TypeError in DataCaster initialization (#9738)
* fix: TypeError in `DataCaster` init * fix: Apply suggestion * refactor: Remove unnecessary comments * fix: Revert to phpstan types
1 parent 752d0c8 commit e4bbe32

File tree

16 files changed

+54
-102
lines changed

16 files changed

+54
-102
lines changed

system/DataCaster/DataCaster.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
use CodeIgniter\Entity\Exceptions\CastException;
2929
use CodeIgniter\Exceptions\InvalidArgumentException;
3030

31+
/**
32+
* @phpstan-type cast_handlers array<string, class-string<EntityCastInterface|CastInterface>>
33+
*
34+
* @see CodeIgniter\DataCaster\DataCasterTest
35+
* @see CodeIgniter\Entity\EntityTest
36+
*/
3137
final class DataCaster
3238
{
3339
/**
@@ -38,9 +44,9 @@ final class DataCaster
3844
private array $types = [];
3945

4046
/**
41-
* Convert handlers
47+
* Convert handlers.
4248
*
43-
* @var array<string, class-string> [type => classname]
49+
* @var cast_handlers [type => classname]
4450
*/
4551
private array $castHandlers = [
4652
'array' => ArrayCast::class,
@@ -59,18 +65,18 @@ final class DataCaster
5965
];
6066

6167
/**
62-
* @param array<string, class-string>|null $castHandlers Custom convert handlers
63-
* @param array<string, string>|null $types [field => type]
64-
* @param object|null $helper Helper object.
65-
* @param bool $strict Strict mode? Set to false for casts for Entity.
68+
* @param cast_handlers|null $castHandlers Custom convert handlers
69+
* @param array<string, string>|null $types [field => type]
70+
* @param object|null $helper Helper object.
71+
* @param bool $strict Strict mode? Set to `false` for casts for Entity.
6672
*/
6773
public function __construct(
6874
?array $castHandlers = null,
6975
?array $types = null,
7076
private readonly ?object $helper = null,
7177
private readonly bool $strict = true,
7278
) {
73-
$this->castHandlers = array_merge($this->castHandlers, $castHandlers);
79+
$this->castHandlers = array_merge($this->castHandlers, $castHandlers ?? []);
7480

7581
if ($types !== null) {
7682
$this->setTypes($types);
@@ -113,12 +119,16 @@ public function setTypes(array $types): static
113119
* Add ? at the beginning of the type (i.e. ?string) to get `null`
114120
* instead of casting $value when $value is null.
115121
*
116-
* @param mixed $value The value to convert
117-
* @param string $field The field name
118-
* @param 'get'|'set' $method Allowed to "get" and "set"
122+
* @param mixed $value The value to convert
123+
* @param string $field The field name
124+
* @param string $method Allowed to "get" and "set"
119125
*/
120126
public function castAs(mixed $value, string $field, string $method = 'get'): mixed
121127
{
128+
if ($method !== 'get' && $method !== 'set') {
129+
throw CastException::forInvalidMethod($method);
130+
}
131+
122132
// If the type is not defined, return as it is.
123133
if (! isset($this->types[$field])) {
124134
return $value;

system/Entity/Cast/ArrayCast.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
/**
17-
* Class ArrayCast
18-
*/
1916
class ArrayCast extends BaseCast
2017
{
21-
/**
22-
* {@inheritDoc}
23-
*/
2418
public static function get($value, array $params = []): array
2519
{
2620
if (is_string($value) && (str_starts_with($value, 'a:') || str_starts_with($value, 's:'))) {
@@ -30,9 +24,6 @@ public static function get($value, array $params = []): array
3024
return (array) $value;
3125
}
3226

33-
/**
34-
* {@inheritDoc}
35-
*/
3627
public static function set($value, array $params = []): string
3728
{
3829
return serialize($value);

system/Entity/Cast/BaseCast.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,13 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
/**
17-
* Class BaseCast
18-
*/
1916
abstract class BaseCast implements CastInterface
2017
{
21-
/**
22-
* Get
23-
*
24-
* @param array|bool|float|int|object|string|null $value Data
25-
* @param array $params Additional param
26-
*
27-
* @return array|bool|float|int|object|string|null
28-
*/
2918
public static function get($value, array $params = [])
3019
{
3120
return $value;
3221
}
3322

34-
/**
35-
* Set
36-
*
37-
* @param array|bool|float|int|object|string|null $value Data
38-
* @param array $params Additional param
39-
*
40-
* @return array|bool|float|int|object|string|null
41-
*/
4223
public static function set($value, array $params = [])
4324
{
4425
return $value;

system/Entity/Cast/BooleanCast.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
/**
17-
* Class BooleanCast
18-
*/
1916
class BooleanCast extends BaseCast
2017
{
21-
/**
22-
* {@inheritDoc}
23-
*/
2418
public static function get($value, array $params = []): bool
2519
{
2620
return (bool) $value;

system/Entity/Cast/CSVCast.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,13 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
/**
17-
* Class CSVCast
18-
*/
1916
class CSVCast extends BaseCast
2017
{
21-
/**
22-
* {@inheritDoc}
23-
*/
2418
public static function get($value, array $params = []): array
2519
{
2620
return explode(',', $value);
2721
}
2822

29-
/**
30-
* {@inheritDoc}
31-
*/
3223
public static function set($value, array $params = []): string
3324
{
3425
return implode(',', $value);

system/Entity/Cast/CastInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
namespace CodeIgniter\Entity\Cast;
1515

1616
/**
17-
* Interface CastInterface
18-
*
1917
* The methods work at (1)(4) only.
2018
* [App Code] --- (1) --> [Entity] --- (2) --> [Database]
2119
* [App Code] <-- (4) --- [Entity] <-- (3) --- [Database]

system/Entity/Cast/DatetimeCast.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
use DateTime;
1818
use Exception;
1919

20-
/**
21-
* Class DatetimeCast
22-
*/
2320
class DatetimeCast extends BaseCast
2421
{
2522
/**

system/Entity/Cast/FloatCast.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
/**
17-
* Class FloatCast
18-
*/
1916
class FloatCast extends BaseCast
2017
{
21-
/**
22-
* {@inheritDoc}
23-
*/
2418
public static function get($value, array $params = []): float
2519
{
2620
return (float) $value;

system/Entity/Cast/IntBoolCast.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
namespace CodeIgniter\Entity\Cast;
1515

1616
/**
17-
* Int Bool Cast
18-
*
1917
* DB column: int (0/1) <--> Class property: bool
2018
*/
2119
final class IntBoolCast extends BaseCast

system/Entity/Cast/IntegerCast.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313

1414
namespace CodeIgniter\Entity\Cast;
1515

16-
/**
17-
* Class IntegerCast
18-
*/
1916
class IntegerCast extends BaseCast
2017
{
21-
/**
22-
* {@inheritDoc}
23-
*/
2418
public static function get($value, array $params = []): int
2519
{
2620
return (int) $value;

0 commit comments

Comments
 (0)