Skip to content
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
2 changes: 1 addition & 1 deletion src/QueryReflection/PlaceholderValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function checkQuery(Expr $queryExpr, Scope $scope, array $parameters): it

$queryStrings = [];
$namedPlaceholders = false;
foreach ($queryReflection->resolveQueryStrings($queryExpr, $scope) as $queryString) {
foreach ($queryReflection->resolveQueryStrings($queryExpr, $scope, false) as $queryString) {
$queryStrings[] = $queryString;

if ($queryReflection->containsNamedPlaceholders($queryString, $parameters)) {
Expand Down
9 changes: 7 additions & 2 deletions src/QueryReflection/QueryReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function resolvePreparedQueryString(Expr $queryExpr, Type $parameterTypes
*
* @throws UnresolvableQueryException
*/
public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable
public function resolveQueryStrings(Expr $queryExpr, Scope $scope, bool $resolveNonConstantQueries = true): iterable
{
$type = $scope->getType($queryExpr);

Expand All @@ -287,7 +287,12 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable

// query simulation might lead in a invalid query, skip those
$error = $this->validateQueryString($normalizedQuery);
if ($error === null) {
if (
$error === null
// late abort the query, so we allow the query simulation/validation to throw
// UnresolvableQueryException
&& $resolveNonConstantQueries
) {
yield $normalizedQuery;
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/rules/data/syntax-error-in-prepared-statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,25 @@ public function bug458(Connection $conn)
$table = $this->returnsUnion();
$conn->executeQuery('SELECT * FROM ' . $table . ' LIMIT 1');
}

/**
* @param array<int> $ids
*/
protected function conditionalUnnamedPlaceholder(Connection $connection, array $ids, int $adaid): void
{
$values = [];
$query = 'SELECT email, adaid FROM ada';

$query .= ' AND adaid IN (' . someCall($ids) . ')';
$values = array_merge($values, $ids);

if (rand(0, 1) === 1) {
$query .= ' AND email = ?';
$values[] = '[email protected]';
}

$query .= ' ORDER BY adaid';

$connection->preparedQuery($query, $values);
}
}
Loading