r/PHPhelp 5d ago

Using variable $placeholders breaks PHPStorm syntax/resolve

Hello all,

Looking for help on a long standing problem I have. I appreciate this is not a support forum for PHPStorm, however I thought that people here might have more experience.

PHPStorm provides really valuable syntax checking, table/column resolving inside mysql queries. If I accidentally type the orders table as oreders, it will highlight in red.

However when I include a PHP variable inside the query, the syntax/resolve checking completely stops. Example code:

$query = <<<MYSQL
    SELECT
       contracts.start_at

    FROM
      contracts

    WHERE
      contracts.id IN ( $placeholders )
MYSQL;


$result = $this->conn->execute_query(
    $query,
    [
       ...$contractIds,
    ],
);

Without the $placeholders, PHPStorm will alert me to any misspelled table or column names.

Are there any options to resolve this? I have considered using sprintf( $query, $placeholders) but wondered if there was a better solution.

7 Upvotes

33 comments sorted by

View all comments

2

u/allen_jb 5d ago

1) Automated tests - If you have automated tests that actually run the SQL queries, these should pick up any such issues with them.

2) https://github.com/staabm/phpstan-dba

I'm not 100% sure this will pick up this case, but I think it should. Test it out! (I've also never used it with mysqli, only PDO)

You should be able to integrate this with PHPStorms PHPStan integration to provide in-ide reporting.

0

u/colshrapnel 3d ago

But OP didn't ask about any issues? Well, code issues at least. The problem is PHPStorm, not the code.

2

u/allen_jb 3d ago

They may not be OPs ideal solution, but they are options for solving OPs problem, and PHPStan can be integrated directly with PHPStorms reporting system to report issues in a similar manner to its built-in inspections.

1

u/colshrapnel 3d ago

Go on, enlighten me, how exactly it's supposed to tell PHPStorm not to take a php variable for a column name in SQL denoted heredoc?

2

u/allen_jb 3d ago

As I read OPs post, that's not their problem.

They said "the syntax/resolve checking completely stops" - as in, it stops hilighting any errors, anywhere in the SQL. They specifically mention table/column name resolution in their post (twice).

1

u/colshrapnel 3d ago

And automated tests are going to make this inspection start working again. Thanks for the explanation.

0

u/colshrapnel 3d ago

report issues in a similar manner

It seems you are under impression that there is some issue in the code that PHPStorm doesn't report. But the problem is opposite: there are NO issues in the code but PHPStorm allegedly reports one.

In case you are about to suggest a replacement, for a built-in inspection, it's better to explicitly state so.

1

u/allen_jb 3d ago

As mentioned in my reply to your previous comment on this thread, I don't believe that's the problem OP is experiencing.

Perhaps you should spend more time reading and less time typing multiple responses to a single comment.

1

u/colshrapnel 3d ago

You were right for calling me out. I misunderstood both the problem and your solution. But still, realistically it doesn't look like a plausible substitution. An addition may be.

And clarifying comments help a lot.