staabm / phpstan-dba

PHPStan based SQL static analysis and type inference for the database access layer
https://staabm.github.io/archive.html#phpstan-dba
MIT License
258 stars 17 forks source link

Prevent throwing PHPStan internal error when analysing dynamic query with query plans #442

Closed noemi-salaun closed 2 years ago

noemi-salaun commented 2 years ago

When using analyzeQueryPlans with dynamic query, I get a PHPStan inernal error

Internal error: Internal error: Incorrect table name '' in file foo.php

/** @var string $tableExpression */
$snapshotRows = $this->dbal->fetchAllAssociative("SELECT * FROM `$tableExpression`");
staabm commented 2 years ago

thx for reporting.

the problem is that the query-inference cannot work with queries which don't contain a table-reference (or a variable table-reference) in FROM.

we should make sure this doesn't error.. but we cannot do type inference/error checking for those cases.

noemi-salaun commented 2 years ago

Yes of course, I did not expect the type checking to work on this case, just not to fatal error

staabm commented 2 years ago

do you have a full stacktrace to ease debugging?

staabm commented 2 years ago

I was able to isolate and repro the error on mysqli.

next time please provide a proper backtrace and information about used reflector and phpstandba-bootstrap settings. at best a reproducing repository or unit test would.

thanks for reporting

noemi-salaun commented 2 years ago

To be honest, I thought the error would be easier to deal with. I could have dug deeper and provided more specific information, but you solved the problem too quickly to give me time to react after your last message.

Thank you for your work :heart:

staabm commented 2 years ago

no worries. I was kind of suprised, that it was so hard to reproduce ;). but after a few hours fiddling it worked out

noemi-salaun commented 2 years ago

@staabm After updating to v0.2.50 I get a query error even without enabling the debug mode.

Query error: Incorrect table name '' (1103).

$rows = $this->dbal->fetchAllAssociative("SELECT * FROM `$tableExpression`");
staabm commented 2 years ago

@noemi-salaun thx for reporting. fixed with https://github.com/staabm/phpstan-dba/pull/456