Skip to content

Commit

Permalink
[9.x] Strict comparison for Str::replaceFirst and Str::is (#34670)
Browse files Browse the repository at this point in the history
* strict types for replaceFirst

* add strict str_is

* Update Str.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
fey and taylorotwell authored Oct 5, 2020
1 parent 28e97da commit 7713cb7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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

0 comments on commit 7713cb7

Please sign in to comment.