Skip to content

Commit ee79e7e

Browse files
stofnicolas-grekas
authored andcommitted
[Yaml] Improve the deprecation warnings for octal numbers to suggest migrating
1 parent 37bcbbb commit ee79e7e

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

Inline.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -646,21 +646,18 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
646646
return (float) substr($scalar, 8);
647647
case 0 === strpos($scalar, '!!binary '):
648648
return self::evaluateBinaryScalar(substr($scalar, 9));
649-
default:
650-
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar), self::$parsedLineNumber, $scalar, self::$parsedFilename);
651649
}
652-
// no break
650+
651+
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar), self::$parsedLineNumber, $scalar, self::$parsedFilename);
653652
case preg_match('/^(?:\+|-)?0o(?P<value>[0-7_]++)$/', $scalar, $matches):
654653
$value = str_replace('_', '', $matches['value']);
655654

656655
if ('-' === $scalar[0]) {
657656
return -octdec($value);
658-
} else {
659-
return octdec($value);
660657
}
661658

659+
return octdec($value);
662660
// Optimize for returning strings.
663-
// no break
664661
case \in_array($scalar[0], ['+', '-', '.'], true) || is_numeric($scalar[0]):
665662
if (Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar)) {
666663
$scalar = str_replace('_', '', $scalar);
@@ -669,7 +666,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
669666
switch (true) {
670667
case ctype_digit($scalar):
671668
if (preg_match('/^0[0-7]+$/', $scalar)) {
672-
trigger_deprecation('symfony/yaml', '5.1', 'Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0.');
669+
trigger_deprecation('symfony/yaml', '5.1', 'Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0. Use "%s" to represent the octal number.', '0o'.substr($scalar, 1));
673670

674671
return octdec($scalar);
675672
}
@@ -679,7 +676,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
679676
return ($scalar === (string) $cast) ? $cast : $scalar;
680677
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
681678
if (preg_match('/^-0[0-7]+$/', $scalar)) {
682-
trigger_deprecation('symfony/yaml', '5.1', 'Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0.');
679+
trigger_deprecation('symfony/yaml', '5.1', 'Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0. Use "%s" to represent the octal number.', '-0o'.substr($scalar, 2));
683680

684681
return -octdec(substr($scalar, 1));
685682
}

Tests/InlineTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -771,19 +771,19 @@ public function getTestsForOctalNumbers()
771771
* @group legacy
772772
* @dataProvider getTestsForOctalNumbersYaml11Notation
773773
*/
774-
public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml)
774+
public function testParseOctalNumbersYaml11Notation(int $expected, string $yaml, string $replacement)
775775
{
776-
$this->expectDeprecation('Since symfony/yaml 5.1: Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0.');
776+
$this->expectDeprecation(sprintf('Since symfony/yaml 5.1: Support for parsing numbers prefixed with 0 as octal numbers. They will be parsed as strings as of 6.0. Use "%s" to represent the octal number.', $replacement));
777777

778778
self::assertSame($expected, Inline::parse($yaml));
779779
}
780780

781781
public function getTestsForOctalNumbersYaml11Notation()
782782
{
783783
return [
784-
'positive octal number' => [28, '034'],
785-
'positive octal number with separator' => [1243, '0_2_3_3_3'],
786-
'negative octal number' => [-28, '-034'],
784+
'positive octal number' => [28, '034', '0o34'],
785+
'positive octal number with separator' => [1243, '0_2_3_3_3', '0o2333'],
786+
'negative octal number' => [-28, '-034', '-0o34'],
787787
];
788788
}
789789

0 commit comments

Comments
 (0)