Skip to content

Commit bba9be5

Browse files
committed
Make linter happy.
Fix variable reuse for $wpdb->update()
1 parent 9e4afd9 commit bba9be5

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/Search_Replace_Command.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -529,21 +529,25 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
529529
$count = 0;
530530
$replacer = new \WP_CLI\SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit );
531531

532-
$table_sql = self::esc_sql_ident( $table );
533-
$col_sql = self::esc_sql_ident( $col );
534-
$where = $this->regex ? '' : " WHERE $col_sql" . $wpdb->prepare( ' LIKE BINARY %s', '%' . self::esc_like( $old ) . '%' );
535-
$primary_keys_sql = implode( ',', self::esc_sql_ident( $primary_keys ) );
536-
$order_by_keys = array_map(
532+
$table_sql = self::esc_sql_ident( $table );
533+
$col_sql = self::esc_sql_ident( $col );
534+
$where = $this->regex ? '' : " WHERE $col_sql" . $wpdb->prepare( ' LIKE BINARY %s', '%' . self::esc_like( $old ) . '%' );
535+
$escaped_primary_keys = self::esc_sql_ident( $primary_keys );
536+
$primary_keys_sql = implode( ',', $escaped_primary_keys );
537+
$order_by_keys = array_map(
537538
function( $key ) {
538539
return "{$key} ASC";
539540
},
540-
$primary_keys
541+
$escaped_primary_keys
541542
);
542-
$order_by_sql = "ORDER BY " . implode( ',', self::esc_sql_ident( $order_by_keys ) );
543-
$limit = 1000;
544-
$offset = 0;
545-
546-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
543+
$order_by_sql = 'ORDER BY ' . implode( ',', $order_by_keys );
544+
$limit = 1000;
545+
$offset = 0;
546+
547+
// 2 errors:
548+
// - WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
549+
// - WordPress.CodeAnalysis.AssignmentInCondition -- no reason to do copy-paste for a single valid assignment in while
550+
// phpcs:ignore
547551
while ( $rows = $wpdb->get_results( "SELECT {$primary_keys_sql} FROM {$table_sql} {$where} {$order_by_sql} LIMIT {$limit} OFFSET {$offset}" ) ) {
548552
foreach ( $rows as $keys ) {
549553
$where_sql = '';
@@ -575,14 +579,16 @@ function( $key ) {
575579
if ( $this->dry_run ) {
576580
$count++;
577581
} else {
578-
$where = array();
582+
$update_where = array();
579583
foreach ( (array) $keys as $k => $v ) {
580-
$where[ $k ] = $v;
584+
$update_where[ $k ] = $v;
581585
}
582586

583-
$count += $wpdb->update( $table, array( $col => $value ), $where );
587+
$count += $wpdb->update( $table, array( $col => $value ), $update_where );
584588
}
585589
}
590+
591+
$offset += $limit;
586592
}
587593

588594
if ( $this->verbose && 'table' === $this->format ) {

0 commit comments

Comments
 (0)