Skip to content

Commit

Permalink
throw unresolvable query while query resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Mar 7, 2023
1 parent bb9422d commit 452b816
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
16 changes: 9 additions & 7 deletions src/QueryReflection/QueryReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use staabm\PHPStanDba\SchemaReflection\SchemaReflection;
use staabm\PHPStanDba\SqlAst\ParserInference;
use staabm\PHPStanDba\UnresolvableQueryException;
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;

final class QueryReflection
{
Expand Down Expand Up @@ -280,13 +281,7 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable

$queryString = $this->resolveQueryExpr($queryExpr, $scope);
if (null !== $queryString) {
$normalizedQuery = QuerySimulation::stripComments($this->normalizeQueryString($queryString));

// query simulation might lead in a invalid query, skip those
$error = $this->validateQueryString($normalizedQuery);
if ($error === null) {
yield $normalizedQuery;
}
yield QuerySimulation::stripComments($this->normalizeQueryString($queryString));
}
}

Expand Down Expand Up @@ -360,6 +355,13 @@ private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope, bool $res
$leftString = $this->resolveQueryStringExpr($left, $scope);
$rightString = $this->resolveQueryStringExpr($right, $scope);

// queries with a dynamic FROM are not resolvable
if (QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
if (str_ends_with(rtrim($leftString), 'FROM') && is_numeric(trim($rightString, '"\''))) {
throw new UnresolvableQueryDynamicFromException('Seems the query is too dynamic to be resolved by query simulation');
}
}

if (null === $leftString || null === $rightString) {
return null;
}
Expand Down
16 changes: 16 additions & 0 deletions src/UnresolvableQueryDynamicFromException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba;

/**
* @api
*/
final class UnresolvableQueryDynamicFromException extends UnresolvableQueryException
{
public static function getTip(): string
{
return 'Consider to simplify the FROM-clause construction.';
}
}
16 changes: 0 additions & 16 deletions src/UnresolvableQueryInvalidAfterSimulationException.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/rules/UnresolvableQueryMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPStan\Testing\RuleTestCase;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule;
use staabm\PHPStanDba\UnresolvableQueryInvalidAfterSimulationException;
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;
use staabm\PHPStanDba\UnresolvableQueryMixedTypeException;
use staabm\PHPStanDba\UnresolvableQueryStringTypeException;

Expand Down Expand Up @@ -76,7 +76,7 @@ public function testBug548(): void
[
'Unresolvable Query: Seems the query is too dynamic to be resolved by query simulation.',
10,
UnresolvableQueryInvalidAfterSimulationException::getTip(),
UnresolvableQueryDynamicFromException::getTip(),
],
]);
}
Expand All @@ -87,7 +87,7 @@ public function testBug547(): void
[
'Unresolvable Query: Seems the query is too dynamic to be resolved by query simulation.',
10,
UnresolvableQueryInvalidAfterSimulationException::getTip(),
UnresolvableQueryDynamicFromException::getTip(),
],
]);
}
Expand Down

0 comments on commit 452b816

Please sign in to comment.