Skip to content

Commit aa5d0ea

Browse files
committed
feature #38269 [String] allow passing null to string functions (kbond)
This PR was submitted for the 5.1 branch but it was merged into the 5.2-dev branch instead. Discussion ---------- [String] allow passing null to string functions | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #38260 | License | MIT | Doc PR | n/a I also added a default value for `s` to match the other functions. Commits ------- f20a318 [String] allow passing null to string functions
2 parents d635390 + f20a318 commit aa5d0ea

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/Symfony/Component/String/Resources/functions.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111

1212
namespace Symfony\Component\String;
1313

14-
function u(string $string = ''): UnicodeString
14+
function u(?string $string = ''): UnicodeString
1515
{
16-
return new UnicodeString($string);
16+
return new UnicodeString($string ?? '');
1717
}
1818

19-
function b(string $string = ''): ByteString
19+
function b(?string $string = ''): ByteString
2020
{
21-
return new ByteString($string);
21+
return new ByteString($string ?? '');
2222
}
2323

2424
/**
2525
* @return UnicodeString|ByteString
2626
*/
27-
function s(string $string): AbstractString
27+
function s(?string $string = ''): AbstractString
2828
{
29+
$string = $string ?? '';
30+
2931
return preg_match('//u', $string) ? new UnicodeString($string) : new ByteString($string);
3032
}

src/Symfony/Component/String/Tests/FunctionsTest.php

+43-3
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,67 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\String\AbstractString;
16+
use function Symfony\Component\String\b;
1617
use Symfony\Component\String\ByteString;
1718
use function Symfony\Component\String\s;
19+
use function Symfony\Component\String\u;
1820
use Symfony\Component\String\UnicodeString;
1921

2022
final class FunctionsTest extends TestCase
2123
{
2224
/**
23-
* @dataProvider provideStrings
25+
* @dataProvider provideSStrings
2426
*/
25-
public function testS(AbstractString $expected, string $input)
27+
public function testS(AbstractString $expected, ?string $input)
2628
{
2729
$this->assertEquals($expected, s($input));
2830
}
2931

30-
public function provideStrings(): array
32+
public function provideSStrings(): array
3133
{
3234
return [
35+
[new UnicodeString(''), ''],
36+
[new UnicodeString(''), null],
3337
[new UnicodeString('foo'), 'foo'],
3438
[new UnicodeString('अनुच्छेद'), 'अनुच्छेद'],
3539
[new ByteString("b\x80ar"), "b\x80ar"],
3640
[new ByteString("\xfe\xff"), "\xfe\xff"],
3741
];
3842
}
43+
44+
/**
45+
* @dataProvider provideUStrings
46+
*/
47+
public function testU(UnicodeString $expected, ?string $input)
48+
{
49+
$this->assertEquals($expected, u($input));
50+
}
51+
52+
public function provideUStrings(): array
53+
{
54+
return [
55+
[new UnicodeString(''), ''],
56+
[new UnicodeString(''), null],
57+
[new UnicodeString('foo'), 'foo'],
58+
[new UnicodeString('अनुच्छेद'), 'अनुच्छेद'],
59+
];
60+
}
61+
62+
/**
63+
* @dataProvider provideBStrings
64+
*/
65+
public function testB(ByteString $expected, ?string $input)
66+
{
67+
$this->assertEquals($expected, b($input));
68+
}
69+
70+
public function provideBStrings(): array
71+
{
72+
return [
73+
[new ByteString(''), ''],
74+
[new ByteString(''), null],
75+
[new ByteString("b\x80ar"), "b\x80ar"],
76+
[new ByteString("\xfe\xff"), "\xfe\xff"],
77+
];
78+
}
3979
}

0 commit comments

Comments
 (0)