Skip to content

Commit

Permalink
use strpos rather than polyfill
Browse files Browse the repository at this point in the history
! use strpos rather than polyfill
- remove BaseStringHelper::contains
  • Loading branch information
chriscpty committed Oct 22, 2024
1 parent 7538551 commit 4ed589f
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 35 deletions.
1 change: 0 additions & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Yii Framework 2 Change Log
- Bug #20256: Add support for dropping views in MSSQL server when running migrate/fresh (ambrozt)
- Enh #20248: Add support for attaching behaviors in configurations with Closure (timkelty)
- Enh #20268: Minor optimisation in `\yii\helpers\BaseArrayHelper::map` (chriscpty)
- Enh #20268: Add `\yii\helpers\BaseStringHelper::contains()` as a polyfill for `str_contains` (chriscpty)

2.0.51 July 18, 2024
--------------------
Expand Down
2 changes: 1 addition & 1 deletion framework/helpers/BaseArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public static function getColumn($array, $name, $keepKeys = true)
*/
public static function map($array, $from, $to, $group = null)
{
if (is_string($from) && is_string($to) && $group === null && !\yii\helpers\StringHelper::contains($from, '.') && !\yii\helpers\StringHelper::contains($to, '.')) {
if (is_string($from) && is_string($to) && $group === null && strpos($from, '.') === false && strpos($to, '.') === false) {
return array_column($array, $to, $from);
}
$result = [];
Expand Down
24 changes: 0 additions & 24 deletions framework/helpers/BaseStringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,30 +226,6 @@ protected static function truncateHtml($string, $count, $suffix, $encoding = fal
return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
}

/**
* @param string $string The input string.
* @param string $needle Part to search inside $string
* @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must
* exactly match the starting of the string in order to get a true value.
* @return bool
*/
public static function contains($string, $needle, $caseSensitive = true)
{
$string = (string)$string;
$needle = (string)$needle;

if ($caseSensitive) {
// can be replaced with just the str_contains call when minimum supported PHP version is raised to 8.0 or higher
if (function_exists('str_contains')) {
return str_contains($string, $needle);
}
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
return mb_strpos($string, $needle, 0, $encoding) !== false;
}
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
return mb_stripos($string, $needle, 0, $encoding) !== false;
}

/**
* Check if given string starts with specified substring. Binary and multibyte safe.
*
Expand Down
9 changes: 0 additions & 9 deletions tests/framework/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,6 @@ public function testTruncateWords()
$this->assertEquals('<span><img src="image.png" />This is a test</span><strong> for</strong>...', StringHelper::truncateWords('<span><img src="image.png" />This is a test</span><strong> for a sentance</strong>', 5, '...', true));
}

public function testContains()
{
$this->assertEquals(true, StringHelper::contains('Test', 'st'));
$this->assertEquals(false, StringHelper::contains('Test', 'ts'));
$this->assertEquals(false, StringHelper::contains('Test', 'St'));
$this->assertEquals(true, StringHelper::contains('Test', 'St', false));
$this->assertEquals(false, StringHelper::contains('Test', 'Ste', false));
}

/**
* @dataProvider providerStartsWith
* @param bool $result
Expand Down

0 comments on commit 4ed589f

Please sign in to comment.