Make WordPress Core


Ignore:
Timestamp:
11/04/2023 12:24:33 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Remove unnecessary ignore annotations in dbDelta().

It is perfectly possible to write a commented regex with layout for readability by using the x modifier.

As per the manual:

x (PCRE_EXTENDED)

If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include commentary inside complicated patterns.

Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.

Reference: PHP Manual: Pattern Modifiers.

This commit rewrites these two regexes to use the x modifier and gets rid of the unnecessary phpcs:disable comments.

The tests in the tests/phpunit/tests/db/dbDelta.php file cover this change.

Follow-up to [42249].

Props jrf.
See #59650.

File:
1 edited

Legend:

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

    r56664 r57061  
    29112911
    29122912                                        // Extract type, name and columns from the definition.
    2913                                         // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    29142913                                        preg_match(
    2915                                                 '/^'
    2916                                                 .   '(?P<index_type>'             // 1) Type of the index.
    2917                                                 .       'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX'
    2918                                                 .   ')'
    2919                                                 .   '\s+'                         // Followed by at least one white space character.
    2920                                                 .   '(?:'                         // Name of the index. Optional if type is PRIMARY KEY.
    2921                                                 .       '`?'                      // Name can be escaped with a backtick.
    2922                                                 .           '(?P<index_name>'     // 2) Name of the index.
    2923                                                 .               '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
    2924                                                 .           ')'
    2925                                                 .       '`?'                      // Name can be escaped with a backtick.
    2926                                                 .       '\s+'                     // Followed by at least one white space character.
    2927                                                 .   ')*'
    2928                                                 .   '\('                          // Opening bracket for the columns.
    2929                                                 .       '(?P<index_columns>'
    2930                                                 .           '.+?'                 // 3) Column names, index prefixes, and orders.
    2931                                                 .       ')'
    2932                                                 .   '\)'                          // Closing bracket for the columns.
    2933                                                 . '$/im',
     2914                                                '/^
     2915                                                        (?P<index_type>             # 1) Type of the index.
     2916                                                                PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX
     2917                                                        )
     2918                                                        \s+                         # Followed by at least one white space character.
     2919                                                        (?:                         # Name of the index. Optional if type is PRIMARY KEY.
     2920                                                                `?                      # Name can be escaped with a backtick.
     2921                                                                        (?P<index_name>     # 2) Name of the index.
     2922                                                                                (?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+
     2923                                                                        )
     2924                                                                `?                      # Name can be escaped with a backtick.
     2925                                                                \s+                     # Followed by at least one white space character.
     2926                                                        )*
     2927                                                        \(                          # Opening bracket for the columns.
     2928                                                                (?P<index_columns>
     2929                                                                        .+?                 # 3) Column names, index prefixes, and orders.
     2930                                                                )
     2931                                                        \)                          # Closing bracket for the columns.
     2932                                                $/imx',
    29342933                                                $fld,
    29352934                                                $index_matches
    29362935                                        );
    2937                                         // phpcs:enable
    29382936
    29392937                                        // Uppercase the index type and normalize space characters.
     
    29532951                                        foreach ( $index_columns as $id => &$index_column ) {
    29542952                                                // Extract column name and number of indexed characters (sub_part).
    2955                                                 // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    29562953                                                preg_match(
    2957                                                         '/'
    2958                                                         .   '`?'                      // Name can be escaped with a backtick.
    2959                                                         .       '(?P<column_name>'    // 1) Name of the column.
    2960                                                         .           '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
    2961                                                         .       ')'
    2962                                                         .   '`?'                      // Name can be escaped with a backtick.
    2963                                                         .   '(?:'                     // Optional sub part.
    2964                                                         .       '\s*'                 // Optional white space character between name and opening bracket.
    2965                                                         .       '\('                  // Opening bracket for the sub part.
    2966                                                         .           '\s*'             // Optional white space character after opening bracket.
    2967                                                         .           '(?P<sub_part>'
    2968                                                         .               '\d+'         // 2) Number of indexed characters.
    2969                                                         .           ')'
    2970                                                         .           '\s*'             // Optional white space character before closing bracket.
    2971                                                         .       '\)'                  // Closing bracket for the sub part.
    2972                                                         .   ')?'
    2973                                                         . '/',
     2954                                                        '/
     2955                                                                `?                      # Name can be escaped with a backtick.
     2956                                                                        (?P<column_name>    # 1) Name of the column.
     2957                                                                                (?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+
     2958                                                                        )
     2959                                                                `?                      # Name can be escaped with a backtick.
     2960                                                                (?:                     # Optional sub part.
     2961                                                                        \s*                 # Optional white space character between name and opening bracket.
     2962                                                                        \(                  # Opening bracket for the sub part.
     2963                                                                                \s*             # Optional white space character after opening bracket.
     2964                                                                                (?P<sub_part>
     2965                                                                                        \d+         # 2) Number of indexed characters.
     2966                                                                                )
     2967                                                                                \s*             # Optional white space character before closing bracket.
     2968                                                                        \)                  # Closing bracket for the sub part.
     2969                                                                )?
     2970                                                        /x',
    29742971                                                        $index_column,
    29752972                                                        $index_column_matches
    29762973                                                );
    2977                                                 // phpcs:enable
    29782974
    29792975                                                // Escape the column name with backticks.
Note: See TracChangeset for help on using the changeset viewer.