Skip to content

Commit

Permalink
Unify impl. for whole int range
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 28, 2024
1 parent dcbddf3 commit dc5cd73
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/Types/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use function assert;
use function is_int;
use function is_string;

use const PHP_INT_MAX;
use const PHP_INT_MIN;
use function preg_replace;

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

if (
($value > PHP_INT_MIN && $value < PHP_INT_MAX)
|| $value === (string) (int) $value
) {
return (int) $value;
}

assert(
is_string($value),
'DBAL assumes values outside of the integer range to be returned as string by the database driver.',
);

if (preg_replace('~^(\+|-(?=0+($|\.)))|(?<=^|^[+\-])0+(?=\d)|\.0+$~', '', $value) === (string) (int) $value) {
return (int) $value;

Check failure on line 54 in src/Types/BigIntType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

InvalidCast

src/Types/BigIntType.php:54:26: InvalidCast: never cannot be cast to int (see https://psalm.dev/103)
}

return $value;

Check failure on line 57 in src/Types/BigIntType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

NoValue

src/Types/BigIntType.php:57:9: NoValue: All possible types for this return were invalidated - This may be dead code (see https://psalm.dev/179)
}
}
28 changes: 28 additions & 0 deletions tests/Functional/Types/BigIntTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use const PHP_INT_MIN;
use const PHP_INT_SIZE;

use function str_starts_with;

Check failure on line 18 in tests/Functional/Types/BigIntTypeTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Use statements should be sorted alphabetically. The first wrong one is str_starts_with.
use function substr;

class BigIntTypeTest extends FunctionalTestCase
{
#[DataProvider('provideBigIntLiterals')]
Expand All @@ -38,6 +41,26 @@ public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedVa
Types::BIGINT,
),
);

self::assertSame(
$expectedValue,
$this->connection->convertToPHPValue(
$sqlLiteral . '.00',
Types::BIGINT,
),
);

$startsWithSign = str_starts_with($sqlLiteral, '-') || str_starts_with($sqlLiteral, '+');

self::assertSame(
$expectedValue,
$this->connection->convertToPHPValue(
($startsWithSign ? substr($sqlLiteral, 0, 1) : '')

Check failure on line 58 in tests/Functional/Types/BigIntTypeTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Multi-line function call not indented correctly; expected 16 spaces but found 12
. '00'

Check failure on line 59 in tests/Functional/Types/BigIntTypeTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Trying to invoke '00' but it's not a callable.
($startsWithSign ? substr($sqlLiteral, 1) : $sqlLiteral),
Types::BIGINT,
),
);
}

/** @return Generator<string, array{string, int|string|null}> */
Expand All @@ -53,6 +76,11 @@ public static function provideBigIntLiterals(): Generator
yield 'large negative number' => [PHP_INT_SIZE === 4 ? '-2147483647' : '-9223372036854775807', PHP_INT_MIN + 1];
yield 'largest positive number' => [PHP_INT_SIZE === 4 ? '2147483647' : '9223372036854775807', PHP_INT_MAX];
yield 'largest negative number' => [PHP_INT_SIZE === 4 ? '-2147483648' : '-9223372036854775808', PHP_INT_MIN];

yield 'plus largest positive number' => [
PHP_INT_SIZE === 4 ? '+2147483647' : '+9223372036854775807',
PHP_INT_MAX,
];
}

public function testUnsignedBigIntOnMySQL(): void
Expand Down

0 comments on commit dc5cd73

Please sign in to comment.