Skip to content

doctrine dbal: fix error detection in queries without args #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 168 additions & 1 deletion .phpstan-dba.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 25 additions & 13 deletions src/Rules/SyntaxErrorInPreparedStatementMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function checkErrors(CallLike $callLike, Scope $scope): array
{
$args = $callLike->getArgs();

if (\count($args) < 2) {
if (\count($args) < 1) {
return [];
}

Expand All @@ -102,29 +102,41 @@ private function checkErrors(CallLike $callLike, Scope $scope): array
}

$queryReflection = new QueryReflection();
$parameterTypes = $scope->getType($args[1]->value);
try {
$parameters = $queryReflection->resolveParameters($parameterTypes) ?? [];
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip(UnresolvableQueryException::RULE_TIP)->line($callLike->getLine())->build(),
];

$parameters = null;
if (\count($args) > 1) {
$parameterTypes = $scope->getType($args[1]->value);
try {
$parameters = $queryReflection->resolveParameters($parameterTypes) ?? [];
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip(UnresolvableQueryException::RULE_TIP)->line($callLike->getLine())->build(),
];
}
}

if (null === $parameters) {
$queryStrings = $queryReflection->resolveQueryStrings($queryExpr, $scope);
} else {
$queryStrings = $queryReflection->resolvePreparedQueryStrings($queryExpr, $parameterTypes, $scope);
}

$errors = [];
$placeholderValidation = new PlaceholderValidation();
try {
foreach ($queryReflection->resolvePreparedQueryStrings($queryExpr, $parameterTypes, $scope) as $queryString) {
foreach ($queryStrings as $queryString) {
$queryError = $queryReflection->validateQueryString($queryString);
if (null !== $queryError) {
$error = $queryError->asRuleMessage();
$errors[$error] = $error;
}
}

foreach ($placeholderValidation->checkQuery($queryExpr, $scope, $parameters) as $error) {
// make error messages unique
$errors[$error] = $error;
if (null !== $parameters) {
$placeholderValidation = new PlaceholderValidation();
foreach ($placeholderValidation->checkQuery($queryExpr, $scope, $parameters) as $error) {
// make error messages unique
$errors[$error] = $error;
}
}

$ruleErrors = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function testSyntaxErrorInQueryRule(): void
'Query expects placeholder :name, but it is missing from values given.',
307,
],
[
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'gesperrt freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
319,
],
]);
}
}
Loading