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 231f96c

Browse filesBrowse files
committed
Add a 2rd argument to getInt to make it strict. Add deprecation when not set to true
1 parent ce6910d commit 231f96c
Copy full SHA for 231f96c

File tree

5 files changed

+84
-38
lines changed
Filter options

5 files changed

+84
-38
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
CHANGELOG
22
=========
3+
34
6.3
45
---
56

6-
* Deprecate `ParameterBag::getInt`, replaced by `ParameterBag::getInteger` to behavior on invalid value
7-
* Add `ParameterBag::getString` and `ParameterBag::getInteger` to validate and convert a parameter into string or int
8-
* Calling `ParameterBag` `getDigit`, `getAlnum`, `getAlpha`, `getInteger`, `getString` on an `array` throws an `UnexpectedValueException` instead of a `TypeError`
7+
* Add a 3rd argument to `ParameterBag::getInt`. When set to `true`, the method throw an exception if the parameter is invalid
8+
* Deprecate calling `ParameterBag::getInt` without setting `true` as 3rd argument
9+
* Add `ParameterBag::getString` to convert a parameter into string and throw an exception if the parameter is invalid
10+
* Calling `ParameterBag` `getDigit`, `getAlnum`, `getAlpha` on an `array` throws an `UnexpectedValueException` instead of a `TypeError`
911

1012
6.2
1113
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/InputBag.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ public function set(string $key, mixed $value)
7373
$this->parameters[$key] = $value;
7474
}
7575

76+
/**
77+
* Returns the parameter value converted to integer.
78+
*/
79+
public function getInt(string $key, int $default = 0, bool $failOnInvalid = false): int
80+
{
81+
if (false === $failOnInvalid) {
82+
trigger_deprecation('symfony/http-foundation', '6.3', '3rd argument "$failOnInvalid" of the method "%s" will always be "true" on 7.0.', __METHOD__);
83+
84+
return (int) $this->get($key, $default);
85+
}
86+
$value = $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]);
87+
if (false === $value) {
88+
throw new BadRequestException(sprintf('Parameter value "%s" cannot be converted to "int".', $key));
89+
}
90+
91+
return $value;
92+
}
93+
7694
public function getString(string $key, string $default = ''): string
7795
{
7896
return (string) $this->get($key, $default);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/ParameterBag.php
+7-21Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,33 +121,21 @@ public function getAlnum(string $key, string $default = ''): string
121121
*/
122122
public function getDigits(string $key, string $default = ''): string
123123
{
124-
$value = $this->filter($key, $default, \FILTER_SANITIZE_NUMBER_INT, ['flags' => \FILTER_REQUIRE_SCALAR]);
125-
if (false === $value) {
126-
throw new \UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "string".', $key));
127-
}
128-
129-
// we need to remove - and + because they're allowed in the filter
130-
return str_replace(['-', '+'], '', $value);
124+
return preg_replace('/[^[:digit:]]/', '', $this->getString($key, $default));
131125
}
132126

133127
/**
134128
* Returns the parameter value converted to integer.
135-
* @deprecated since Symfony 6.3, replaced by getInteger
136129
*/
137-
public function getInt(string $key, int $default = 0): int
130+
public function getInt(string $key, int $default = 0, bool $failOnInvalid = false): int
138131
{
139-
trigger_deprecation('symfony/http-foundation', '6.3', 'Method "getInt" is deprecated and replaced by "getInteger".');
132+
if (false === $failOnInvalid) {
133+
trigger_deprecation('symfony/http-foundation', '6.3', '3rd argument "$failOnInvalid" of the method "%s" will always be "true" on 7.0.', __METHOD__);
140134

141-
return (int) $this->get($key, $default);
142-
}
143-
144-
/**
145-
* Returns the parameter value converted to integer.
146-
*/
147-
public function getInteger(string $key, int $default = 0): int
148-
{
135+
return (int) $this->get($key, $default);
136+
}
149137
$value = $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]);
150-
if (!\is_int($value)) {
138+
if (false === $value) {
151139
throw new \UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "int".', $key));
152140
}
153141

@@ -156,8 +144,6 @@ public function getInteger(string $key, int $default = 0): int
156144

157145
/**
158146
* Returns the parameter as string.
159-
*
160-
* @@throws \UnexpectedValueException when
161147
*/
162148
public function getString(string $key, string $default = ''): string
163149
{

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public function __toString(): string
3535
$this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
3636
}
3737

38+
public function testGetIntegerError()
39+
{
40+
$bag = new InputBag(['foo' => 'bar']);
41+
42+
$this->expectException(BadRequestException::class);
43+
$this->expectExceptionMessage('Parameter value "foo" cannot be converted to "int".');
44+
45+
$bag->getInt('foo', 0, true);
46+
}
47+
3848
public function testGetString()
3949
{
4050
$bag = new InputBag(['integer' => 123, 'bool_true' => true, 'bool_false' => false, 'string' => 'abc', 'stringable' => new ParamStringable('bar')]);
@@ -129,6 +139,36 @@ public function testFilterArrayWithoutArrayFlag()
129139
$bag = new InputBag(['foo' => ['bar', 'baz']]);
130140
$bag->filter('foo', \FILTER_VALIDATE_INT);
131141
}
142+
143+
public function testGetAlnumExceptionWithArray()
144+
{
145+
$bag = new InputBag(['word' => ['foo_BAR_012']]);
146+
147+
$this->expectException(BadRequestException::class);
148+
$this->expectExceptionMessage('Input value "word" contains a non-scalar value.');
149+
150+
$bag->getAlnum('word');
151+
}
152+
153+
public function testGetAlphaExceptionWithArray()
154+
{
155+
$bag = new InputBag(['word' => ['foo_BAR_012']]);
156+
157+
$this->expectException(BadRequestException::class);
158+
$this->expectExceptionMessage('Input value "word" contains a non-scalar value.');
159+
160+
$bag->getAlpha('word');
161+
}
162+
163+
public function testGetDigitsExceptionWithArray()
164+
{
165+
$bag = new InputBag(['word' => ['foo_BAR_012']]);
166+
167+
$this->expectException(BadRequestException::class);
168+
$this->expectExceptionMessage('Input value "word" contains a non-scalar value.');
169+
170+
$bag->getDigits('word');
171+
}
132172
}
133173

134174
class ParamStringable

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
+14-14Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testGetAlnumExceptionWithArray()
156156

157157
public function testGetDigits()
158158
{
159-
$bag = new ParameterBag(['word' => 'foo_BAR_012', 'bool' => true, 'integer' => 123]);
159+
$bag = new ParameterBag(['word' => 'foo_BAR_0+1-2', 'bool' => true, 'integer' => 123]);
160160

161161
$this->assertSame('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
162162
$this->assertSame('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
@@ -178,9 +178,9 @@ public function testGetDigitsExceptionWithArray()
178178
/**
179179
* @group legacy
180180
*/
181-
public function testGetInt()
181+
public function testGetIntLegacy()
182182
{
183-
$this->expectDeprecation('Since symfony/http-foundation 6.3: Method "getInt" is deprecated and replaced by "getInteger".');
183+
$this->expectDeprecation('Since symfony/http-foundation 6.3: 3rd argument "$failOnInvalid" of the method "Symfony\Component\HttpFoundation\ParameterBag::getInt" will always be "true" on 7.0.');
184184

185185
$bag = new ParameterBag(['digits' => '123', 'bool' => true, 'string' => 'abc']);
186186

@@ -191,44 +191,44 @@ public function testGetInt()
191191
$this->assertSame(0, $bag->getInt('string', 10), '->getInt() returns zero if a parameter is invalid');
192192
}
193193

194-
public function testGetInteger()
194+
public function testGetInt()
195195
{
196196
$bag = new ParameterBag(['digits' => '123', 'bool' => true]);
197197

198-
$this->assertSame(123, $bag->getInteger('digits'), '->getInteger() gets a value of parameter as integer');
199-
$this->assertSame(0, $bag->getInteger('unknown'), '->getInteger() returns zero if a parameter is not defined');
200-
$this->assertSame(10, $bag->getInteger('unknown', 10), '->getInteger() returns the default if a parameter is not defined');
201-
$this->assertSame(1, $bag->getInteger('bool'), '->getInteger() returns 1 if a parameter is true');
198+
$this->assertSame(123, $bag->getInt('digits', 0, true), '->getInt() gets a value of parameter as integer');
199+
$this->assertSame(0, $bag->getInt('unknown', 0, true), '->getInt() returns zero if a parameter is not defined');
200+
$this->assertSame(10, $bag->getInt('unknown', 10, true), '->getInt() returns the default if a parameter is not defined');
201+
$this->assertSame(1, $bag->getInt('bool', 0, true), '->getInt() returns 1 if a parameter is true');
202202
}
203203

204-
public function testGetIntegerExceptionWithArray()
204+
public function testGetIntExceptionWithArray()
205205
{
206206
$bag = new ParameterBag(['digits' => ['123']]);
207207

208208
$this->expectException(\UnexpectedValueException::class);
209209
$this->expectExceptionMessage('Parameter value "digits" cannot be converted to "int".');
210210

211-
$bag->getInteger('digits');
211+
$bag->getInt('digits', 0, true);
212212
}
213213

214-
public function testGetIntegerExceptionWithObject()
214+
public function testGetIntExceptionWithObject()
215215
{
216216
$bag = new ParameterBag(['object' => $this]);
217217

218218
$this->expectException(\UnexpectedValueException::class);
219219
$this->expectExceptionMessage('Parameter value "object" cannot be filtered.');
220220

221-
$bag->getInteger('object');
221+
$bag->getInt('object', 0, true);
222222
}
223223

224-
public function testGetIntegerExceptionWithInvalid()
224+
public function testGetIntExceptionWithInvalid()
225225
{
226226
$bag = new ParameterBag(['word' => 'foo_BAR_012']);
227227

228228
$this->expectException(\UnexpectedValueException::class);
229229
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "int".');
230230

231-
$bag->getInteger('word');
231+
$bag->getInt('word', 0, true);
232232
}
233233

234234
public function testGetString()

0 commit comments

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