Skip to content

Commit

Permalink
Fix formatParameter() for floats (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Nov 19, 2023
1 parent 282661f commit e4e0855
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
9 changes: 5 additions & 4 deletions lib/Doctrine/Migrations/InlineParameterFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function implode;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_string;
use function sprintf;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function formatParameters(array $params, array $types): string
return sprintf('with parameters (%s)', implode(', ', $formattedParameters));
}

private function formatParameter(mixed $value, string|int $type): string|int|null
private function formatParameter(mixed $value, string|int $type): string|int|float|null
{
if (is_string($type) && Type::hasType($type)) {
return Type::getType($type)->convertToDatabaseValue(
Expand All @@ -64,14 +65,14 @@ private function formatParameter(mixed $value, string|int $type): string|int|nul
return $this->parameterToString($value);
}

/** @param int[]|bool[]|string[]|array|int|string|bool $value */
private function parameterToString(array|int|string|bool $value): string
/** @param int[]|bool[]|string[]|float[]|array|int|string|float|bool $value */
private function parameterToString(array|int|string|float|bool $value): string
{
if (is_array($value)) {
return implode(', ', array_map($this->parameterToString(...), $value));
}

if (is_int($value) || is_string($value)) {
if (is_int($value) || is_string($value) || is_float($value)) {
return (string) $value;
}

Expand Down
22 changes: 13 additions & 9 deletions tests/Doctrine/Migrations/Tests/InlineParameterFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ public function testFormatParameters(): void
$params = [
0 => 'string value',
1 => 1,
2 => [1, true, false, 'string value'],
3 => true,
4 => false,
5 => 'string value',
6 => 1,
7 => true,
8 => false,
9 => [1, true, false, 'string value'],
2 => 1.5,
3 => [1, true, false, 'string value'],
4 => true,
5 => false,
6 => 'string value',
7 => 1,
8 => 1.5,
9 => true,
10 => false,
11 => [1, true, false, 'string value'],
'named' => 'string value',
];

$types = [
Types::STRING,
Types::INTEGER,
Types::FLOAT,
Types::SIMPLE_ARRAY,
Types::BOOLEAN,
Types::BOOLEAN,
Expand All @@ -46,11 +49,12 @@ public function testFormatParameters(): void
'unknown',
'unknown',
'unknown',
'unknown',
];

$result = $this->parameterFormatter->formatParameters($params, $types);

$expected = 'with parameters ([string value], [1], [1,1,,string value], [], [], [string value], [1], [true], [false], [1, true, false, string value], :named => [string value])';
$expected = 'with parameters ([string value], [1], [1.5], [1,1,,string value], [], [], [string value], [1], [1.5], [true], [false], [1, true, false, string value], :named => [string value])';

self::assertSame($expected, $result);
}
Expand Down

0 comments on commit e4e0855

Please sign in to comment.