Make WordPress Core


Ignore:
Timestamp:
06/22/2023 02:55:47 PM (5 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Replace usage of substr() with str_starts_with() and str_ends_with().

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

WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9.

This commit uses str_starts_with() and str_ends_with() in core files where appropriate:

  • $needle === substr( $string, 0, $length ), where $length is the length of $needle, is replaced with str_starts_with( $haystack, $needle ).
  • $needle === substr( $string, $offset ), where $offset is negative and the absolute value of $offset is the length of $needle, is replaced with str_ends_with( $haystack, $needle ).

This aims to make the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988].

Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov.
Fixes #58220.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r55988 r55990  
    12541254            $signature_url = false;
    12551255
    1256             if ( is_string( $url_path ) && ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) ) {
     1256            if ( is_string( $url_path ) && ( str_ends_with( $url_path, '.zip' ) || str_ends_with( $url_path, '.tar.gz' ) ) ) {
    12571257                $signature_url = str_replace( $url_path, $url_path . '.sig', $url );
    12581258            }
     
    16471647        }
    16481648
    1649         if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
     1649        if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
    16501650            continue;
    16511651        }
     
    16601660        $dirname = dirname( $info['name'] );
    16611661
    1662         if ( '/' === substr( $info['name'], -1 ) ) {
     1662        if ( str_ends_with( $info['name'], '/' ) ) {
    16631663            // Directory.
    16641664            $needed_dirs[] = $to . untrailingslashit( $info['name'] );
     
    17271727        }
    17281728
    1729         if ( '/' === substr( $info['name'], -1 ) ) { // Directory.
     1729        if ( str_ends_with( $info['name'], '/' ) ) { // Directory.
    17301730            continue;
    17311731        }
    17321732
    1733         if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
     1733        if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
    17341734            continue;
    17351735        }
     
    18011801    // Determine any children directories needed (From within the archive).
    18021802    foreach ( $archive_files as $file ) {
    1803         if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
     1803        if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
    18041804            continue;
    18051805        }
     
    18671867        }
    18681868
    1869         if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
     1869        if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
    18701870            continue;
    18711871        }
Note: See TracChangeset for help on using the changeset viewer.