8000 use InputBag for POST requests too, added missing scalar type hints · symfony/symfony@381a0a1 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 381a0a1

Browse files
committed
use InputBag for POST requests too, added missing scalar type hints
1 parent 79f6a5c commit 381a0a1

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
final class InputBag extends ParameterBag
2222
{
2323
/**
24-
* Returns a string input value by name.
24+
* Returns a scalar input value by name.
2525
*
26-
* @param string|null $default The default value if the input key does not exist
26+
* @param string|int|float|bool|null $default The default value if the input key does not exist
2727
*
28-
* @return string|null
28+
* @return string|int|float|bool|null
2929
*/
3030
public function get(string $key, $default = null)
3131
{
3232
if (null !== $default && !is_scalar($default) && !(\is_object($default) && method_exists($default, '__toString'))) {
33-
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead.', __METHOD__);
33+
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing a non-scalar value as 2nd argument to "%s()" is deprecated, pass a scalar or null instead.', __METHOD__);
3434
}
3535

3636
$value = parent::get($key, $this);
@@ -72,12 +72,12 @@ public function add(array $inputs = [])
7272
/**
7373
* Sets an input by name.
7474
*
75-
* @param string|array|null $value
75+
* @param string|int|float|bool|array|null $value
7676
*/
7777
public function set(string $key, $value)
7878
{
7979
if (null !== $value && !is_scalar($value) && !\is_array($value) && !method_exists($value, '__toString')) {
80-
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead.', get_debug_type($value), __METHOD__);
80+
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a scalar, array, or null instead.', get_debug_type($value), __METHOD__);
8181
}
8282

8383
$this->parameters[$key] = $value;

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Request
9191
/**
9292
* Request body parameters ($_POST).
9393
*
94-
* @var InputBag|ParameterBag
94+
* @var InputBag
9595
*/
9696
public $request;
9797

@@ -275,7 +275,7 @@ public function __construct(array $query = [], array $request = [], array $attri
275275
*/
276276
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
277277
{
278-
$this->request = new ParameterBag($request);
278+
$this->request = new InputBag($request);
279279
$this->query = new InputBag($query);
280280
$this->attributes = new ParameterBag($attributes);
281281
$this->cookies = new InputBag($cookies);
@@ -305,9 +305,7 @@ public static function createFromGlobals()
305305
{
306306
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
307307

308-
if ($_POST) {
309-
$request->request = new InputBag($_POST);
310-
} elseif (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
308+
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
311309
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
312310
) {
313311
parse_str($request->getContent(), $data);
@@ -457,7 +455,7 @@ public function duplicate(array $query = null, array $request = null, array $att
457455
$dup->query = new InputBag($query);
458456
}
459457
if (null !== $request) {
460-
$dup->request = new ParameterBag($request);
458+
$dup->request = new InputBag($request);
461459
}
462460
if (null !== $attributes) {
463461
$dup->attributes = new ParameterBag($attributes);

src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ class InputBagTest extends TestCase
2121

2222
public function testGet()
2323
{
24-
$bag = new InputBag(['foo' => 'bar', 'null' => null]);
24+
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false]);
2525

26-
$this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
27-
$this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
26+
$this->assertSame('bar', $bag->get('foo'), '->get() gets the value of a string parameter');
27+
$this->assertSame('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
2828
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
29+
$this->assertSame(1, $bag->get('int'), '->get() gets the value of an int parameter');
30+
$this->assertSame(1.0, $bag->get('float'), '->get() gets the value of a float parameter');
31+
$this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
2932
}
3033

3134
public function testGetDoesNotUseDeepByDefault()
@@ -59,10 +62,10 @@ public function testFilterCallback()
5962
/**
6063
* @group legacy
6164
*/
62-
public function testSetWithNonStringishOrArrayIsDeprecated()
65+
public function testSetWithNonScalarOrArrayIsDeprecated()
6366
{
6467
$bag = new InputBag();
65-
$this->expectDeprecation('Since symfony/http-foundation 5.1: Passing "Symfony\Component\HttpFoundation\InputBag" as a 2nd Argument to "Symfony\Component\HttpFoundation\InputBag::set()" is deprecated, pass a string, array, or null instead.');
68+
$this->expectDeprecation('Since symfony/http-foundation 5.1: Passing "Symfony\Component\HttpFoundation\InputBag" as a 2nd Argument to "Symfony\Component\HttpFoundation\InputBag::set()" is deprecated, pass a scalar, array, or null instead.');
6669
$bag->set('foo', new InputBag());
6770
}
6871

@@ -82,7 +85,7 @@ public function testGettingANonStringValueIsDeprecated()
8285
public function testGetWithNonStringDefaultValueIsDeprecated()
8386
{
8487
$bag = new InputBag(['foo' => 'bar']);
85-
$this->expectDeprecation('Since symfony/http-foundation 5.1: Passing a non-string value as 2nd argument to "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, pass a string or null instead.');
88+
$this->expectDeprecation('Since symfony/http-foundation 5.1: Passing a non-scalar value as 2nd argument to "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, pass a scalar or null instead.');
8689
$bag->get('foo', ['a', 'b']);
8790
}
8891

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,11 +1287,6 @@ public function testCreateFromGlobals($method)
12871287
{
12881288
$normalizedMethod = strtoupper($method);
12891289

1290-
$_POST = [];
1291-
$request = Request::createFromGlobals();
1292-
$this->assertNotInstanceOf(InputBag::class, $request->request);
1293-
$this->assertInstanceOf(ParameterBag::class, $request->request);
1294-
12951290
$_GET['foo1'] = 'bar1';
12961291
$_POST['foo2'] = 'bar2';
12971292
$_COOKIE['foo3'] = 'bar3';

0 commit comments

Comments
 (0)
0