Code Monkey home page Code Monkey logo

Comments (5)

staabm avatar staabm commented on June 18, 2024

thanks for reporting. could you please provide a concrete code sample?

from phpstan-dba.

noemi-salaun avatar noemi-salaun commented on June 18, 2024

I have some code that clone a user with all its data.

  1. So I got an hardcoded list of known tables.
  2. Foreach on the list.
  3. SELECT * FROM each table
  4. Change some of the data (replace the userId used as a foreign key, ...)
  5. INSERT the new data in the same table

For the insert, I use a function that add backquote around column and table name.

/**
 * @param literal-string       $tableExpression
 * @param array<string, mixed> $data
 */
private function insert(string $tableExpression, array $data): void
{
    $backquotedTableExpression = "`$tableExpression`";

    $backquotedData = [];
    foreach ($data as $key => $value) {
        $backquotedData["`$key`"] = $value;
    }

    $this->dbal->insert($backquotedTableExpression, $backquotedData);
}

So my real error message is

Argument#0 expects a literal string, got literal-string&non-falsy-string

But the error stay the same if I remove the backquote and pass $tableExpression directly to dbal insert

Argument#0 expects a literal string, got literal-string

from phpstan-dba.

hemberger avatar hemberger commented on June 18, 2024

What is being checked in the DoctrineKeyValueStyleRule is that the keys of the array passed to Doctrine\DBAL\Connection::insert correspond to columns in the database. In your insert function, PHPStan doesn't know the literal values of the keys of $data, and therefore it cannot check that they are columns in the database.

Similarly for the table name, if PHPStan doesn't know the literal value of the string, it can't check that the table exists in the database.

If you are wrapping calls to Doctrine\DBAL\Connection in your own class, e.g. My\Connection, then you should probably be declaring its function in your PHPStan configuration instead of the Doctrine ones. For example:

services:
    -
        class: staabm\PHPStanDba\Rules\DoctrineKeyValueStyleRule
        tags: [phpstan.rules.rule]
        arguments:
            classMethods:
                - 'My\Connection::insert#1'

(Note: technically this rule checks for a constant string, so maybe the error message should be revised to replace "literal" -> "constant"?)

from phpstan-dba.

staabm avatar staabm commented on June 18, 2024

(Note: technically this rule checks for a constant string, so maybe the error message should be revised to replace "literal" -> "constant"?)

ohh I did not see that. we should not talk about literal strings when we expect constant strings and vice versa.
these are 2 different things - please fix that

from phpstan-dba.

staabm avatar staabm commented on June 18, 2024

@hemberger the code example mentioned in #560 (comment) should be concluded as not analyzable and we should not error (as long as we are not in debug mode)

from phpstan-dba.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.