Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/Db/RowCellNumberMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public function formatRowData(Column $column, array $row) {
return null;
}
$decimals = $column->getNumberDecimals() ?? 0;
if ($decimals === 0) {
return (int)$value;
$rounded = round(floatval($value), $decimals);
if ($decimals === 0 && $rounded <= PHP_INT_MAX && $rounded >= PHP_INT_MIN) {
return (int)$rounded;
}

return round(floatval($value), $decimals);
return $rounded;
}

public function applyDataToEntity(Column $column, RowCellSuper $cell, $data): void {
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/Db/RowCellNumberMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RowCellNumberMapperTest extends TestCase {
private RowCellNumberMapper $mapper;
private Column $column;

#[\Override]
public function setUp(): void {
$this->mapper = new RowCellNumberMapper($this->createMock(IDBConnection::class));
// getNumberDecimals() is resolved through Entity's __call magic and cannot be mocked
Expand Down Expand Up @@ -50,6 +51,10 @@ public function formatRowDataProvider(): array {
return [
'integer value' => [42.0, 42],
'explicit zero' => [0.0, 0],
'fraction rounds up' => [2.7, 3],
'fraction rounds down' => [0.4, 0],
'negative fraction rounds away from zero' => [-2.7, -3],
'large value does not saturate at integer maximum' => [12345678901234567890.0, 12345678901234567890.0],
// A NULL cell means "no value" and must not read back as 0.
'null value' => [null, null],
'empty string' => ['', null],
Expand All @@ -59,7 +64,7 @@ public function formatRowDataProvider(): array {
/**
* @dataProvider formatRowDataProvider
*/
public function testFormatRowData(mixed $stored, ?int $expected): void {
public function testFormatRowData(mixed $stored, int|float|null $expected): void {
$this->assertSame($expected, $this->mapper->formatRowData($this->column, ['value' => $stored]));
}
}