bug #36690 [Yaml] prevent notice for invalid octal numbers on PHP 7.4 (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] prevent notice for invalid octal numbers on PHP 7.4

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34807
| License       | MIT
| Doc PR        |

Commits
-------

92bc19fd0c prevent notice for invalid octal numbers on PHP 7.4
This commit is contained in:
Fabien Potencier 2020-05-05 07:30:40 +02:00
commit 6340e87755
2 changed files with 20 additions and 4 deletions

View File

@ -759,15 +759,21 @@ class Inline
switch (true) {
case ctype_digit($scalar):
$raw = $scalar;
if ('0' === $scalar[0]) {
return octdec(preg_replace('/[^0-7]/', '', $scalar));
}
$cast = (int) $scalar;
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
return ($scalar === (string) $cast) ? $cast : $scalar;
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
$raw = $scalar;
if ('0' === $scalar[1]) {
return -octdec(preg_replace('/[^0-7]/', '', substr($scalar, 1)));
}
$cast = (int) $scalar;
return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
return ($scalar === (string) $cast) ? $cast : $scalar;
case is_numeric($scalar):
case Parser::preg_match(self::getHexRegex(), $scalar):
$scalar = str_replace('_', '', $scalar);

View File

@ -842,4 +842,14 @@ class InlineTest extends TestCase
[['' => 'foo', 'bar' => 'ccc'], '{!php/const : foo, bar: ccc}'],
];
}
public function testParsePositiveOctalNumberContainingInvalidDigits()
{
self::assertSame(342391, Inline::parse('0123456789'));
}
public function testParseNegativeOctalNumberContainingInvalidDigits()
{
self::assertSame(-342391, Inline::parse('-0123456789'));
}
}