Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatParameter() for floats #1374

Merged
merged 1 commit into from Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/Doctrine/Migrations/InlineParameterFormatter.php
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;
Comment on lines +75 to 76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did a string value need a string type cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, it's a cheap no-op. I suppose the original author simply wanted to keep the code short.

}

Expand Down
22 changes: 13 additions & 9 deletions tests/Doctrine/Migrations/Tests/InlineParameterFormatterTest.php
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