Skip to content

Commit

Permalink
Reimplement without "preg_replace"
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 28, 2024
1 parent 4c3258b commit 70d3683
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Types/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;

use function assert;
use function ctype_digit;
use function is_int;
use function is_string;
use function preg_replace;
use function rtrim;
use function str_starts_with;
use function strpos;
use function substr;

Expand Down Expand Up @@ -53,13 +54,27 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): int
'DBAL assumes values outside of the integer range to be returned as string by the database driver.',
);

// workaround https://github.com/php/php-src/issues/14345
if (str_starts_with($value, '-') || str_starts_with($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)

DocblockTypeContradiction

src/Types/BigIntType.php:57:45: DocblockTypeContradiction: Operand of type false is always falsy (see https://psalm.dev/155)

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:61: NoValue: All possible types for this argument were invalidated - This may be dead code (see https://psalm.dev/179)
$hasNegativeSign = str_starts_with($value, '-');
$value = substr($value, 1);
} else {
$hasNegativeSign = false;
}

while (substr($value, 0, 1) === '0' && ctype_digit(substr($value, 1, 1))) {
$value = substr($value, 1);
}

if ($hasNegativeSign && $value !== '0') {
$value = '-' . $value;
}

$dotPos = strpos($value, '.');
if ($dotPos !== false && rtrim(substr($value, $dotPos + 1), '0') === '') {
$value = substr($value, 0, $dotPos);
}

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

Expand Down

0 comments on commit 70d3683

Please sign in to comment.