Skip to content

Support for insert/delete/update wrappers #501

@hemberger

Description

@hemberger

As originally requested in #472 (comment), I was wondering if it would be possible to support Doctrine-style wrappers for write operations.

For example, an insert method is converted into a query as follows:

$conn->insert('user', ['username' => 'jwage']);
// INSERT INTO user (username) VALUES (?) (jwage)

This conversion is described explicitly in the help text for the following methods:

Doctrine\DBAL\Connection::delete
Doctrine\DBAL\Connection::insert
Doctrine\DBAL\Connection::update

If supported, my expectation would be that PHPStan could identify if any of the table or column names did not exist.

In addition to the Doctrine methods, I also have custom wrappers that use a similar interface (i.e. table name as first argument, column name-value array for any additional arguments), but perform a query that Doctrine doesn't have a convenience wrapper for, e.g.

$myconn->replace('user', ['username' => 'jwage']);
// REPLACE INTO user (username) VALUES (?) (jwage)

As such, I would find it very useful to have some generic way to support interfaces like these. I have no idea what would be easiest in terms of implementation, but I was imagining the following possibilities:

  1. A SyntaxError class that could be specified in the PHPStan config, e.g.
    class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryWrapperRule # hopefully w/ a better name!
    tags: [phpstan.rules.rule]
    arguments:
        classMethods:
            - 'My\Connection::replace'
  2. Parameter types similar to class-string that could identify table/column names, e.g.
    /**
     * @param phpstandba-table-string $table
     * @param array<phpstandba-column-string, mixed> $params
     */
    public function replace(string $table, array $params)

I'm happy to help in whatever way I can, though I should note that I'm a complete newbie when it comes to extending PHPStan, so would benefit greatly from some pointers if you want me to submit a PR. :)

Thank you for your time, and for all the work you've done on this amazing project. I'm super excited to finally have some confidence in my SQL queries!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions