Skip to content

Commit a92644b

Browse files
authored
[11.x] Handle Null Check in Str::startsWith and Str::endsWith (#54520)
* [11.x] Handle Deprecated Null Check in str_starts_with and str_ends_with * Revert "[11.x] Handle Deprecated Null Check in str_starts_with and str_ends_with" This reverts commit 946325f. * [11.x] Fix IDE Auto Styling * [11.x] Fix Styling
1 parent b544b83 commit a92644b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ public static function endsWith($haystack, $needles)
379379
$needles = (array) $needles;
380380
}
381381

382+
if (is_null($haystack)) {
383+
return false;
384+
}
385+
382386
foreach ($needles as $needle) {
383387
if ((string) $needle !== '' && str_ends_with($haystack, $needle)) {
384388
return true;
@@ -1588,6 +1592,10 @@ public static function startsWith($haystack, $needles)
15881592
$needles = [$needles];
15891593
}
15901594

1595+
if (is_null($haystack)) {
1596+
return false;
1597+
}
1598+
15911599
foreach ($needles as $needle) {
15921600
if ((string) $needle !== '' && str_starts_with($haystack, $needle)) {
15931601
return true;

tests/Support/SupportStrTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public function testStartsWith()
175175
$this->assertTrue(Str::startsWith(7.123, '7'));
176176
$this->assertTrue(Str::startsWith(7.123, '7.12'));
177177
$this->assertFalse(Str::startsWith(7.123, '7.13'));
178+
$this->assertFalse(Str::startsWith(null, 'Marc'));
178179
// Test for multibyte string support
179180
$this->assertTrue(Str::startsWith('Jönköping', ''));
180181
$this->assertTrue(Str::startsWith('Malmö', 'Malmö'));
@@ -207,6 +208,7 @@ public function testEndsWith()
207208
$this->assertTrue(Str::endsWith(0.27, '7'));
208209
$this->assertTrue(Str::endsWith(0.27, '0.27'));
209210
$this->assertFalse(Str::endsWith(0.27, '8'));
211+
$this->assertFalse(Str::endsWith(null, 'Marc'));
210212
// Test for multibyte string support
211213
$this->assertTrue(Str::endsWith('Jönköping', 'öping'));
212214
$this->assertTrue(Str::endsWith('Malmö', ''));

0 commit comments

Comments
 (0)