Skip to content

Commit fd843ae

Browse files
committed
Address PreparedSQL lints in Db
The WPCS PreparedSQL lints are unable to work with variables. They require either refactoring to run everything inline or ignoring the linter. Refactor the SHOW INDEX query to be all inline. Ignore the linter for other calls since it's impossible to write them inline. I reviewed all callers to confirm that they already prepare the SQL properly.
1 parent 70a6904 commit fd843ae

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/Db.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ public static function ensureIndex(
3434
): bool {
3535
global $wpdb;
3636

37-
$query = $wpdb->prepare(
38-
"SHOW INDEX FROM $table_name WHERE key_name = %s",
39-
$index_name
37+
$indexes = $wpdb->query(
38+
$wpdb->prepare(
39+
'SHOW INDEX FROM %i WHERE key_name = %s',
40+
$table_name,
41+
$index_name,
42+
),
4043
);
41-
$indexes = $wpdb->query( $query );
4244

4345
if ( 0 === $indexes ) {
46+
// Ignore prepare rule because the query has already
47+
// been prepared by the caller.
48+
// phpcs:ignore WordPress.DB.PreparedSQL
4449
$result = $wpdb->query( $create_index_sql );
4550
if ( false === $result ) {
4651
WsLog::l( "Failed to create $index_name index on $table_name." );
@@ -90,6 +95,9 @@ public static function query(
9095
): int|bool {
9196
global $wpdb;
9297

98+
// Ignore prepare rule because the query has already
99+
// been prepared by the caller.
100+
// phpcs:ignore WordPress.DB.PreparedSQL
93101
$result = $wpdb->query( $query );
94102
if ( $result !== false ) {
95103
return $result;

0 commit comments

Comments
 (0)