Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Strict comparison for Str::replaceFirst and Str::is #34670

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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ö'));
Expand Down