WordPress-Coding-Standards: False positive for interpolated table names (WordPress.DB.PreparedSQL.NotPrepared)

Bug Description

Interpolated table names that are not $wpdb properties incorrectly raise WordPress.DB.PreparedSQL.NotPrepared.

What did you expect to happen? What actually happened? Interpolated table names in FROM clauses should be ignored.

Minimal Code Snippet

$table = 'foo';
$id      = 6;
$wpdb->prepare( "SELECT * FROM `{$table}` WHERE id = %d", $id);

Error Code

WordPress.DB.PreparedSQL.NotPrepared

Environment

Question Answer
PHP version 7.2.12
PHP_CodeSniffer version 3.4.0
WPCS version 1.2.1
WPCS install type Composer project local

Tested Against develop branch?

  • I have verified the issue still exists in the develop branch of WPCS.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 15 (5 by maintainers)

Most upvoted comments

@jonshipman is spot on. {$wpdb->prefix}{$table_name} should work.

@jrfnl {$wpdb->prefix}tablename does make the error go away, but referring to the table with a literal in this way only makes sense if you are accessing this table one time in your entire project. Writing tablename in every query where it is needed introduces duplication and makes it difficult to refactor if the tables name ever changes. I’d be surprised if saving custom table names as constants/variables ($wpdb->prefix excluded) was NOT the norm for developers.

Perhaps introducing a new sniff that only whitelists table name variables? Like using the example from @mundschenk-at: ... FROM {$wpdb->prefix}tablename ... could become ... FROM {$wpdb->prefix}{$table_name} ... // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared.TableName where the sniff looks as the variable ceding the $wpdb->prefix variable.

This would allow people to specifically ignore table names while not blanket ignoring the whole prepared statement.

I’d like to suggest closing this issue in favour of encouraging people to start using %i for table names once it is available (likely to be in WP 6.2).

Refs:

@eddr Well, first clue would be to report this to the WooCommerce market place.

Second clue would be that the WPCS support for the %i feature has not been released yet and is only available in the develop branch and I suspect the WooCommerce market place may be using the last stable release.

Either way, this is not something we can help you with as it is not a WPCS problem, but a problem with how the WooCommerce market place uses WPCS.

@jrfnl I’m dealing with queries that are pulling in data from outside of WordPress entirely, in a different database, whose name we’re storing as a class constant.

Why must I instantiate an object and use an object member variable when I’m dealing with an actual constant?

I understand the rationale behind the WPCS sniff - to prevent people from unintentionally introducing SQL injection errors into their code and queries - and, yes, I wrap the queries in phpcs:enable/phpcs:disable for the WordPress.DB.PreparedSQL.NotPrepared sniff. In the end, I’ll say that the benefit that this sniff provides far outweighs the minor inconvenience of having to disable the sniff around a few queries.