Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPStorm warnings on placeholders in WordPress plugin queries

I have a WordPress plugin that I am editing in PHPStorm. PHPStorm finds syntax errors on queries like this when the SQL dialect is set to MySQL:

$foo = $wpdb->get_var(
           $wpdb->prepare(
               'SELECT `foo` FROM `some_table` WHERE `id` = %d',
               $bar
           )
       );

Specifically, it sees %d and complains with this message:

<expression> expected, got '%'

Of course, %d is a perfectly legitimate placeholder in WordPress queries. Is there a way to configure PHPStorm to accept this? Or do I have to disable all checks on SQL statements, as suggested in this answer?

Note that I am using PHPStorm EAP 8 (138.1751), and the same thing happens with other placeholders like %s.

like image 628
elixenide Avatar asked Aug 30 '14 20:08

elixenide


1 Answers

This is now possible in PHPStorm 8, as explained by this post on the official PHPStorm blog:

Database Language Injection Configuration

To solve it:

  • Go to Tools > Databases
  • Make sure a regex matching the placeholders in question is in the list of custom parameters. By default, the list includes \%\w+, which will match %s, %d, etc.
  • Check the box labeled "Use in other language string literals"

PHPStorm will now correctly recognize placeholders like those used in WordPress.

like image 121
elixenide Avatar answered Sep 22 '22 02:09

elixenide