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

Skip to content

Commit 176f52d

Browse files
committed
feature #38859 [HttpFoundation] Deprecate not passing a Closure together with FILTER_CALLBACK to ParameterBag::filter() (nicolas-grekas)
This PR was merged into the 5.2-dev branch. Discussion ---------- [HttpFoundation] Deprecate not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()` | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - Using `filter_var()` with a configurable flag is risky, because of the `FILTER_CALLBACK` flag. Restricting the type of callable that is accepted here mitigates the risk. We did the same in Twig: twigphp/Twig#3308 Commits ------- d6aea28 [HttpFoundation] Deprecate not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()`
2 parents 6b1b12f + d6aea28 commit 176f52d

File tree

7 files changed

+42
-0
lines changed

7 files changed

+42
-0
lines changed

UPGRADE-5.2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Form
3939
$builder->setDataMapper(new DataMapper(new PropertyPathAccessor()));
4040
```
4141

42+
HttpFoundation
43+
--------------
44+
45+
* Deprecated not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()`; wrap your filter in a closure instead.
46+
4247
Lock
4348
----
4449

UPGRADE-6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ HttpFoundation
6666
* Removed `Response::create()`, `JsonResponse::create()`,
6767
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
6868
`__construct()` instead)
69+
* Not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()` throws an `InvalidArgumentException`; wrap your filter in a closure instead.
6970

7071
HttpKernel
7172
----------

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* added ability to use comma separated ip addresses for `RequestMatcher::matchIps()`
1111
* added `Request::toArray()` to parse a JSON request body to an array
1212
* added `RateLimiter\RequestRateLimiterInterface` and `RateLimiter\AbstractRequestRateLimiter`
13+
* deprecated not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()`; wrap your filter in a closure instead.
1314

1415
5.1.0
1516
-----

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public function filter(string $key, $default = null, int $filter = \FILTER_DEFAU
103103
}
104104
}
105105

106+
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
107+
trigger_deprecation('symfony/http-foundation', '5.2', 'Not passing a Closure together with FILTER_CALLBACK to "%s()" is deprecated. Wrap your filter in a closure instead.', __METHOD__);
108+
// 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)));
109+
}
110+
106111
return filter_var($value, $filter, $options);
107112
}
108113
}

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ public function filter(string $key, $default = null, int $filter = \FILTER_DEFAU
194194
$options['flags'] = \FILTER_REQUIRE_ARRAY;
195195
}
196196

197+
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
198+
trigger_deprecation('symfony/http-foundation', '5.2', 'Not passing a Closure together with FILTER_CALLBACK to "%s()" is deprecated. Wrap your filter in a closure instead.', __METHOD__);
199+
// 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)));
200+
}
201+
197202
return filter_var($value, $filter, $options);
198203
}
199204

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function testFilterArray()
4545
$this->assertSame([12, 8], $result);
4646
}
4747

48+
/**
49+
* @group legacy
50+
*/
51+
public function testFilterCallback()
52+
{
53+
$bag = new InputBag(['foo' => 'bar']);
54+
55+
$this->expectDeprecation('Since symfony/http-foundation 5.2: Not passing a Closure together with FILTER_CALLBACK to "Symfony\Component\HttpFoundation\InputBag::filter()" is deprecated. Wrap your filter in a closure instead.');
56+
$this->assertSame('BAR', $bag->filter('foo', null, \FILTER_CALLBACK, ['options' => 'strtoupper']));
57+
}
58+
4859
/**
4960
* @group legacy
5061
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
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\ParameterBag;
1718

1819
class ParameterBagTest extends TestCase
1920
{
21+
use ExpectDeprecationTrait;
22+
2023
public function testConstructor()
2124
{
2225
$this->testAll();
@@ -176,6 +179,17 @@ public function testFilter()
176179
$this->assertEquals(['bang'], $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
177180
}
178181

182+
/**
183+
* @group legacy
184+
*/
185+
public function testFilterCallback()
186+
{
187+
$bag = new ParameterBag(['foo' => 'bar']);
188+
189+
$this->expectDeprecation('Since symfony/http-foundation 5.2: Not passing a Closure together with FILTER_CALLBACK to "Symfony\Component\HttpFoundation\ParameterBag::filter()" is deprecated. Wrap your filter in a closure instead.');
190+
$this->assertSame('BAR', $bag->filter('foo', null, \FILTER_CALLBACK, ['options' => 'strtoupper']));
191+
}
192+
179193
public function testGetIterator()
180194
{
181195
$parameters = ['foo' => 'bar', 'hello' => 'world'];

0 commit comments

Comments
 (0)