mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
General: Introduce polyfill for str_contains() added in PHP 8.0.
PHP 8.0 introduced a new function: `str_contains()`. It performs a case-sensitive check indicating if given substring (needle) is contained in the string to search in (haystack). This polyfill makes this function available for use in Core. Ref: * PHP RFC https://wiki.php.net/rfc/str_contains * PHP manual https://www.php.net/manual/en/function.str-contains.php Props ayeshrajans, costdev, desrosj, hellofromTonya, knutsp, pbearne. Fixes #49652. git-svn-id: https://develop.svn.wordpress.org/trunk@52039 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
b21d748b41
commit
3cc8f1237a
@ -417,6 +417,24 @@ if ( ! function_exists( 'array_key_last' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'str_contains' ) ) {
|
||||
/**
|
||||
* Polyfill for `str_contains()` function added in PHP 8.0.
|
||||
*
|
||||
* Performs a case-sensitive check indicating if needle is
|
||||
* contained in haystack.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param string $haystack The string to search in.
|
||||
* @param string $needle The substring to search for in the haystack.
|
||||
* @return bool True if `$needle` is in `$haystack`, otherwise false.
|
||||
*/
|
||||
function str_contains( $haystack, $needle ) {
|
||||
return ( '' === $needle || false !== strpos( $haystack, $needle ) );
|
||||
}
|
||||
}
|
||||
|
||||
// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later.
|
||||
if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
|
||||
define( 'IMAGETYPE_WEBP', 18 );
|
||||
|
||||
104
tests/phpunit/tests/compat/strContains.php
Normal file
104
tests/phpunit/tests/compat/strContains.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group compat
|
||||
*
|
||||
* @covers ::str_contains
|
||||
*/
|
||||
class Tests_Compat_strContains extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Test that str_contains() is always available (either from PHP or WP).
|
||||
*
|
||||
* @ticket 49652
|
||||
*/
|
||||
public function test_is_str_contains_availability() {
|
||||
$this->assertTrue( function_exists( 'str_contains' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_str_contains
|
||||
*
|
||||
* @ticket 49652
|
||||
*
|
||||
* @param bool $expected Whether or not `$haystack` is expected to contain `$needle`.
|
||||
* @param string $haystack The string to search in.
|
||||
* @param string $needle The substring to search for in `$haystack`.
|
||||
*/
|
||||
public function test_str_contains( $expected, $haystack, $needle ) {
|
||||
if ( ! function_exists( 'str_contains' ) ) {
|
||||
$this->markTestSkipped( 'str_contains() is not available.' );
|
||||
} else {
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
str_contains( $haystack, $needle )
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data_str_contains() {
|
||||
return array(
|
||||
'empty needle' => array(
|
||||
'expected' => true,
|
||||
'haystack' => 'This is a Test',
|
||||
'needle' => '',
|
||||
),
|
||||
'empty haystack and needle' => array(
|
||||
'expected' => true,
|
||||
'haystack' => '',
|
||||
'needle' => '',
|
||||
),
|
||||
'empty haystack' => array(
|
||||
'expected' => false,
|
||||
'haystack' => '',
|
||||
'needle' => 'test',
|
||||
),
|
||||
'start of string' => array(
|
||||
'expected' => true,
|
||||
'haystack' => 'This is a Test',
|
||||
'needle' => 'This',
|
||||
),
|
||||
'middle of string' => array(
|
||||
'expected' => true,
|
||||
'haystack' => 'The needle in middle of string.',
|
||||
'needle' => 'middle',
|
||||
),
|
||||
'end of string' => array(
|
||||
'expected' => true,
|
||||
'string' => 'The needle is at end.',
|
||||
'needle' => 'end',
|
||||
),
|
||||
'lowercase' => array(
|
||||
'expected' => true,
|
||||
'string' => 'This is a test',
|
||||
'needle' => 'test',
|
||||
),
|
||||
'uppercase' => array(
|
||||
'expected' => true,
|
||||
'string' => 'This is a TEST',
|
||||
'needle' => 'TEST',
|
||||
),
|
||||
'camelCase' => array(
|
||||
'expected' => true,
|
||||
'string' => 'String contains camelCase.',
|
||||
'needle' => 'camelCase',
|
||||
),
|
||||
'with hyphen' => array(
|
||||
'expected' => true,
|
||||
'string' => 'String contains foo-bar needle.',
|
||||
'needle' => 'foo-bar',
|
||||
),
|
||||
'missing' => array(
|
||||
'expected' => false,
|
||||
'haystack' => 'This is a camelcase',
|
||||
'needle' => 'camelCase',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user