Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0a483e

Browse files
committed
feature #18785 [Yaml] deprecate comma separators in floats (xabbuh)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Yaml] deprecate comma separators in floats | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #18178 | License | MIT | Doc PR | Commits ------- 3f3623d [Yaml] deprecate comma separators in floats
2 parents 4c5464c + 3f3623d commit b0a483e

File tree

4 files changed

+101
-14
lines changed

4 files changed

+101
-14
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
591591
case 0 === strpos($scalar, '!!binary '):
592592
return self::evaluateBinaryScalar(substr($scalar, 9));
593593
case preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
594+
@trigger_error('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
594595
case preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
595596
return (float) str_replace(array(',', '_'), '', $scalar);
596597
case preg_match(self::getTimestampRegex(), $scalar):

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,37 +509,53 @@ test: Integers
509509
spec: 2.19
510510
yaml: |
511511
canonical: 12345
512-
decimal: +12,345
513512
octal: 014
514513
hexadecimal: 0xC
515514
php: |
516515
array(
517516
'canonical' => 12345,
518-
'decimal' => 12345.0,
519517
'octal' => 014,
520518
'hexadecimal' => 0xC
521519
)
522520
---
521+
test: Decimal Integer
522+
deprecated: true
523+
spec: 2.19
524+
yaml: |
525+
decimal: +12,345
526+
php: |
527+
array(
528+
'decimal' => 12345.0,
529+
)
530+
---
523531
# FIX: spec shows parens around -inf and NaN
524532
test: Floating point
525533
spec: 2.20
526534
yaml: |
527535
canonical: 1.23015e+3
528536
exponential: 12.3015e+02
529-
fixed: 1,230.15
530537
negative infinity: -.inf
531538
not a number: .NaN
532539
float as whole number: !!float 1
533540
php: |
534541
array(
535542
'canonical' => 1230.15,
536543
'exponential' => 1230.15,
537-
'fixed' => 1230.15,
538544
'negative infinity' => log(0),
539545
'not a number' => -log(0),
540546
'float as whole number' => (float) 1
541547
)
542548
---
549+
test: Fixed Floating point
550+
deprecated: true
551+
spec: 2.20
552+
yaml: |
553+
fixed: 1,230.15
554+
php: |
555+
array(
556+
'fixed' => 1230.15,
557+
)
558+
---
543559
test: Miscellaneous
544560
spec: 2.21
545561
yaml: |
@@ -1532,29 +1548,42 @@ php: |
15321548
test: Integer
15331549
yaml: |
15341550
canonical: 12345
1535-
decimal: +12,345
15361551
octal: 014
15371552
hexadecimal: 0xC
15381553
php: |
15391554
array(
15401555
'canonical' => 12345,
1541-
'decimal' => 12345.0,
15421556
'octal' => 12,
15431557
'hexadecimal' => 12
15441558
)
15451559
---
1560+
test: Decimal
1561+
deprecated: true
1562+
yaml: |
1563+
decimal: +12,345
1564+
php: |
1565+
array(
1566+
'decimal' => 12345.0,
1567+
)
1568+
---
1569+
test: Fixed Float
1570+
deprecated: true
1571+
yaml: |
1572+
fixed: 1,230.15
1573+
php: |
1574+
array(
1575+
'fixed' => 1230.15,
1576+
)
15461577
test: Float
15471578
yaml: |
15481579
canonical: 1.23015e+3
15491580
exponential: 12.3015e+02
1550-
fixed: 1,230.15
15511581
negative infinity: -.inf
15521582
not a number: .NaN
15531583
php: |
15541584
array(
15551585
'canonical' => 1230.15,
15561586
'exponential' => 1230.15,
1557-
'fixed' => 1230.15,
15581587
'negative infinity' => log(0),
15591588
'not a number' => -log(0)
15601589
)

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,37 @@ brief: >
176176
yaml: |
177177
zero: 0
178178
simple: 12
179-
one-thousand: 1,000
180-
negative one-thousand: -1,000
181179
php: |
182180
array(
183181
'zero' => 0,
184182
'simple' => 12,
183+
)
184+
---
185+
test: Positive Big Integer
186+
deprecated: true
187+
dump_skip: true
188+
brief: >
189+
An integer is a series of numbers, optionally
190+
starting with a positive or negative sign. Integers
191+
may also contain commas for readability.
192+
yaml: |
193+
one-thousand: 1,000
194+
php: |
195+
array(
185196
'one-thousand' => 1000.0,
197+
)
198+
---
199+
test: Negative Big Integer
200+
deprecated: true
201+
dump_skip: true
202+
brief: >
203+
An integer is a series of numbers, optionally
204+
starting with a positive or negative sign. Integers
205+
may also contain commas for readability.
206+
yaml: |
207+
negative one-thousand: -1,000
208+
php: |
209+
array(
186210
'negative one-thousand' => -1000.0
187211
)
188212
---
@@ -208,15 +232,27 @@ brief: >
208232
positive and negative infinity and "not a number."
209233
yaml: |
210234
a simple float: 2.00
211-
larger float: 1,000.09
212235
scientific notation: 1.00009e+3
213236
php: |
214237
array(
215238
'a simple float' => 2.0,
216-
'larger float' => 1000.09,
217239
'scientific notation' => 1000.09
218240
)
219241
---
242+
test: Larger Float
243+
dump_skip: true
244+
deprecated: true
245+
brief: >
246+
Floats are represented by numbers with decimals,
247+
allowing for scientific notation, as well as
248+
positive and negative infinity and "not a number."
249+
yaml: |
250+
larger float: 1,000.09
251+
php: |
252+
array(
253+
'larger float' => 1000.09,
254+
)
255+
---
220256
test: Time
221257
todo: true
222258
brief: >

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,30 @@ protected function tearDown()
3131
/**
3232
* @dataProvider getDataFormSpecifications
3333
*/
34-
public function testSpecifications($file, $expected, $yaml, $comment)
34+
public function testSpecifications($file, $expected, $yaml, $comment, $deprecated)
3535
{
36+
$deprecations = array();
37+
38+
if ($deprecated) {
39+
set_error_handler(function ($type, $msg) use (&$deprecations) {
40+
if (E_USER_DEPRECATED !== $type) {
41+
restore_error_handler();
42+
43+
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
44+
}
45+
46+
$deprecations[] = $msg;
47+
});
48+
}
49+
3650
$this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
51+
52+
if ($deprecated) {
53+
restore_error_handler();
54+
55+
$this->assertCount(1, $deprecations);
56+
$this->assertContains('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', $deprecations[0]);
57+
}
3758
}
3859

3960
public function getDataFormSpecifications()
@@ -58,7 +79,7 @@ public function getDataFormSpecifications()
5879
} else {
5980
eval('$expected = '.trim($test['php']).';');
6081

61-
$tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
82+
$tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test'], isset($test['deprecated']) ? $test['deprecated'] : false);
6283
}
6384
}
6485
}

0 commit comments

Comments
 (0)