Skip to content

Commit

Permalink
General: Introduce polyfills for str_ends_with() and `str_starts_wi…
Browse files Browse the repository at this point in the history
…th()` added in PHP 8.0.

PHP 8.0 introduced two new functions: `str_ends_with()` and `str_starts_with()`. These perform a case-sensitive check indicating if the string to search in (haystack) ends or begins with the given substring (needle).

These polyfills make these functios available for use in Core.

Ref:
* PHP RFC https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
* PHP manual `str_ends_with()` https://www.php.net/manual/en/function.str-ends-with.php
* PHP manual `str_starts_with()`  https://www.php.net/manual/en/function.str-starts-with.php

Props costdev, hellofromTonya, pbearne, pbiron.
Fixes #54377.

git-svn-id: https://develop.svn.wordpress.org/trunk@52040 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Nov 8, 2021
1 parent 3cc8f12 commit 1046682
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/wp-includes/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,49 @@ function str_contains( $haystack, $needle ) {
}
}

if ( ! function_exists( 'str_starts_with' ) ) {
/**
* Polyfill for `str_starts_with()` function added in PHP 8.0.
*
* Performs a case-sensitive check indicating if
* the haystack begins with needle.
*
* @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 `$haystack` starts with `$needle`, otherwise false.
*/
function str_starts_with( $haystack, $needle ) {
if ( '' === $needle ) {
return true;
}
return 0 === strpos( $haystack, $needle );
}
}

if ( ! function_exists( 'str_ends_with' ) ) {
/**
* Polyfill for `str_ends_with()` function added in PHP 8.0.
*
* Performs a case-sensitive check indicating if
* the haystack ends with needle.
*
* @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 `$haystack` ends with `$needle`, otherwise false.
*/
function str_ends_with( $haystack, $needle ) {
if ( '' === $haystack && '' !== $needle ) {
return false;
}
$len = strlen( $needle );
return 0 === substr_compare( $haystack, $needle, -$len, $len );
}
}

// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later.
if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
define( 'IMAGETYPE_WEBP', 18 );
Expand Down
115 changes: 115 additions & 0 deletions tests/phpunit/tests/compat/strEndsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* @group compat
*
* @covers ::str_ends_with
*/
class Tests_Compat_StrEndsWith extends WP_UnitTestCase {

/**
* Test that str_ends_with() is always available (either from PHP or WP).
*
* @ticket 54377
*/
public function test_str_ends_with_availability() {
$this->assertTrue( function_exists( 'str_ends_with' ) );
}

/**
* @dataProvider data_str_ends_with
*
* @ticket 54377
*
* @param bool $expected Whether or not `$haystack` is expected to end with `$needle`.
* @param string $haystack The string to search in.
* @param string $needle The substring to search for at the end of `$haystack`.
*/
public function test_str_ends_with( $expected, $haystack, $needle ) {
if ( ! function_exists( 'str_ends_with' ) ) {
$this->markTestSkipped( 'str_ends_with() is not available.' );
} else {
$this->assertSame(
$expected,
str_ends_with( $haystack, $needle )
);
}

}

/**
* Data provider.
*
* @return array[]
*/
public function data_str_ends_with() {
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',
),
'lowercase' => array(
'expected' => true,
'haystack' => 'This is a test',
'needle' => 'test',
),
'uppercase' => array(
'expected' => true,
'haystack' => 'This is a TEST',
'needle' => 'TEST',
),
'first letter uppercase' => array(
'expected' => true,
'haystack' => 'This is a Test',
'needle' => 'Test',
),
'camelCase' => array(
'expected' => true,
'haystack' => 'This is a camelCase',
'needle' => 'camelCase',
),
'null' => array(
'expected' => true,
'haystack' => 'This is a null \x00test',
'needle' => '\x00test',
),
'trademark' => array(
'expected' => true,
'haystack' => 'This is a trademark\x2122',
'needle' => 'trademark\x2122',
),
'not camelCase' => array(
'expected' => false,
'haystack' => 'This is a cammelcase',
'needle' => 'cammelCase',
),
'missing' => array(
'expected' => false,
'haystack' => 'This is a cammelcase',
'needle' => 'cammelCase',
),
'not end' => array(
'expected' => false,
'haystack' => 'This is a test extra',
'needle' => 'test',
),
'extra space' => array(
'expected' => false,
'haystack' => 'This is a test ',
'needle' => 'test',
),

);
}
}
119 changes: 119 additions & 0 deletions tests/phpunit/tests/compat/strStartsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* @group compat
*
* @covers ::str_starts_with
*/
class Tests_Compat_StrStartsWith extends WP_UnitTestCase {

/**
* Test that str_starts_with() is always available (either from PHP or WP).
*
* @ticket 54377
*/
public function test_str_starts_with_availability() {
$this->assertTrue( function_exists( 'str_starts_with' ) );
}

/**
* @dataProvider data_str_starts_with
*
* @ticket 54377
*
* @param bool $expected Whether or not `$haystack` is expected to start with `$needle`.
* @param string $haystack The string to search in.
* @param string $needle The substring to search for at the start of `$haystack`.
*/
public function test_str_starts_with( $expected, $haystack, $needle ) {
if ( ! function_exists( 'str_starts_with' ) ) {
$this->markTestSkipped( 'str_starts_with() is not available.' );
} else {
$this->assertSame(
$expected,
str_starts_with( $haystack, $needle )
);
}

}

/**
* Data provider.
*
* @return array[]
*/
public function data_str_starts_with() {
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',
),
'lowercase' => array(
'expected' => true,
'haystack' => 'this is a test',
'needle' => 'this',
),
'uppercase' => array(
'expected' => true,
'haystack' => 'THIS is a TEST',
'needle' => 'THIS',
),
'first letter uppercase' => array(
'expected' => true,
'haystack' => 'This is a Test',
'needle' => 'This',
),
'case mismatch' => array(
'expected' => false,
'haystack' => 'This is a test',
'needle' => 'this',
),
'camelCase' => array(
'expected' => true,
'haystack' => 'camelCase is the start',
'needle' => 'camelCase',
),
'null' => array(
'expected' => true,
'haystack' => 'This\x00is a null test ',
'needle' => 'This\x00is',
),
'trademark' => array(
'expected' => true,
'haystack' => 'trademark\x2122 is a null test ',
'needle' => 'trademark\x2122',
),
'not camelCase' => array(
'expected' => false,
'haystack' => ' cammelcase is the start',
'needle' => 'cammelCase',
),
'missing' => array(
'expected' => false,
'haystack' => 'This is a test',
'needle' => 'camelCase',
),
'not start' => array(
'expected' => false,
'haystack' => 'This is a test extra',
'needle' => 'test',
),
'extra_space' => array(
'expected' => false,
'haystack' => ' This is a test',
'needle' => 'This',
),
);
}
}

0 comments on commit 1046682

Please sign in to comment.