Skip to content

Commit 0ffd65a

Browse files
VincentLangletnicolas-grekas
authored andcommitted
[HttpFoundation] Use Symfony exception for request unexpected values
1 parent 62d4c8c commit 0ffd65a

8 files changed

+35
-16
lines changed

Exception/BadRequestException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
/**
1515
* Raised when a user sends a malformed request.
1616
*/
17-
class BadRequestException extends \UnexpectedValueException implements RequestExceptionInterface
17+
class BadRequestException extends UnexpectedValueException implements RequestExceptionInterface
1818
{
1919
}

Exception/ConflictingHeadersException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
*
1717
* @author Magnus Nordlander <magnus@fervo.se>
1818
*/
19-
class ConflictingHeadersException extends \UnexpectedValueException implements RequestExceptionInterface
19+
class ConflictingHeadersException extends UnexpectedValueException implements RequestExceptionInterface
2020
{
2121
}

Exception/JsonException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
*
1717
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1818
*/
19-
final class JsonException extends \UnexpectedValueException implements RequestExceptionInterface
19+
final class JsonException extends UnexpectedValueException implements RequestExceptionInterface
2020
{
2121
}

Exception/SuspiciousOperationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
* Raised when a user has performed an operation that should be considered
1616
* suspicious from a security perspective.
1717
*/
18-
class SuspiciousOperationException extends \UnexpectedValueException implements RequestExceptionInterface
18+
class SuspiciousOperationException extends UnexpectedValueException implements RequestExceptionInterface
1919
{
2020
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
class UnexpectedValueException extends \UnexpectedValueException
15+
{
16+
}

InputBag.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation;
1313

1414
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
15+
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;
1516

1617
/**
1718
* InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
@@ -87,7 +88,7 @@ public function getEnum(string $key, string $class, \BackedEnum $default = null)
8788
{
8889
try {
8990
return parent::getEnum($key, $class, $default);
90-
} catch (\UnexpectedValueException $e) {
91+
} catch (UnexpectedValueException $e) {
9192
throw new BadRequestException($e->getMessage(), $e->getCode(), $e);
9293
}
9394
}

ParameterBag.php

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

1414
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
15+
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;
1516

1617
/**
1718
* ParameterBag is a container for key/value pairs.
@@ -140,7 +141,7 @@ public function getString(string $key, string $default = ''): string
140141
{
141142
$value = $this->get($key, $default);
142143
if (!\is_scalar($value) && !$value instanceof \Stringable) {
143-
throw new \UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "string".', $key));
144+
throw new UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "string".', $key));
144145
}
145146

146147
return (string) $value;
@@ -184,7 +185,7 @@ public function getEnum(string $key, string $class, \BackedEnum $default = null)
184185
try {
185186
return $class::from($value);
186187
} catch (\ValueError|\TypeError $e) {
187-
throw new \UnexpectedValueException(sprintf('Parameter "%s" cannot be converted to enum: %s.', $key, $e->getMessage()), $e->getCode(), $e);
188+
throw new UnexpectedValueException(sprintf('Parameter "%s" cannot be converted to enum: %s.', $key, $e->getMessage()), $e->getCode(), $e);
188189
}
189190
}
190191

@@ -211,7 +212,7 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER
211212
}
212213

213214
if (\is_object($value) && !$value instanceof \Stringable) {
214-
throw new \UnexpectedValueException(sprintf('Parameter value "%s" cannot be filtered.', $key));
215+
throw new UnexpectedValueException(sprintf('Parameter value "%s" cannot be filtered.', $key));
215216
}
216217

217218
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
@@ -232,7 +233,7 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER
232233
$method = ($method['object'] ?? null) === $this ? $method['function'] : 'filter';
233234
$hint = 'filter' === $method ? 'pass' : 'use method "filter()" with';
234235

235-
trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw an "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, \UnexpectedValueException::class);
236+
trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw an "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, UnexpectedValueException::class);
236237

237238
return false;
238239
}

Tests/ParameterBagTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1616
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
17+
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;
1718
use Symfony\Component\HttpFoundation\ParameterBag;
1819
use Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum;
1920

@@ -128,7 +129,7 @@ public function testGetAlphaExceptionWithArray()
128129
{
129130
$bag = new ParameterBag(['word' => ['foo_BAR_012']]);
130131

131-
$this->expectException(\UnexpectedValueException::class);
132+
$this->expectException(UnexpectedValueException::class);
132133
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "string".');
133134

134135
$bag->getAlpha('word');
@@ -149,7 +150,7 @@ public function testGetAlnumExceptionWithArray()
149150
{
150151
$bag = new ParameterBag(['word' => ['foo_BAR_012']]);
151152

152-
$this->expectException(\UnexpectedValueException::class);
153+
$this->expectException(UnexpectedValueException::class);
153154
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "string".');
154155

155156
$bag->getAlnum('word');
@@ -170,7 +171,7 @@ public function testGetDigitsExceptionWithArray()
170171
{
171172
$bag = new ParameterBag(['word' => ['foo_BAR_012']]);
172173

173-
$this->expectException(\UnexpectedValueException::class);
174+
$this->expectException(UnexpectedValueException::class);
174175
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "string".');
175176

176177
$bag->getDigits('word');
@@ -232,7 +233,7 @@ public function testGetStringExceptionWithArray()
232233
{
233234
$bag = new ParameterBag(['key' => ['abc']]);
234235

235-
$this->expectException(\UnexpectedValueException::class);
236+
$this->expectException(UnexpectedValueException::class);
236237
$this->expectExceptionMessage('Parameter value "key" cannot be converted to "string".');
237238

238239
$bag->getString('key');
@@ -242,7 +243,7 @@ public function testGetStringExceptionWithObject()
242243
{
243244
$bag = new ParameterBag(['object' => $this]);
244245

245-
$this->expectException(\UnexpectedValueException::class);
246+
$this->expectException(UnexpectedValueException::class);
246247
$this->expectExceptionMessage('Parameter value "object" cannot be converted to "string".');
247248

248249
$bag->getString('object');
@@ -359,7 +360,7 @@ public function testGetEnumThrowsExceptionWithNotBackingValue()
359360
{
360361
$bag = new ParameterBag(['invalid-value' => 2]);
361362

362-
$this->expectException(\UnexpectedValueException::class);
363+
$this->expectException(UnexpectedValueException::class);
363364
if (\PHP_VERSION_ID >= 80200) {
364365
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum.');
365366
} else {
@@ -373,7 +374,7 @@ public function testGetEnumThrowsExceptionWithInvalidValueType()
373374
{
374375
$bag = new ParameterBag(['invalid-value' => ['foo']]);
375376

376-
$this->expectException(\UnexpectedValueException::class);
377+
$this->expectException(UnexpectedValueException::class);
377378
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum::from(): Argument #1 ($value) must be of type int, array given.');
378379

379380
$this->assertNull($bag->getEnum('invalid-value', FooEnum::class));

0 commit comments

Comments
 (0)