8000 [HttpFoundation] Deprecate `\Stringable` support in `InputBag` · symfony/symfony@da8fb96 · GitHub
[go: up one dir, main page]

Skip to content

Commit da8fb96

Browse files
committed
[HttpFoundation] Deprecate \Stringable support in InputBag
1 parent 0b1d173 commit da8fb96

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

UPGRADE-6.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ HttpFoundation
1313
--------------
1414

1515
* Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead
16+
* Deprecate passing `\Stringable` objects to `InputBag`, use scalars, arrays or null instead.
1617

1718
Mailer
1819
--------

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Deprecate passing `\Stringable` objects to `InputBag`, use scalars, arrays or null instead.
8+
49
6.1
5
---
611

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,26 @@ final class InputBag extends ParameterBag
2727
*/
2828
public function get(string $key, mixed $default = null): string|int|float|bool|null
2929
{
30+
// @deprecated since symfony 6.2, in 7.0 change to:
31+
// if (null !== $default && !\is_scalar($default)) {
3032
if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
3133
throw new \InvalidArgumentException(sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
3234
}
3335

36+
if ($default instanceof \Stringable) {
37+
trigger_deprecation('symfony/http-foundation', '6.2', 'Passing a "%s" object as 2nd argument ($default) to "%s()" is deprecated, pass a scalar or null instead.', get_debug_type($default), __METHOD__);
38+
}
39+
3440
$value = parent::get($key, $this);
3541

3642
if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
3743
throw new BadRequestException(sprintf('Input value "%s" contains a non-scalar value.', $key));
3844
}
3945

46+
if ($value instanceof \Stringable) {
47+
trigger_deprecation('symfony/http-foundation', '6.2', 'Retrieving a "%s" object from "%s()" is deprecated, use scalars or null instead.', get_debug_type($value), __METHOD__);
48+
}
49+
4050
return $this === $value ? $default : $value;
4151
}
4252

@@ -66,10 +76,16 @@ public function add(array $inputs = [])
6676
*/
6777
public function set(string $key, mixed $value)
6878
{
79+
// @deprecated since symfony 6.2, in 7.0 change to:
80+
// if (null !== $value && !\is_scalar($value) && !\is_array($value)) {
6981
if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
7082
throw new \InvalidArgumentException(sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
7183
}
7284

85+
if ($value instanceof \Stringable) {
86+
trigger_deprecation('symfony/http-foundation', '6.2', 'Passing a "%s" object as a 2nd argument ($value) to "%s()" is deprecated. Pass a scalar, an array or null instead.', get_debug_type($value), __METHOD__);
87+
}
88+
7389
$this->parameters[$key] = $value;
7490
}
7591

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,23 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
1617
use Symfony\Component\HttpFoundation\InputBag;
1718

1819
class InputBagTest extends TestCase
1920
{
21+
use ExpectDeprecationTrait;
22+
2023
public function testGet()
2124
{
22-
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false, 'stringable' => new class() implements \Stringable {
23-
public function __toString(): string
24-
{
25-
return 'strval';
26-
}
27-
}]);
25+
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false]);
2826

2927
$this->assertSame('bar', $bag->get('foo'), '->get() gets the value of a string parameter');
3028
$this->assertSame('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
3129
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
3230
$this->assertSame(1, $bag->get('int'), '->get() gets the value of an int parameter');
3331
$this->assertSame(1.0, $bag->get('float'), '->get() gets the value of a float parameter');
34-
$this->assertSame('strval', $bag->get('stringable'), '->get() gets the string value of a \Stringable parameter');
3532
$this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
3633
}
3734

@@ -106,4 +103,35 @@ public function testFilterArrayWithoutArrayFlag()
106103
$bag = new InputBag(['foo' => ['bar', 'baz']]);
107104
$bag->filter('foo', \FILTER_VALIDATE_INT);
108105
}
106+
107+
/**
108+
* @group legacy
109+
*/
110+
public function testLegacyGetStringableObject()
111+
{
112+
$bag = new InputBag(['stringable' => new class() implements \Stringable {
113+
public function __toString(): string
114+
{
115+
return 'strval';
116+
}
117+
}]);
118+
119+
$this->expectDeprecation('Since symfony/http-foundation 6.2: Retrieving a "Stringable@anonymous" object from "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, use scalars or null instead.');
120+
$this->assertSame('strval', $bag->get('stringable'), '->get() gets the string value of a \Stringable parameter');
121+
}
122+
123+
/**
124+
* @group legacy
125+
*/
126+
public function testLegacySetStringableObject()
127+
{
128+
$bag = new InputBag();
129+
$this->expectDeprecation('Since symfony/http-foundation 6.2: Passing a "Stringable@anonymous" object as a 2nd argument ($value) to "Symfony\Component\HttpFoundation\InputBag::set()" is deprecated. Pass a scalar, an array or null instead.');
130+
$bag->set('stringable', new class() implements \Stringable {
131+
public function __toString(): string
132+
{
133+
return 'strval';
134+
}
135+
});
136+
}
109137
}

0 commit comments

Comments
 (0)
0