Home > Software design >  PhpStorm does not suggest table name in UPDATE query inside a PHP string
PhpStorm does not suggest table name in UPDATE query inside a PHP string

Time:10-21

I'm using PhpStorm 2022.2.3

Build #PS-222.4345.15, built on October 5, 2022
Runtime version: 17.0.4.1 7-b469.62 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.15.0-50-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 4048M
Cores: 8
Registry:
    run.processes.with.pty=TRUE

Non-Bundled Plugins:
    de.espend.idea.php.annotation (8.2.3)
    fr.adrienbrault.idea.symfony2plugin (2022.1.234)

Inside a PHP file, it does not suggest table names for UPDATE query, but instead it does for INSERT INTO query.

enter image description here

Then, Ctrl Space

enter image description here

CodePudding user response:

That's because it's NOT detected as SQL yet -- not enough context info to treat the string context as SQL.

Having INSERT INTO at the start of a string is good enough (it's quite unique combo at the start of the string) but UPDATE is not -- too many possible false positives. For example, I do not see why would I need to see a table name completion/string treated as SQL statement if I need to write "Update linked records" or a similar text that starts with UPDATE word.

Possible solutions:

  1. Use Intentions / Quick Fix menu (Alt Enter on most keymaps or using a mouse on a light bulb icon) and choose Inject language or reference and then select the desired SQL there. This will temporarily inject SQL (lasts while the file is opened). Once the string will have enough context the permanent injection rule will naturally take over.

    enter image description here

  2. Force-inject SQL with a comment (just before the string):

    $sql = /** @lang SQL */"UPDATE ";
    

    enter image description here

  3. Use HEREDOC/NOWDOC with specific SQL label -- will utilize the same forced injection (pros: multiline text, looks natural).

    $sql = <<<SQL
    UPDATE 
    SQL;
    

    enter image description here

  4. If you want that ALL strings that start with UPDATE (mind -- it's case insensitive) to have SQL auto-injected and you do not care about any possible false positive -- go and change (or better: make a copy so you do not screw up the original one) the Language Injection rule that is responsible for PHP.

    Settings (Preferences on macOS) | Editor | Language Injections -- look at the following rule:

    enter image description here

    NOTE: the syntax is RegEx alike but it's not RegEx.

  • Related