8000 [HttpFoundation] deprecate using parameterBag::get() with non-string … · symfony/symfony@ae1e05b · GitHub
[go: up one dir, main page]

Skip to content

Commit ae1e05b

Browse files
committed
[HttpFoundation] deprecate using parameterBag::get() with non-string values
1 parent c3455b4 commit ae1e05b

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CHANGELOG
1010
* made `Request::getSession()` throw if the session has not been set before
1111
* removed `Response::HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL`
1212
* passing a null url when instantiating a `RedirectResponse` is not allowed
13+
* deprecated using `ParameterBag::get()` with non string values
14+
* added `ParameterBag::getAll()`
1315

1416
4.4.0
1517
-----
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Exception;
13+
14+
/**
15+
* Raised when a user has sent a malformed request.
16+
*/
17+
class BadRequestException extends \UnexpectedValueException implements RequestExceptionInterface
18+
{
19+
}

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,40 @@ public function add(array $parameters = [])
6565
}
6666

6767
/**
68-
* Returns a parameter by name.
68+
* Returns a string parameter by name.
6969
*
70-
* @param mixed $default The default value if the parameter key does not exist
70+
* @param string|null $default The default value if the parameter key does not exist
7171
*
72-
* @return mixed
72+
* @return string|null
7373
*/
7474
public function get(string $key, $default = null)
7575
{
76-
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
76+
if (null !== $default && !\is_string($default)) {
77+
@trigger_error(sprintf('Passing a non-string value as 2nd argument to "%s::%s()" is deprecated since Symfony 5.0, pass a string or null instead.', static::class, __METHOD__), E_USER_DEPRECATED);
78+
}
79+
80+
$value = \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
81+
82+
if (null !== $value && $default !== $value && !\is_string($value)) {
83+
@trigger_error(sprintf('Retriving a non-string value from "%s::%s()" is deprecated since Symfony 5.0, use "%s::getAll()" instead.', static::class, __METHOD__, static::class), E_USER_DEPRECATED);
84+
}
85+
86+
return $value;
87+
}
88+
89+
/**
90+
* Returns an array of parameter by name.
91+
*
92+
* @param array|null $default The default value if the parameter key does not exist
93+
*/
94+
public function getAll(string $key, array $default = null): ?array
95+
{
96+
$value = \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
97+
if (null !== $value && \is_string($value)) {
98+
throw new Exception\BadRequestException(sprintf('Unexcepted value for %s parameter.', $key));
99+
}
100+
101+
return $value;
77102
}
78103

79104
/**

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

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

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
1516
use Symfony\Component\HttpFoundation\ParameterBag;
1617

1718
class ParameterBagTest extends TestCase
@@ -74,6 +75,22 @@ public function testGetDoesNotUseDeepByDefault()
7475
$this->assertNull($bag->get('foo[bar]'));
7576
}
7677

78+
public function testGetAll()
79+
{
80+
$bag = new ParameterBag(['foo' => ['bar', 'baz'], 'null' => null]);
81+
82+
$this->assertEquals(['bar', 'baz'], $bag->get('foo'), '->getAll() gets the value of a parameter');
83+
$this->ass 960E ertEquals(['default'], $bag->get('unknown', ['default']), '->getAll() returns second argument as default if a parameter is not defined');
84+
$this->assertNull($bag->get('null', ['foo', 'bar']), '->getAll() returns null if null is set');
85+
}
86+
87+
public function testGetAllThrowsForNonArrayValues()
88+
{
89+
$this->expectException(BadRequestException::class);
90+
$bag = new ParameterBag(['foo' => 'bar', 'null' => null]);
91+
$bag->getAll('foo');
92+
}
93+
7794
public function testSet()
7895
{
7996
$bag = new ParameterBag([]);

0 commit comments

Comments
 (0)
0