Skip to content

Commit 8cc25d2

Browse files
committed
refactor!: update Cast handlers
1 parent 469f532 commit 8cc25d2

14 files changed

+190
-44
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,26 +2061,6 @@
20612061
'count' => 2,
20622062
'path' => __DIR__ . '/system/Encryption/Handlers/SodiumHandler.php',
20632063
];
2064-
$ignoreErrors[] = [
2065-
'message' => '#^Parameter \\#1 \\$value \\(bool\\|int\\|string\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\IntBoolCast\\:\\:set\\(\\) should be contravariant with parameter \\$value \\(array\\|bool\\|float\\|int\\|object\\|string\\|null\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\BaseCast\\:\\:set\\(\\)$#',
2066-
'count' => 1,
2067-
'path' => __DIR__ . '/system/Entity/Cast/IntBoolCast.php',
2068-
];
2069-
$ignoreErrors[] = [
2070-
'message' => '#^Parameter \\#1 \\$value \\(bool\\|int\\|string\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\IntBoolCast\\:\\:set\\(\\) should be contravariant with parameter \\$value \\(array\\|bool\\|float\\|int\\|object\\|string\\|null\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\CastInterface\\:\\:set\\(\\)$#',
2071-
'count' => 1,
2072-
'path' => __DIR__ . '/system/Entity/Cast/IntBoolCast.php',
2073-
];
2074-
$ignoreErrors[] = [
2075-
'message' => '#^Parameter \\#1 \\$value \\(int\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\IntBoolCast\\:\\:get\\(\\) should be contravariant with parameter \\$value \\(array\\|bool\\|float\\|int\\|object\\|string\\|null\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\BaseCast\\:\\:get\\(\\)$#',
2076-
'count' => 1,
2077-
'path' => __DIR__ . '/system/Entity/Cast/IntBoolCast.php',
2078-
];
2079-
$ignoreErrors[] = [
2080-
'message' => '#^Parameter \\#1 \\$value \\(int\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\IntBoolCast\\:\\:get\\(\\) should be contravariant with parameter \\$value \\(array\\|bool\\|float\\|int\\|object\\|string\\|null\\) of method CodeIgniter\\\\Entity\\\\Cast\\\\CastInterface\\:\\:get\\(\\)$#',
2081-
'count' => 1,
2082-
'path' => __DIR__ . '/system/Entity/Cast/IntBoolCast.php',
2083-
];
20842064
$ignoreErrors[] = [
20852065
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
20862066
'count' => 4,

system/Entity/Cast/ArrayCast.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class ArrayCast extends BaseCast
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public static function get($value, array $params = []): array
22+
public static function fromDatabase($value, array $params = []): array
2323
{
24-
if (is_string($value) && (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
24+
if (! is_string($value)) {
25+
self::invalidTypeValueError($value);
26+
}
27+
28+
if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
2529
$value = unserialize($value);
2630
}
2731

@@ -31,8 +35,12 @@ public static function get($value, array $params = []): array
3135
/**
3236
* {@inheritDoc}
3337
*/
34-
public static function set($value, array $params = []): string
38+
public static function toDatabase($value, array $params = []): string
3539
{
40+
if (is_array($value)) {
41+
self::invalidTypeValueError($value);
42+
}
43+
3644
return serialize($value);
3745
}
3846
}

system/Entity/Cast/BaseCast.php

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

1212
namespace CodeIgniter\Entity\Cast;
1313

14+
use TypeError;
15+
1416
/**
1517
* Class BaseCast
1618
*/
@@ -68,4 +70,16 @@ public static function fromDatabase($value, array $params = [])
6870
{
6971
return $value;
7072
}
73+
74+
/**
75+
* Throws TypeError
76+
*
77+
* @param mixed $value
78+
*
79+
* @return never
80+
*/
81+
protected static function invalidTypeValueError($value)
82+
{
83+
throw new TypeError('Invalid type value: ' . var_export($value, true));
84+
}
7185
}

system/Entity/Cast/BooleanCast.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ class BooleanCast extends BaseCast
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public static function get($value, array $params = []): bool
22+
public static function set($value, array $params = []): bool
2323
{
2424
return (bool) $value;
2525
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public static function fromDatabase($value, array $params = []): bool
31+
{
32+
if (! is_string($value)) {
33+
self::invalidTypeValueError($value);
34+
}
35+
36+
return (bool) $value;
37+
}
2638
}

system/Entity/Cast/CSVCast.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ class CSVCast extends BaseCast
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public static function get($value, array $params = []): array
22+
public static function fromDatabase($value, array $params = []): array
2323
{
24+
if (! is_string($value)) {
25+
self::invalidTypeValueError($value);
26+
}
27+
2428
return explode(',', $value);
2529
}
2630

2731
/**
2832
* {@inheritDoc}
2933
*/
30-
public static function set($value, array $params = []): string
34+
public static function toDatabase($value, array $params = []): string
3135
{
36+
if (! is_array($value)) {
37+
self::invalidTypeValueError($value);
38+
}
39+
3240
return implode(',', $value);
3341
}
3442
}

system/Entity/Cast/DatetimeCast.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DatetimeCast extends BaseCast
2727
*
2828
* @throws Exception
2929
*/
30-
public static function get($value, array $params = [])
30+
public static function set($value, array $params = [])
3131
{
3232
if ($value instanceof Time) {
3333
return $value;
@@ -45,6 +45,34 @@ public static function get($value, array $params = [])
4545
return Time::parse($value);
4646
}
4747

48-
return $value;
48+
self::invalidTypeValueError($value);
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*
54+
* @return Time
55+
*
56+
* @throws Exception
57+
*/
58+
public static function fromDatabase($value, array $params = [])
59+
{
60+
if (is_string($value)) {
61+
return Time::parse($value);
62+
}
63+
64+
self::invalidTypeValueError($value);
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public static function toDatabase($value, array $params = []): string
71+
{
72+
if (! $value instanceof Time) {
73+
self::invalidTypeValueError($value);
74+
}
75+
76+
return (string) $value;
4977
}
5078
}

system/Entity/Cast/FloatCast.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ class FloatCast extends BaseCast
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public static function get($value, array $params = []): float
22+
public static function set($value, array $params = []): float
23+
{
24+
return (float) $value;
25+
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public static function fromDatabase($value, array $params = []): float
2331
{
2432
return (float) $value;
2533
}

system/Entity/Cast/IntBoolCast.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,38 @@
1919
final class IntBoolCast extends BaseCast
2020
{
2121
/**
22-
* @param int $value
22+
* {@inheritDoc}
2323
*/
24-
public static function get($value, array $params = []): bool
24+
public static function set($value, array $params = []): bool
2525
{
26+
if (! is_bool($value)) {
27+
self::invalidTypeValueError($value);
28+
}
29+
30+
return $value;
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public static function fromDatabase($value, array $params = []): bool
37+
{
38+
if (! is_int($value) && ! is_string($value)) {
39+
self::invalidTypeValueError($value);
40+
}
41+
2642
return (bool) $value;
2743
}
2844

2945
/**
30-
* @param bool|int|string $value
46+
* {@inheritDoc}
3147
*/
32-
public static function set($value, array $params = []): int
48+
public static function toDatabase($value, array $params = []): int
3349
{
50+
if (! is_bool($value)) {
51+
self::invalidTypeValueError($value);
52+
}
53+
3454
return (int) $value;
3555
}
3656
}

system/Entity/Cast/IntegerCast.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ class IntegerCast extends BaseCast
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public static function get($value, array $params = []): int
22+
public static function set($value, array $params = []): int
2323
{
2424
return (int) $value;
2525
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public static function fromDatabase($value, array $params = []): int
31+
{
32+
if (! is_string($value) && ! is_int($value)) {
33+
self::invalidTypeValueError($value);
34+
}
35+
36+
return (int) $value;
37+
}
2638
}

system/Entity/Cast/JsonCast.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ class JsonCast extends BaseCast
2323
/**
2424
* {@inheritDoc}
2525
*/
26-
public static function get($value, array $params = [])
26+
public static function fromDatabase($value, array $params = [])
2727
{
28+
if (! is_string($value)) {
29+
self::invalidTypeValueError($value);
30+
}
31+
2832
$associative = in_array('array', $params, true);
2933

30-
$tmp = $value !== null ? ($associative ? [] : new stdClass()) : null;
34+
// @TODO Can $value be null?
35+
$tmp = ($associative ? [] : new stdClass());
3136

3237
if (function_exists('json_decode')
3338
&& (
34-
(is_string($value)
35-
&& strlen($value) > 1
39+
(strlen($value) > 1
3640
&& in_array($value[0], ['[', '{', '"'], true))
3741
|| is_numeric($value)
3842
)
@@ -49,8 +53,10 @@ public static function get($value, array $params = [])
4953

5054
/**
5155
* {@inheritDoc}
56+
*
57+
* @param mixed $value
5258
*/
53-
public static function set($value, array $params = []): string
59+
public static function toDatabase($value, array $params = []): string
5460
{
5561
if (function_exists('json_encode')) {
5662
try {

system/Entity/Cast/ObjectCast.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace CodeIgniter\Entity\Cast;
1313

14+
use stdClass;
15+
1416
/**
1517
* Class ObjectCast
1618
*/
@@ -19,8 +21,42 @@ class ObjectCast extends BaseCast
1921
/**
2022
* {@inheritDoc}
2123
*/
22-
public static function get($value, array $params = []): object
24+
public static function set($value, array $params = []): object
2325
{
26+
if (! is_array($value)) {
27+
self::invalidTypeValueError($value);
28+
}
29+
2430
return (object) $value;
2531
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public static function toDatabase($value, array $params = [])
37+
{
38+
if (! $value instanceof stdClass) {
39+
self::invalidTypeValueError($value);
40+
}
41+
42+
// @TODO How to implement?
43+
return serialize($value);
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
public static function fromDatabase($value, array $params = []): array
50+
{
51+
if (! is_string($value)) {
52+
self::invalidTypeValueError($value);
53+
}
54+
55+
// @TODO How to implement?
56+
if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
57+
$value = unserialize($value);
58+
}
59+
60+
return (array) $value;
61+
}
2662
}

system/Entity/Cast/StringCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class StringCast extends BaseCast
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public static function get($value, array $params = []): string
22+
public static function set($value, array $params = []): string
2323
{
2424
return (string) $value;
2525
}

system/Entity/Cast/TimestampCast.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ class TimestampCast extends BaseCast
2020
{
2121
/**
2222
* {@inheritDoc}
23+
*
24+
* @return int
2325
*/
24-
public static function get($value, array $params = [])
26+
public static function set($value, array $params = [])
2527
{
28+
if (! is_string($value)) {
29+
self::invalidTypeValueError($value);
30+
}
31+
2632
$value = strtotime($value);
2733

2834
if ($value === false) {

system/Entity/Cast/URICast.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ class URICast extends BaseCast
2121
/**
2222
* {@inheritDoc}
2323
*/
24-
public static function get($value, array $params = []): URI
24+
public static function set($value, array $params = []): URI
2525
{
26-
return $value instanceof URI ? $value : new URI($value);
26+
if ($value instanceof URI) {
27+
return $value;
28+
}
29+
30+
if (! is_string($value)) {
31+
self::invalidTypeValueError($value);
32+
}
33+
34+
return new URI($value);
2735
}
2836
}

0 commit comments

Comments
 (0)