Skip to content

Commit c3d4e15

Browse files
committed
fix: TypeError in DataCaster init
1 parent b352164 commit c3d4e15

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

system/DataCaster/DataCaster.php

Lines changed: 19 additions & 7 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+
* @template TCastHandlers of array<string, CastInterface|class-string|EntityCastInterface>
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 TCastHandlers [type => classname]
4450
*/
4551
private array $castHandlers = [
4652
'array' => ArrayCast::class,
@@ -59,18 +65,20 @@ 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 TCastHandlers|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+
if ($castHandlers !== null && $castHandlers !== []) {
80+
$this->castHandlers = array_merge($this->castHandlers, $castHandlers);
81+
}
7482

7583
if ($types !== null) {
7684
$this->setTypes($types);
@@ -119,6 +127,10 @@ public function setTypes(array $types): static
119127
*/
120128
public function castAs(mixed $value, string $field, string $method = 'get'): mixed
121129
{
130+
if ($method !== 'get' && $method !== 'set') {
131+
throw CastException::forInvalidMethod($method);
132+
}
133+
122134
// If the type is not defined, return as it is.
123135
if (! isset($this->types[$field])) {
124136
return $value;

system/Entity/Cast/BaseCast.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,15 @@
1414
namespace CodeIgniter\Entity\Cast;
1515

1616
/**
17-
* Class BaseCast
17+
* Class BaseCast.
1818
*/
1919
abstract class BaseCast implements CastInterface
2020
{
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-
*/
2921
public static function get($value, array $params = [])
3022
{
3123
return $value;
3224
}
3325

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-
*/
4226
public static function set($value, array $params = [])
4327
{
4428
return $value;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\DataCaster;
15+
16+
use CodeIgniter\Entity\Exceptions\CastException;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use PHPUnit\Framework\Attributes\Group;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[Group('Others')]
24+
final class DataCasterTest extends CIUnitTestCase
25+
{
26+
public function testCastInvalidMethodException(): void
27+
{
28+
$this->expectException(CastException::class);
29+
$this->expectExceptionMessage('The "add" is invalid cast method, valid methods are: ["get", "set"].');
30+
31+
$dataCaster = new DataCaster();
32+
$dataCaster->castAs([], 'name', 'add'); // @phpstan-ignore argument.type
33+
}
34+
}

0 commit comments

Comments
 (0)