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

Skip to content

Commit 522a590

Browse files
committed
New argument must be virtual
1 parent 9717f40 commit 522a590

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

UPGRADE-6.3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ SecurityBundle
1919
HttpFoundation
2020
--------------
2121

22-
* Set 3rd argument of `ParameterBag::getInt($key, $default, $failOnInvalid)` to `true` to make an exception in cas of invalid data.
22+
* Set 3rd argument of `ParameterBag::getInt($key, $default, $failOnInvalid)` to `true` to make an exception in case of invalid data.
2323
To keep the previous behaviour, cast the result of `ParameterBag::get()`: `(int) $parameters->get('foo')`

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ public function set(string $key, mixed $value)
7575

7676
/**
7777
* Returns the parameter value converted to integer.
78+
*
79+
* @param bool $failOnInvalid Throw an exception when the value is not a valid int
7880
*/
79-
public function getInt(string $key, int $default = 0, bool $failOnInvalid = false): int
81+
public function getInt(string $key, int $default = 0, /*bool $failOnInvalid = false*/): int
8082
{
83+
$failOnInvalid = func_num_args() >= 3 && func_get_arg(2);
84+
8185
if (false === $failOnInvalid) {
8286
trigger_deprecation('symfony/http-foundation', '6.3', '3rd argument "$failOnInvalid" of the method "%s" will always be "true" on 7.0.', __METHOD__);
8387

@@ -93,6 +97,7 @@ public function getInt(string $key, int $default = 0, bool $failOnInvalid = fals
9397

9498
public function getString(string $key, string $default = ''): string
9599
{
100+
// Shortcuts the parent method because the validation on scalar is already done in get().
96101
return (string) $this->get($key, $default);
97102
}
98103

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ public function getDigits(string $key, string $default = ''): string
126126

127127
/**
128128
* Returns the parameter value converted to integer.
129+
*
130+
* @param bool $failOnInvalid Throw an exception when the value is not a valid int
129131
*/
130-
public function getInt(string $key, int $default = 0, bool $failOnInvalid = false): int
132+
public function getInt(string $key, int $default = 0, /*bool $failOnInvalid = false*/): int
131133
{
134+
$failOnInvalid = func_num_args() >= 3 && func_get_arg(2);
135+
132136
if (false === $failOnInvalid) {
133137
trigger_deprecation('symfony/http-foundation', '6.3', '3rd argument "$failOnInvalid" of the method "%s" will always be "true" on 7.0.', __METHOD__);
134138

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,20 @@ public function testGetIntegerError()
4747

4848
public function testGetString()
4949
{
50-
$bag = new InputBag(['integer' => 123, 'bool_true' => true, 'bool_false' => false, 'string' => 'abc', 'stringable' => new ParamStringable('bar')]);
50+
$bag = new InputBag(['integer' => 123, 'bool_true' => true, 'bool_false' => false, 'string' => 'abc', 'stringable' => new class() implements \Stringable {
51+
public function __toString(): string
52+
{
53+
return 'strval';
54+
}
55+
}]);
5156

5257
$this->assertSame('123', $bag->getString('integer'), '->getString() gets a value of parameter as string');
5358
$this->assertSame('abc', $bag->getString('string'), '->getString() gets a value of parameter as string');
5459
$this->assertSame('', $bag->getString('unknown'), '->getString() returns zero if a parameter is not defined');
5560
$this->assertSame('foo', $bag->getString('unknown', 'foo'), '->getString() returns the default if a parameter is not defined');
5661
$this->assertSame('1', $bag->getString('bool_true'), '->getString() returns "1" if a parameter is true');
5762
$this->assertSame('', $bag->getString('bool_false', 'foo'), '->getString() returns an empty empty string if a parameter is false');
58-
$this->assertSame('bar', $bag->getString('stringable'), '->getString() gets a value of a stringable paramater as string');
63+
$this->assertSame('strval', $bag->getString('stringable'), '->getString() gets a value of a stringable paramater as string');
5964
}
6065

6166
public function testGetStringExceptionWithArray()
@@ -170,15 +175,3 @@ public function testGetDigitsExceptionWithArray()
170175
$bag->getDigits('word');
171176
}
172177
}
173-
174-
class ParamStringable
175-
{
176-
public function __construct(private string $value)
177-
{
178-
}
179-
180-
public function __toString(): string
181-
{
182-
return $this->value;
183-
}
184-
}

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,20 @@ public function testGetIntExceptionWithInvalid()
233233

234234
public function testGetString()
235235
{
236-
$bag = new ParameterBag(['integer' => 123, 'bool_true' => true, 'bool_false' => false, 'string' => 'abc', 'stringable' => new InputStringable('bar')]);
236+
$bag = new ParameterBag(['integer' => 123, 'bool_true' => true, 'bool_false' => false, 'string' => 'abc', 'stringable' => new class() implements \Stringable {
237+
public function __toString(): string
238+
{
239+
return 'strval';
240+
}
241+
}]);
237242

238243
$this->assertSame('123', $bag->getString('integer'), '->getString() gets a value of parameter as string');
239244
$this->assertSame('abc', $bag->getString('string'), '->getString() gets a value of parameter as string');
240245
$this->assertSame('', $bag->getString('unknown'), '->getString() returns zero if a parameter is not defined');
241246
$this->assertSame('foo', $bag->getString('unknown', 'foo'), '->getString() returns the default if a parameter is not defined');
242247
$this->assertSame('1', $bag->getString('bool_true'), '->getString() returns "1" if a parameter is true');
243248
$this->assertSame('', $bag->getString('bool_false', 'foo'), '->getString() returns an empty empty string if a parameter is false');
244-
$this->assertSame('bar', $bag->getString('stringable'), '->getString() gets a value of a stringable paramater as string');
249+
$this->assertSame('strval', $bag->getString('stringable'), '->getString() gets a value of a stringable paramater as string');
245250
}
246251

247252
public function testGetStringExceptionWithArray()
@@ -353,15 +358,3 @@ public function testGetBoolean()
353358
$this->assertFalse($bag->getBoolean('string', true), '->getBoolean() returns false if a parameter is invalid');
354359
}
355360
}
356-
357-
class InputStringable
358-
{
359-
public function __construct(private string $value)
360-
{
361-
}
362-
363-
public function __toString(): string
364-
{
365-
return $this->value;
366-
}
367-
}

0 commit comments

Comments
 (0)