-3

I have some problem with PhpStorm.

When I have part of query, it tells me, that I have unexpected end of file. For example, in this code:

$data = [
    ['aaa', 'bbb', 'ccc'],
    ['ddd', 'eee', 'fff']
];

$query = "INSERT INTO some_table (col_a,col_b,col_c) VALUES"; // "(" expected, unexpected end of file
$comma = FALSE;
foreach ($data as $row)
{
    if ($comma) $query .= ',';
    $comma = FALSE;
    $query .= '(';
    foreach ($row as $column)
    {
        $query .= quote($column);
    }
    $query .= ')';
}

I've tried to disable all MySQL inspections but error is still shown.

Of course code works fine.

Of course I could rewrite all queries, use prepared statements and other good solutions, but this is project with over 30 MB of php files (not counting external libraries) written in 10 years, so i can't just rewrite everything :) and this is quite annoying problem.

Is there any way to disable this inspection? What inspection is it?

This is not execution problem. Code runs just fine. It's just PhpStorm problem informing me that I have unexpected end of file in this string.

[edit] This is screen with info what is the problem for me :) http://scr.hu/6i5j/3spz6

MatKus
  • 56
  • 11
  • I have phpstorm, and I do not see such issue when I pasted over.. :( – Lionel Chan Apr 13 '16 at 12:25
  • The actual issue is elsewhere in the file. – Jonnix Apr 13 '16 at 12:25
  • 2
    Search for missing `}` , `{` on non-closed strings. From your provided code PHP does not reach unexpected file end – Justinas Apr 13 '16 at 12:25
  • Unexpected end of file is not a code execution issue, it's a code "compilation" issue. You need to inspect the whole source file for any missing quotes, brackets etc. – apokryfos Apr 13 '16 at 12:25
  • 1
    I highly suspect it's due to an opened `"` elsewhere before this PHP file, and when it finally reaches the opening `"` of this line, it thinks it is the end of the long string, hence the "unexpected end of file"? Just a rough guess – Lionel Chan Apr 13 '16 at 12:27
  • This code runs just fine, it's just phpStorm problem showing me there is unexpected end of file at the end of string, but in fact it is rather unexpected end of mySQL query. – MatKus Apr 13 '16 at 12:30
  • It's because PHPStorm is kinda dumb. He is expecting you to add the parenthesis and values after `... VALUES`. It's a unfinished query, it doesn't understand that you will add the values later. You can ignore it, I guess. – Phiter Apr 13 '16 at 12:32
  • @PhiterFernandes no it's not showing as an error in PHPStorm, and the IDE is quite powerful though... – Lionel Chan Apr 13 '16 at 12:33
  • I know, it's not an error, it's a warning. I know the IDE is extremely powerful but it has some bugs. It doesn't know what you will do with the query in the future. It's like "ooh he is going to use this unfinished query let me warn him" – Phiter Apr 13 '16 at 12:34
  • @PhiterFernandes IDE is not dumb in this case. If you configure IDE to use MySQL dialect for your SQL code .. then IDE will check if your SQL is valid. And the SQL code that OP has is not valid for sure (it's unfinished). There are solutions how to bypass it (in other words: tell IDE to ignore this particular place) – LazyOne Apr 13 '16 at 12:36
  • @MateuszKuśnierz Your options: **1)** use `Generic` SQL dialect instead of MySQL (or whatever you have there) -- `Settings/Preferences | Languages & Frameworks | SQL Dialect`. This way you will still have some syntax coloring but no SQL code checks. **2)** Disable SQL Language injections completely (so that your `$query` content will be treated as plain text string) -- `Settings/Preferences | Editor | Language Injections` **3)** Instead of doing #2 globally -- do it for that line only -- for example: `$query = /** @lang Text */"INSERT INTO some_table...` – LazyOne Apr 13 '16 at 12:41
  • @MateuszKuśnierz **4)** Use some placeholder (e.g. `%s`) -- `$query = 'INSERT INTO some_table (col_a,col_b,col_c) VALUES %s';` Then alter your code so that your values will not be added to `$query` straight away but into some intermediate variable first. Then, when you will have your final string, just replace placeholder `%s` with actual values (e.g. using `str_replace()` or `sprintf()` or `vsprintf()`) – LazyOne Apr 13 '16 at 12:48
  • Tkanks @LazyOne . #4 does not work (because %s is not valid part of query). I think i'll stick to solution #3 for now. – MatKus Apr 14 '16 at 10:46
  • @MateuszKuśnierz **#4 works just fine.** Possibly you simple do not have appropriate option/pattern enabled in Settings (open `Settings/Preferences` and look for `Database | User Parameters` – LazyOne Apr 14 '16 at 10:59
  • One year later: You should have found: http://stackoverflow.com/questions/20729064/reference-expected-unexpected-end-of-file-on-phpstorm – Adam P. Apr 19 '17 at 09:56
  • One year later: You should have found: http://stackoverflow.com/questions/20729064/reference-expected-unexpected-end-of-file-on-phpstorm – Adam P. Apr 19 '17 at 09:57

1 Answers1

1

The mix-in languages are configured in

Settings > Editor > Language injections

You should be able to disable all the SQL injections in there.

Markus Müller
  • 2,611
  • 1
  • 17
  • 25