Skip to content

Commit 0ac7b38

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@57061 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 75796f5 commit 0ac7b38

2 files changed

Lines changed: 38 additions & 42 deletions

File tree

‎src/wp-admin/includes/upgrade.php‎

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,31 +2910,29 @@ function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.N
29102910
*/
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.
29402938
$index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) );
@@ -2952,29 +2950,27 @@ function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.N
29522950
// Normalize columns.
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.
29802976
$index_column = '`' . $index_column_matches['column_name'] . '`';

‎src/wp-includes/kses.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,8 @@ function wp_kses_hair_parse( $attr ) {
15651565
* Note: do NOT remove the `x` modifiers as they are essential for the above regex!
15661566
*/
15671567

1568-
$validation = "%^($regex)+$%x";
1569-
$extraction = "%$regex%x";
1568+
$validation = "/^($regex)+$/x";
1569+
$extraction = "/$regex/x";
15701570

15711571
if ( 1 === preg_match( $validation, $attr ) ) {
15721572
preg_match_all( $extraction, $attr, $attrarr );

0 commit comments

Comments
 (0)