Skip to content

Commit de07ff8

Browse files
committed
Simplify PrimaryKeyTypes
1 parent af13c74 commit de07ff8

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

tests/Fixtures/Blog/Type/PrimaryKeyBinaryType.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use LogicException;
99
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\PrimaryKey;
1010
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Compat\CompatibilityType;
11+
use function get_debug_type;
12+
use function is_string;
1113
use function pack;
1214
use function unpack;
1315

@@ -25,11 +27,11 @@ public function convertToPHPValue(
2527
return null;
2628
}
2729

28-
if ($value instanceof PrimaryKey) {
29-
return $value;
30+
if (is_string($value)) {
31+
return new PrimaryKey(unpack('N', $value)[1]); // @phpstan-ignore offsetAccess.nonOffsetAccessible
3032
}
3133

32-
return new PrimaryKey(unpack('N', $value)[1]); // @phpstan-ignore offsetAccess.nonOffsetAccessible
34+
throw new LogicException('Unexpected value: ' . get_debug_type($value));
3335
}
3436

3537
public function convertToDatabaseValue(

tests/Fixtures/Blog/Type/PrimaryKeyIntegerType.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use LogicException;
99
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\PrimaryKey;
1010
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Compat\CompatibilityType;
11+
use function get_debug_type;
12+
use function is_int;
1113

1214
final class PrimaryKeyIntegerType extends Type
1315
{
@@ -23,11 +25,11 @@ public function convertToPHPValue(
2325
return null;
2426
}
2527

26-
if ($value instanceof PrimaryKey) {
27-
return $value;
28+
if (is_int($value)) {
29+
return new PrimaryKey($value);
2830
}
2931

30-
return new PrimaryKey($value);
32+
throw new LogicException('Unexpected value: ' . get_debug_type($value));
3133
}
3234

3335
public function convertToDatabaseValue(

tests/Fixtures/Blog/Type/PrimaryKeyStringType.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use LogicException;
99
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\PrimaryKey;
1010
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Compat\CompatibilityType;
11+
use function get_debug_type;
12+
use function is_string;
1113

1214
final class PrimaryKeyStringType extends Type
1315
{
@@ -23,11 +25,11 @@ public function convertToPHPValue(
2325
return null;
2426
}
2527

26-
if ($value instanceof PrimaryKey) {
27-
return $value;
28+
if (is_string($value)) {
29+
return new PrimaryKey((int) $value);
2830
}
2931

30-
return new PrimaryKey((int) $value);
32+
throw new LogicException('Unexpected value: ' . get_debug_type($value));
3133
}
3234

3335
public function convertToDatabaseValue(

0 commit comments

Comments
 (0)