Permalink
Browse files

bug #30063 [Form] don't lose int precision with not needed type casts…

… (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] don't lose int precision with not needed type casts

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26795
| License       | MIT
| Doc PR        |

Commits
-------

72136f1 don't lose int precision with not needed type casts
  • Loading branch information...
nicolas-grekas committed Feb 8, 2019
2 parents 68b4825 + 72136f1 commit eb2a18ece1cdecdcce8504cbe03d2b5f71cb1ae6
@@ -44,4 +44,12 @@ public function reverseTransform($value)
return null !== $result ? (int) $result : null;
}
/**
* @internal
*/
protected function castParsedValue($value)
{
return $value;
}
}
@@ -181,9 +181,7 @@ public function reverseTransform($value)
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
}
if (\is_int($result) && $result === (int) $float = (float) $result) {
$result = $float;
}
$result = $this->castParsedValue($result);
if (false !== $encoding = mb_detect_encoding($value, null, true)) {
$length = mb_strlen($value, $encoding);
@@ -228,6 +226,18 @@ protected function getNumberFormatter()
return $formatter;
}
/**
* @internal
*/
protected function castParsedValue($value)
{
if (\is_int($value) && $value === (int) $float = (float) $value) {
return $float;
}
return $value;
}
/**
* Rounds a number according to the configured scale and rounding mode.
*
@@ -50,4 +50,45 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedD
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}
public function testSubmittedLargeIntegersAreNotCastToFloat()
{
if (4 === \PHP_INT_SIZE) {
$this->markTestSkipped('This test requires a 64bit PHP.');
}
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit('201803221011791');
$this->assertSame(201803221011791, $form->getData());
$this->assertSame('201803221011791', $form->getViewData());
}
public function testTooSmallIntegersAreNotValid()
{
if (4 === \PHP_INT_SIZE) {
$min = '-2147483649';
} else {
$min = '-9223372036854775808';
}
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit($min);
$this->assertFalse($form->isSynchronized());
}
public function testTooGreatIntegersAreNotValid()
{
if (4 === \PHP_INT_SIZE) {
$max = '2147483648';
} else {
$max = '9223372036854775808';
}
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit($max);
$this->assertFalse($form->isSynchronized());
}
}

0 comments on commit eb2a18e

Please sign in to comment.