Skip to content

Commit e22cc52

Browse files
committed
Unify impl. for whole int range
1 parent dcbddf3 commit e22cc52

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/Types/BigIntType.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
use function assert;
1111
use function is_int;
1212
use function is_string;
13-
14-
use const PHP_INT_MAX;
15-
use const PHP_INT_MIN;
13+
use function preg_replace;
1614

1715
/**
1816
* Type that attempts to map a database BIGINT to a PHP int.
@@ -47,18 +45,15 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): int
4745
return $value;
4846
}
4947

50-
if (
51-
($value > PHP_INT_MIN && $value < PHP_INT_MAX)
52-
|| $value === (string) (int) $value
53-
) {
54-
return (int) $value;
55-
}
56-
5748
assert(
5849
is_string($value),
5950
'DBAL assumes values outside of the integer range to be returned as string by the database driver.',
6051
);
6152

53+
if (preg_replace('~^\+|\.0+$~', '', $value) === (string) (int) $value) {
54+
return (int) $value;
55+
}
56+
6257
return $value;
6358
}
6459
}

tests/Functional/Types/BigIntTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedVa
3838
Types::BIGINT,
3939
),
4040
);
41+
42+
self::assertSame(
43+
$expectedValue,
44+
$this->connection->convertToPHPValue(
45+
$sqlLiteral . '.00',
46+
Types::BIGINT,
47+
),
48+
);
4149
}
4250

4351
/** @return Generator<string, array{string, int|string|null}> */
@@ -53,6 +61,11 @@ public static function provideBigIntLiterals(): Generator
5361
yield 'large negative number' => [PHP_INT_SIZE === 4 ? '-2147483647' : '-9223372036854775807', PHP_INT_MIN + 1];
5462
yield 'largest positive number' => [PHP_INT_SIZE === 4 ? '2147483647' : '9223372036854775807', PHP_INT_MAX];
5563
yield 'largest negative number' => [PHP_INT_SIZE === 4 ? '-2147483648' : '-9223372036854775808', PHP_INT_MIN];
64+
65+
yield 'plus largest positive number' => [
66+
PHP_INT_SIZE === 4 ? '+2147483647' : '+9223372036854775807',
67+
PHP_INT_MAX,
68+
];
5669
}
5770

5871
public function testUnsignedBigIntOnMySQL(): void

0 commit comments

Comments
 (0)