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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[DependencyInjection] Deprecate numeric parameter names
  • Loading branch information
HeahDude committed Sep 26, 2022
commit 3e0050a8c416071bb7c33619798736677b9c21a6
1 change: 1 addition & 0 deletions UPGRADE-6.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ DependencyInjection

* Change the signature of `ContainerAwareInterface::setContainer()` to `setContainer(?ContainerInterface)`
* Deprecate calling `ContainerAwareTrait::setContainer()` without arguments
* Deprecate using numeric parameter names

Form
----
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Allow #[When] to be extended
* Change the signature of `ContainerAwareInterface::setContainer()` to `setContainer(?ContainerInterface)`
* Deprecate calling `ContainerAwareTrait::setContainer()` without arguments
* Deprecate using numeric parameter names

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public function get(string $name): array|bool|string|int|float|\UnitEnum|null

public function set(string $name, array|bool|string|int|float|\UnitEnum|null $value)
{
if (is_numeric($name)) {
trigger_deprecation('symfony/dependency-injection', '6.2', sprintf('Using numeric parameter name "%s" is deprecated and will throw as of 7.0.', $name));
// uncomment the following line in 7.0
// throw new InvalidArgumentException(sprintf('The parameter name "%s" cannot be numeric.', $name));
}

$this->parameters[$name] = $value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
no = no
none = none
constant = PHP_VERSION
12 = 12
12_int = 12
12_string = '12'
12_quoted_number = "12"
12_comment = 12 ; comment
12_string_comment = '12' ; comment
12_quoted_number_comment = "12" ; comment
-12 = -12
0 = 0
1 = 1
0b0110 = 0b0110
11112222333344445555 = 1111,2222,3333,4444,5555
0777 = 0777
255 = 0xFF
100.0 = 1e2
-120.0 = -1.2E2
-10100.1 = -10100.1
-10,100.1 = -10,100.1
-12_negative = -12
zero = 0
one = 1
0b0110_byte_string = 0b0110
11112222333344445555_great_number = 1111,2222,3333,4444,5555
0777_number_starting_with_0 = 0777
255_hexadecimal = 0xFF
100.0_exponential = 1e2
-120.0_exponential = -1.2E2
-10100.1_negative_float = -10100.1
-10,100.1_negative_float = -10,100.1
list[] = 1
list[] = 2
map[one] = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[parameters]
true = true
true_comment = true ; comment
false = false
null = null
on = on
off = off
yes = yes
no = no
none = none
constant = PHP_VERSION
12 = 12
12_string = '12'
12_quoted_number = "12"
12_comment = 12 ; comment
12_string_comment = '12' ; comment
12_quoted_number_comment = "12" ; comment
-12 = -12
0 = 0
1 = 1
0b0110 = 0b0110
11112222333344445555 = 1111,2222,3333,4444,5555
0777 = 0777
255 = 0xFF
100.0 = 1e2
-120.0 = -1.2E2
-10100.1 = -10100.1
-10,100.1 = -10,100.1
list[] = 1
list[] = 2
map[one] = 1
map[two] = 2
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter>a string</parameter>
<parameter key="a_string">a string</parameter>
<parameter key="foo">bar</parameter>
<parameter key="values" type="collection">
<parameter>0</parameter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testImportWithGlobPattern()

$actual = $container->getParameterBag()->all();
$expected = [
'a string',
'a_string' => 'a string',
'foo' => 'bar',
'values' => [
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,56 @@ public function testTypeConversionsWithNativePhp($key, $value, $supported)
}

public function getTypeConversions()
{
return [
['true_comment', true, true],
['true', true, true],
['false', false, true],
['on', true, true],
['off', false, true],
['yes', true, true],
['no', false, true],
['none', false, true],
['null', null, true],
['constant', \PHP_VERSION, true],
['12_int', 12, true],
['12_string', '12', true],
['12_quoted_number', 12, false], // INI_SCANNER_RAW removes the double quotes
['12_comment', 12, true],
['12_string_comment', '12', true],
['12_quoted_number_comment', 12, false], // INI_SCANNER_RAW removes the double quotes
['-12_negative', -12, true],
['one', 1, true],
['zero', 0, true],
['0b0110_byte_string', bindec('0b0110'), false], // not supported by INI_SCANNER_TYPED
['11112222333344445555_great_number', '1111,2222,3333,4444,5555', true],
['0777_number_starting_with_0', 0777, false], // not supported by INI_SCANNER_TYPED
['255_hexadecimal', 0xFF, false], // not supported by INI_SCANNER_TYPED
['100.0_exponential', 1e2, false], // not supported by INI_SCANNER_TYPED
['-120.0_exponential', -1.2E2, false], // not supported by INI_SCANNER_TYPED
['-10100.1_negative_float', -10100.1, false], // not supported by INI_SCANNER_TYPED
['-10,100.1_negative_float', '-10,100.1', true],
['list', [1, 2], true],
['map', ['one' => 1, 'two' => 2], true],
];
}

/**
* @group legacy
*
* @dataProvider getLegacyTypeConversions
*/
public function testLegacyTypeConversionsWithNativePhp($key, $value, $supported)
{
if (!$supported) {
$this->markTestSkipped(sprintf('Converting the value "%s" to "%s" is not supported by the IniFileLoader.', $key, $value));
}

$expected = parse_ini_file(__DIR__.'/../Fixtures/ini/types_legacy.ini', true, \INI_SCANNER_TYPED);
$this->assertSame($value, $expected['parameters'][$key], '->load() converts values to PHP types');
}

public function getLegacyTypeConversions()
{
return [
['true_comment', true, true],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testLoadParameters()

$actual = $container->getParameterBag()->all();
$expected = [
'a string',
'a_string' => 'a string',
'foo' => 'bar',
'values' => [
0,
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testLoadImports()

$actual = $container->getParameterBag()->all();
$expected = [
'a string',
'a_string' => 'a string',
'foo' => 'bar',
'values' => [
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ public function testGetSet()
}
}

/**
* @group legacy
* Test it will throw in 7.0
*/
public function testGetSetNumericName()
{
$bag = new ParameterBag(['foo']);
$bag->set(1001, 'foo');
$this->assertEquals('foo', $bag->get(1001), '->set() sets the value of a new parameter');

$bag->set(10.0, 'foo');
$this->assertEquals('foo', $bag->get(10), '->set() sets the value of a new parameter');

$bag->set(0b0110, 'foo');
$this->assertEquals('foo', $bag->get(0b0110), '->set() sets the value of a new parameter');

$bag->set('0', 'baz');
$this->assertEquals('baz', $bag->get(0), '->set() overrides previously set parameter');

$this->assertTrue($bag->has(0));
$this->assertTrue($bag->has(1001));
$this->assertTrue($bag->has(10));
$this->assertTrue($bag->has(0b0110));

foreach (array_keys($bag->all()) as $key) {
$this->assertIsInt($key, 'Numeric string keys are cast to integers');
}
}

/**
* @dataProvider provideGetThrowParameterNotFoundExceptionData
*/
Expand Down