8000 feature #11976 [Yaml] Ensure whole number floats have their data type… · symfony/symfony@089129b · GitHub
[go: up one dir, main page]

Skip to content

Commit 089129b

Browse files
committed
feature #11976 [Yaml] Ensure whole number floats have their data type is persisted. (Alex Pott)
This PR was squashed before being merged into the 2.6-dev branch (closes #11976). Discussion ---------- [Yaml] Ensure whole number floats have their data type is persisted. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | Yes | Fixed tickets | N/A | License | MIT | Doc PR | no See http://yaml.org/spec/1.1/#id858936 specifically the spec about specific data types. Sample code that exposes the issue: ```php $yaml = new \Symfony\Component\Yaml\Yaml(); $expected = array('float' => (float) 1); $test = $yaml->parse($yaml->dump($expected)); if ($expected === $test) { print "match!\n"; } else { var_dump($test); } ``` This will output ``` array(1) { 'float' => int(1) } ``` Commits ------- 6a507cc [Yaml] Ensure whole number floats have their data type is persisted.
2 parents 87d5856 + 6a507cc commit 089129b

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,17 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
128128
if (false !== $locale) {
129129
setlocale(LC_NUMERIC, 'C');
130130
}
131-
$repr = is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value));
132-
131+
if (is_float($value)) {
132+
$repr = strval($value);
133+
if (is_infinite($value)) {
134+
$repr = str_ireplace('INF', '.Inf', $repr);
135+
} elseif (floor($value) == $value && $repr == $value) {
136+
// Preserve float data type since storing a whole number will result in integer value.
137+
$repr = '!!float '.$repr;
138+
}
139+
} else {
140+
$repr = is_string($value) ? "'$value'" : strval($value);
141+
}
133142
if (false !== $locale) {
134143
setlocale(LC_NUMERIC, $locale);
135144
}
@@ -470,6 +479,8 @@ private static function evaluateScalar($scalar, $references = array())
470479
}
471480

472481
return;
482+
case 0 === strpos($scalar, '!!float '):
483+
return (float) substr($scalar, 8);
473484
case ctype_digit($scalar):
474485
$raw = $scalar;
475486
$cast = intval($scalar);

src/Symfony/Component/Yaml/Tests/DumperTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ public function testSpecifications()
9696
// TODO
9797
} else {
9898
eval('$expected = '.trim($test['php']).';');
99-
100-
$this->assertEquals($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
99+
$this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
101100
}
102101
}
103102
}

src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,15 @@ yaml: |
529529
fixed: 1,230.15
530530
negative infinity: -.inf
531531
not a number: .NaN
532+
float as whole number: !!float 1
532533
php: |
533534
array(
534535
'canonical' => 1230.15,
535536
'exponential' => 1230.15,
536537
'fixed' => 1230.15,
537538
'negative infinity' => log(0),
538539
'not a number' => -log(0),
540+
'float as whole number' => (float) 1
539541
)
540542
---
541543
test: Miscellaneous

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testDump($yaml, $value)
4040
{
4141
$this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
4242

43-
$this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
43+
$this->assertSame($value, Inline::parse(Inline::dump($value)), 'check consistency');
4444
}
4545

4646
public function testDumpNumericValueWithLocale()
@@ -323,7 +323,7 @@ public function getTestsForDump()
323323
array('true', true),
324324
array('12', 12),
325325
array("'quoted string'", 'quoted string'),
326-
array('12.30e+02', 12.30e+02),
326+
array('!!float 1230', 12.30e+02),
327327
array('1234', 0x4D2),
328328
array('1243', 02333),
329329
array('.Inf', -log(0)),

0 commit comments

Comments
 (0)
0