From b8bdf4e0c9f56ef7f05d764891f8b576a8dfc1a6 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sun, 5 Jul 2020 04:34:51 +0200 Subject: [PATCH] Make Str::endsWith return false if both haystack and needle are empty strings --- src/Illuminate/Support/Str.php | 2 +- tests/Support/SupportStrTest.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 2dbceab08a28..76dfa9d8852c 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -210,7 +210,7 @@ public static function containsAll($haystack, array $needles) public static function endsWith($haystack, $needles) { foreach ((array) $needles as $needle) { - if (substr($haystack, -strlen($needle)) === (string) $needle) { + if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { return true; } } diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 6f3273e50508..1bbcb4f54817 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -60,6 +60,7 @@ public function testStartsWith() $this->assertTrue(Str::startsWith('0123', 0)); $this->assertFalse(Str::startsWith('jason', 'J')); $this->assertFalse(Str::startsWith('jason', '')); + $this->assertFalse(Str::startsWith('', '')); $this->assertFalse(Str::startsWith('7', ' 7')); $this->assertTrue(Str::startsWith('7a', '7')); $this->assertTrue(Str::startsWith('7a', 7)); @@ -87,6 +88,7 @@ public function testEndsWith() $this->assertFalse(Str::endsWith('jason', 'no')); $this->assertFalse(Str::endsWith('jason', ['no'])); $this->assertFalse(Str::endsWith('jason', '')); + $this->assertFalse(Str::endsWith('', '')); $this->assertFalse(Str::endsWith('jason', [null])); $this->assertFalse(Str::endsWith('jason', null)); $this->assertFalse(Str::endsWith('jason', 'N')); @@ -183,6 +185,7 @@ public function testStrContains() $this->assertFalse(Str::contains('taylor', 'xxx')); $this->assertFalse(Str::contains('taylor', ['xxx'])); $this->assertFalse(Str::contains('taylor', '')); + $this->assertFalse(Str::contains('', '')); } public function testStrContainsAll()