Skip to content

Navigation Menu

Sign in
Appearance settings

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 70261b3

Browse filesBrowse files
committed
Deprecate calling without
1 parent bb1bf04 commit 70261b3
Copy full SHA for 70261b3

File tree

Expand file treeCollapse file tree

6 files changed

+32
-49
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+32
-49
lines changed

‎UPGRADE-6.3.md

Copy file name to clipboardExpand all lines: UPGRADE-6.3.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ HttpFoundation
6161
--------------
6262

6363
* Deprecate conversion of invalid values in `ParameterBag::getInt()` and `ParameterBag::getBoolean()`, an exception will be thrown in 7.0
64+
* Deprecate calling `ParameterBag::filter` without `FILTER_NULL_ON_FAILURE` flag, an exception will be thrown in 7.0
6465

6566
HttpKernel
6667
----------

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate conversion of invalid values in `ParameterBag::getInt()` and `ParameterBag::getBoolean()`
8+
* Deprecate calling `ParameterBag::filter` without `FILTER_NULL_ON_FAILURE` flag
89
* Calling `ParameterBag::getDigit()`, `getAlnum()`, `getAlpha()` on an `array` throws a `UnexpectedValueException` instead of a `TypeError`
910
* Add `ParameterBag::getString()` to convert a parameter into string and throw an exception if the value is invalid
1011
* Add `ParameterBag::getEnum()`

‎src/Symfony/Component/HttpFoundation/InputBag.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/InputBag.php
+8-6Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,18 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER
118118
throw new \InvalidArgumentException(sprintf('A Closure must be passed to "%s()" when FILTER_CALLBACK is used, "%s" given.', __METHOD__, get_debug_type($options['options'] ?? null)));
119119
}
120120

121-
if ($errorOnFailure = $options['error_on_failure'] ?? false) {
122-
$options['flags'] |= \FILTER_NULL_ON_FAILURE;
123-
}
121+
$options['flags'] ??= 0;
122+
$nullOnFailure = (bool) ($options['flags'] & \FILTER_NULL_ON_FAILURE);
123+
$options['flags'] |= \FILTER_NULL_ON_FAILURE;
124124

125125
$value = filter_var($value, $filter, $options);
126126

127-
if (null === $value && $errorOnFailure) {
128-
throw new BadRequestException(sprintf('Parameter value "%s" is invalid.', $key));
127+
if (null !== $value || $nullOnFailure) {
128+
return $value;
129129
}
130130

131-
return $value;
131+
trigger_deprecation('symfony/http-foundation', '6.3', 'Not passing flag "FILTER_NULL_ON_FAILURE" to "%s(%s)" is deprecated. In Symfony 7.0, this method will throw a "%s" when the value is invalid and this flag is omitted.', __METHOD__, $key, BadRequestException::class);
132+
133+
return false;
132134
}
133135
}

‎src/Symfony/Component/HttpFoundation/ParameterBag.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/ParameterBag.php
+13-22Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,27 +151,16 @@ public function getString(string $key, string $default = ''): string
151151
*/
152152
public function getInt(string $key, int $default = 0): int
153153
{
154-
try {
155-
return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR, 'error_on_failure' => true]);
156-
} catch (\UnexpectedValueException|BadRequestException $e) {
157-
trigger_deprecation('symfony/http-foundation', '6.3', 'Invalid values for "%s::%s(%s)" will throw a "%s" exception in version 7.0.', static::class, __FUNCTION__, $key, $e::class);
158-
159-
return 0;
160-
}
154+
// In 7.0 remove the fallback to 0, in case of failure an exception will be thrown
155+
return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]) ?: 0;
161156
}
162157

163158
/**
164159
* Returns the parameter value converted to boolean.
165160
*/
166161
public function getBoolean(string $key, bool $default = false): bool
167162
{
168-
try {
169-
return $this->filter($key, $default, \FILTER_VALIDATE_BOOL, ['flags' => \FILTER_REQUIRE_SCALAR, 'error_on_failure' => true]);
170-
} catch (\UnexpectedValueException|BadRequestException $e) {
171-
trigger_deprecation('symfony/http-foundation', '6.3', 'Invalid values for "%s::%s(%s)" will throw a "%s" exception in version 7.0.', static::class, __FUNCTION__, $key, $e::class);
172-
173-
return false;
174-
}
163+
return $this->filter($key, $default, \FILTER_VALIDATE_BOOL, ['flags' => \FILTER_REQUIRE_SCALAR]);
175164
}
176165

177166
/**
@@ -202,8 +191,8 @@ public function getEnum(string $key, string $class, \BackedEnum $default = null)
202191
/**
203192
* Filter key.
204193
*
205-
* @param int $filter FILTER_* constant
206-
* @param int|array{flags?: int, options?: array, error_on_failure?: bool} $options Flags from FILTER_* constants
194+
* @param int $filter FILTER_* constant
195+
* @param int|array{flags?: int, options?: array} $options Flags from FILTER_* constants
207196
*
208197
* @see https://php.net/filter-var
209198
*/
@@ -229,17 +218,19 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER
229218
throw new \InvalidArgumentException(sprintf('A Closure must be passed to "%s()" when FILTER_CALLBACK is used, "%s" given.', __METHOD__, get_debug_type($options['options'] ?? null)));
230219
}
231220

232-
if ($errorOnFailure = $options['error_on_failure'] ?? false) {
233-
$options['flags'] |= \FILTER_NULL_ON_FAILURE;
234-
}
221+
$options['flags'] ??= 0;
222+
$nullOnFailure = (bool) ($options['flags'] & \FILTER_NULL_ON_FAILURE);
223+
$options['flags'] |= \FILTER_NULL_ON_FAILURE;
235224

236225
$value = filter_var($value, $filter, $options);
237226

238-
if (null === $value && $errorOnFailure) {
239-
throw new \UnexpectedValueException(sprintf('Parameter value "%s" is invalid.', $key));
227+
if (null !== $value || $nullOnFailure) {
228+
return $value;
240229
}
241230

242-
return $value;
231+
trigger_deprecation('symfony/http-foundation', '6.3', 'Not passing flag "FILTER_NULL_ON_FAILURE" to "%s(%s)" is deprecated. In Symfony 7.0, this method will throw a "%s" when the value is invalid and this flag is omitted.', __METHOD__, $key, \UnexpectedValueException::class);
232+
233+
return false;
243234
}
244235

245236
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __toString(): string
4444
*/
4545
public function testGetIntError()
4646
{
47-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Invalid values for "Symfony\Component\HttpFoundation\InputBag::getInt(foo)" will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" exception in version 7.0.');
47+
$this->expectDeprecation('Since symfony/http-foundation 6.3: Not passing flag "FILTER_NULL_ON_FAILURE" to "Symfony\Component\HttpFoundation\InputBag::filter(foo)" is deprecated. In Symfony 7.0, this method will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" when the value is invalid and this flag is omitted.');
4848

4949
$bag = new InputBag(['foo' => 'bar']);
5050
$result = $bag->getInt('foo');
@@ -56,7 +56,7 @@ public function testGetIntError()
5656
*/
5757
public function testGetBooleanError()
5858
{
59-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Invalid values for "Symfony\Component\HttpFoundation\InputBag::getBoolean(foo)" will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" exception in version 7.0.');
59+
$this->expectDeprecation('Since symfony/http-foundation 6.3: Not passing flag "FILTER_NULL_ON_FAILURE" to "Symfony\Component\HttpFoundation\InputBag::filter(foo)" is deprecated. In Symfony 7.0, this method will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" when the value is invalid and this flag is omitted.');
6060

6161
$bag = new InputBag(['foo' => 'bar']);
6262
$result = $bag->getBoolean('foo');

‎src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
+7-19Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,31 +191,19 @@ public function testGetInt()
191191
*/
192192
public function testGetIntExceptionWithArray()
193193
{
194-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Invalid values for "Symfony\Component\HttpFoundation\ParameterBag::getInt(digits)" will throw a "UnexpectedValueException" exception in version 7.0.');
194+
$this->expectDeprecation('Since symfony/http-foundation 6.3: Not passing flag "FILTER_NULL_ON_FAILURE" to "Symfony\Component\HttpFoundation\ParameterBag::filter(digits)" is deprecated. In Symfony 7.0, this method will throw a "UnexpectedValueException" when the value is invalid and this flag is omitted.');
195195

196196
$bag = new ParameterBag(['digits' => ['123']]);
197197
$result = $bag->getInt('digits', 0);
198198
$this->assertSame(0, $result);
199199
}
200200

201-
/**
202-
* @group legacy
203-
*/
204-
public function testGetIntExceptionWithObject()
205-
{
206-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Invalid values for "Symfony\Component\HttpFoundation\ParameterBag::getInt(object)" will throw a "UnexpectedValueException" exception in version 7.0.');
207-
208-
$bag = new ParameterBag(['object' => $this]);
209-
$result = $bag->getInt('object', 0);
210-
$this->assertSame(0, $result);
211-
}
212-
213201
/**
214202
* @group legacy
215203
*/
216204
public function testGetIntExceptionWithInvalid()
217205
{
218-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Invalid values for "Symfony\Component\HttpFoundation\ParameterBag::getInt(word)" will throw a "UnexpectedValueException" exception in version 7.0.');
206+
$this->expectDeprecation('Since symfony/http-foundation 6.3: Not passing flag "FILTER_NULL_ON_FAILURE" to "Symfony\Component\HttpFoundation\ParameterBag::filter(word)" is deprecated. In Symfony 7.0, this method will throw a "UnexpectedValueException" when the value is invalid and this flag is omitted.');
219207

220208
$bag = new ParameterBag(['word' => 'foo_BAR_012']);
221209
$result = $bag->getInt('word', 0);
@@ -282,13 +270,13 @@ public function testFilter()
282270
// This test is repeated for code-coverage
283271
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', \FILTER_VALIDATE_URL, \FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
284272

285-
$this->assertFalse($bag->filter('dec', '', \FILTER_VALIDATE_INT, [
286-
'flags' => \FILTER_FLAG_ALLOW_HEX,
273+
$this->assertNull($bag->filter('dec', '', \FILTER_VALIDATE_INT, [
274+
'flags' => \FILTER_FLAG_ALLOW_HEX | \FILTER_NULL_ON_FAILURE,
287275
'options' => ['min_range' => 1, 'max_range' => 0xFF],
288276
]), '->filter() gets a value of parameter as integer between boundaries');
289277

290-
$this->assertFalse($bag->filter('hex', '', \FILTER_VALIDATE_INT, [
291-
'flags' => \FILTER_FLAG_ALLOW_HEX,
278+
$this->assertNull($bag->filter('hex', '', \FILTER_VALIDATE_INT, [
279+
'flags' => \FILTER_FLAG_ALLOW_HEX | \FILTER_NULL_ON_FAILURE,
292280
'options' => ['min_range' => 1, 'max_range' => 0xFF],
293281
]), '->filter() gets a value of parameter as integer between boundaries');
294282

@@ -350,7 +338,7 @@ public function testGetBoolean()
350338
*/
351339
public function testGetBooleanExceptionWithInvalid()
352340
{
353-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Invalid values for "Symfony\Component\HttpFoundation\ParameterBag::getBoolean(invalid)" will throw a "UnexpectedValueException" exception in version 7.0.');
341+
$this->expectDeprecation('Since symfony/http-foundation 6.3: Not passing flag "FILTER_NULL_ON_FAILURE" to "Symfony\Component\HttpFoundation\ParameterBag::filter(invalid)" is deprecated. In Symfony 7.0, this method will throw a "UnexpectedValueException" when the value is invalid and this flag is omitted.');
354342

355343
$bag = new ParameterBag(['invalid' => 'foo']);
356344
$result = $bag->getBoolean('invalid', 0);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.