Skip to content

Support \Doctrine\DBAL\Connection->query() syntax error checking - #66

Merged
staabm merged 7 commits into
mainfrom
staabm-patch-1
Jan 17, 2022
Merged

staabm merged 7 commits into
mainfrom
staabm-patch-1

Conversation

@staabm

@staabm staabm commented Jan 10, 2022 •

Copy link
Copy Markdown
Owner

@staabm staabm changed the title Update syntax-error-in-query.php Added use Jan 10, 2022
@staabm staabm changed the title Added use Doctrine Dbal support Jan 10, 2022
@staabm
staabm marked this pull request as draft January 11, 2022 12:56
@staabm staabm changed the title Doctrine Dbal support Support \Doctrine\DBAL\Connection->query() syntax error checking Jan 17, 2022
@staabm
staabm marked this pull request as ready for review January 17, 2022 10:24
@staabm
staabm enabled auto-merge (squash) January 17, 2022 10:24
@staabm
staabm merged commit 2eaf50b into main Jan 17, 2022
@staabm
staabm deleted the staabm-patch-1 branch January 17, 2022 10:25
Comment thread config/dba.neon
- 'PDO::query#0'
- 'PDO::prepare#0'
- 'mysqli::query#0'
- 'Doctrine\DBAL\Connection::query#0'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually deprecated. executeQuery($sql, $params), executeCacheQuery($sql, $params), executeStatement($sql, $params) and prepare($sql) are the current methods available on Doctrine\DBAL\Connection, but executeUpdate, exec and query are still supported and widely used so probably I'd keep them in as well.

There's also fetchAllNumeric, fetchAllNumeric, fetchAllAssociative, fetchAllKeyValue, fetchAllAssociativeIndexed, fetchFirstColumn, iterateNumeric, iterateAssociative, iterateKeyValue, iterateAssociativeIndexed, iterateColumn which are all shorthands for calling these on the returned PDOStatement/Result.

I'm not sure if it's as easy as listing all of them here? I guess it'd do it for the query, but what about checking $params? This is currently only working for direct PDO statements via PdoStatementExecuteMethodRule above is that correct? I guess this is more involved and would require a new rule?

@staabm staabm Jan 17, 2022 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for prepared-statement style methods which take a $params array there is a https://github.com/staabm/phpstan-dba/blob/main/src/Rules/SyntaxErrorInPreparedStatementMethodRule.php rule which can be configured.

see https://github.com/staabm/phpstan-dba#use-syntaxerrorinpreparedstatementmethodrule-for-your-custom-classes

this rule expects that the query is passed as a 1st arg and params as a 2nd. but this seems to fit for executeQuery($sql, $params), executeCacheQuery($sql, $params), executeStatement($sql, $params)

There's also fetchAllNumeric, fetchAllNumeric, fetchAllAssociative, fetchAllKeyValue, fetchAllAssociativeIndexed, fetchFirstColumn, iterateNumeric, iterateAssociative, iterateKeyValue, iterateAssociativeIndexed, iterateColumn which are all shorthands for calling these on the returned PDOStatement/Result.

I think for these methods we need custom return type extensions similar to https://github.com/staabm/phpstan-dba/blob/main/src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php

I'm not sure if it's as easy as listing all of them here?

listing these methods in the config at least enables the syntax error checking parts.
to make the type inference work, we need the extensions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for these methods we need custom return type extensions similar to https://github.com/staabm/phpstan-dba/blob/main/src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php

Yes absolutely for the return types, I was more concerned about getting the SQL/params validated everywhere. I think these could also be added to SyntaxErrorInPreparedStatementMethodRule.php then? All of them have the same basic arg signature.

@staabm staabm Jan 17, 2022 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes absolutely for the return types, I was more concerned about getting the SQL/params validated everywhere. I think these could also be added to SyntaxErrorInPreparedStatementMethodRule.php then? All of them have the same basic arg signature.

exactly. just add the rule to the dba.neon config and list them.

the rule is not yet listed in dba.neon, as there is not yet a builtin method for this rule.
I am using it in a closed project already though for a different api. so it should work.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #116 for syntax error detection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants