From 522656ed2b69d2ba805f632559a7f50ee0c2cdb3 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 4 Nov 2023 00:26:20 +0000 Subject: [PATCH] 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: [https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php 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. Built from https://develop.svn.wordpress.org/trunk@57061 git-svn-id: http://core.svn.wordpress.org/trunk@56572 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/upgrade.php | 76 +++++++++++++++++------------------ wp-includes/kses.php | 4 +- wp-includes/version.php | 2 +- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index 6929570fac..705539571e 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -2910,31 +2910,29 @@ function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.N */ // Extract type, name and columns from the definition. - // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation preg_match( - '/^' - . '(?P' // 1) Type of the index. - . 'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX' - . ')' - . '\s+' // Followed by at least one white space character. - . '(?:' // Name of the index. Optional if type is PRIMARY KEY. - . '`?' // Name can be escaped with a backtick. - . '(?P' // 2) Name of the index. - . '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+' - . ')' - . '`?' // Name can be escaped with a backtick. - . '\s+' // Followed by at least one white space character. - . ')*' - . '\(' // Opening bracket for the columns. - . '(?P' - . '.+?' // 3) Column names, index prefixes, and orders. - . ')' - . '\)' // Closing bracket for the columns. - . '$/im', + '/^ + (?P # 1) Type of the index. + PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX + ) + \s+ # Followed by at least one white space character. + (?: # Name of the index. Optional if type is PRIMARY KEY. + `? # Name can be escaped with a backtick. + (?P # 2) Name of the index. + (?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+ + ) + `? # Name can be escaped with a backtick. + \s+ # Followed by at least one white space character. + )* + \( # Opening bracket for the columns. + (?P + .+? # 3) Column names, index prefixes, and orders. + ) + \) # Closing bracket for the columns. + $/imx', $fld, $index_matches ); - // phpcs:enable // Uppercase the index type and normalize space characters. $index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) ); @@ -2952,29 +2950,27 @@ function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.N // Normalize columns. foreach ( $index_columns as $id => &$index_column ) { // Extract column name and number of indexed characters (sub_part). - // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation preg_match( - '/' - . '`?' // Name can be escaped with a backtick. - . '(?P' // 1) Name of the column. - . '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+' - . ')' - . '`?' // Name can be escaped with a backtick. - . '(?:' // Optional sub part. - . '\s*' // Optional white space character between name and opening bracket. - . '\(' // Opening bracket for the sub part. - . '\s*' // Optional white space character after opening bracket. - . '(?P' - . '\d+' // 2) Number of indexed characters. - . ')' - . '\s*' // Optional white space character before closing bracket. - . '\)' // Closing bracket for the sub part. - . ')?' - . '/', + '/ + `? # Name can be escaped with a backtick. + (?P # 1) Name of the column. + (?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+ + ) + `? # Name can be escaped with a backtick. + (?: # Optional sub part. + \s* # Optional white space character between name and opening bracket. + \( # Opening bracket for the sub part. + \s* # Optional white space character after opening bracket. + (?P + \d+ # 2) Number of indexed characters. + ) + \s* # Optional white space character before closing bracket. + \) # Closing bracket for the sub part. + )? + /x', $index_column, $index_column_matches ); - // phpcs:enable // Escape the column name with backticks. $index_column = '`' . $index_column_matches['column_name'] . '`'; diff --git a/wp-includes/kses.php b/wp-includes/kses.php index 7449d8fb4a..026e004124 100644 --- a/wp-includes/kses.php +++ b/wp-includes/kses.php @@ -1565,8 +1565,8 @@ function wp_kses_hair_parse( $attr ) { * Note: do NOT remove the `x` modifiers as they are essential for the above regex! */ - $validation = "%^($regex)+$%x"; - $extraction = "%$regex%x"; + $validation = "/^($regex)+$/x"; + $extraction = "/$regex/x"; if ( 1 === preg_match( $validation, $attr ) ) { preg_match_all( $extraction, $attr, $attrarr ); diff --git a/wp-includes/version.php b/wp-includes/version.php index d28128cf93..356ed4a332 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57060'; +$wp_version = '6.5-alpha-57061'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.