From 7713cb74a8127598560fb60f3c982f3b7a953738 Mon Sep 17 00:00:00 2001 From: Nikolay Gagarinov Date: Mon, 5 Oct 2020 17:51:27 +0500 Subject: [PATCH] [9.x] Strict comparison for Str::replaceFirst and Str::is (#34670) * strict types for replaceFirst * add strict str_is * Update Str.php Co-authored-by: Taylor Otwell --- src/Illuminate/Support/Str.php | 6 ++++-- tests/Support/SupportStrTest.php | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 59158f4c6ee7..86f0537139b5 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -251,7 +251,7 @@ public static function is($pattern, $value) // If the given value is an exact match we can of course return true right // from the beginning. Otherwise, we will translate asterisks and do an // actual pattern match against the two strings to see if they match. - if ($pattern == $value) { + if ($pattern === $value) { return true; } @@ -501,7 +501,9 @@ public static function replaceArray($search, array $replace, $subject) */ public static function replaceFirst($search, $replace, $subject) { - if ($search == '') { + $search = (string) $search; + + if ($search === '') { return $subject; } diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 87bc3c0956c4..c39a65ea4ab9 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -264,6 +264,10 @@ public function testIs() // empty patterns $this->assertFalse(Str::is([], 'test')); + + $this->assertFalse(Str::is('', 0)); + $this->assertFalse(Str::is([null], 0)); + $this->assertTrue(Str::is([null], null)); } /** @@ -348,6 +352,7 @@ public function testReplaceFirst() $this->assertSame('foo foobar', Str::replaceFirst('bar', '', 'foobar foobar')); $this->assertSame('foobar foobar', Str::replaceFirst('xxx', 'yyy', 'foobar foobar')); $this->assertSame('foobar foobar', Str::replaceFirst('', 'yyy', 'foobar foobar')); + $this->assertSame('1', Str::replaceFirst(0, '1', '0')); // Test for multibyte string support $this->assertSame('Jxxxnköping Malmö', Str::replaceFirst('ö', 'xxx', 'Jönköping Malmö')); $this->assertSame('Jönköping Malmö', Str::replaceFirst('', 'yyy', 'Jönköping Malmö'));