Skip to content

Commit 782f7d6

Browse files
committed
throw unresolvable query while query resolving
1 parent 8734052 commit 782f7d6

4 files changed

+28
-26
lines changed

Diff for: src/QueryReflection/QueryReflection.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use staabm\PHPStanDba\SchemaReflection\SchemaReflection;
3333
use staabm\PHPStanDba\SqlAst\ParserInference;
3434
use staabm\PHPStanDba\UnresolvableQueryException;
35+
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;
3536

3637
final class QueryReflection
3738
{
@@ -291,13 +292,7 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable
291292

292293
$queryString = $this->resolveQueryExpr($queryExpr, $scope);
293294
if (null !== $queryString) {
294-
$normalizedQuery = QuerySimulation::stripComments($this->normalizeQueryString($queryString));
295-
296-
// query simulation might lead in a invalid query, skip those
297-
$error = $this->validateQueryString($normalizedQuery);
298-
if ($error === null) {
299-
yield $normalizedQuery;
300-
}
295+
yield QuerySimulation::stripComments($this->normalizeQueryString($queryString));
301296
}
302297
}
303298

@@ -371,6 +366,13 @@ private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope, bool $res
371366
$leftString = $this->resolveQueryStringExpr($left, $scope);
372367
$rightString = $this->resolveQueryStringExpr($right, $scope);
373368

369+
// queries with a dynamic FROM are not resolvable
370+
if (QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
371+
if (str_ends_with(rtrim($leftString), 'FROM') && is_numeric(trim($rightString, '"\''))) {
372+
throw new UnresolvableQueryDynamicFromException('Seems the query is too dynamic to be resolved by query simulation');
373+
}
374+
}
375+
374376
if (null === $leftString || null === $rightString) {
375377
return null;
376378
}

Diff for: src/UnresolvableQueryDynamicFromException.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace staabm\PHPStanDba;
6+
7+
/**
8+
* @api
9+
*/
10+
final class UnresolvableQueryDynamicFromException extends UnresolvableQueryException
11+
{
12+
public static function getTip(): string
13+
{
14+
return 'Consider to simplify the FROM-clause construction.';
15+
}
16+
}

Diff for: src/UnresolvableQueryInvalidAfterSimulationException.php

-16
This file was deleted.

Diff for: tests/rules/UnresolvableQueryMethodRuleTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPStan\Testing\RuleTestCase;
99
use staabm\PHPStanDba\QueryReflection\QueryReflection;
1010
use staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule;
11-
use staabm\PHPStanDba\UnresolvableQueryInvalidAfterSimulationException;
11+
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;
1212
use staabm\PHPStanDba\UnresolvableQueryMixedTypeException;
1313
use staabm\PHPStanDba\UnresolvableQueryStringTypeException;
1414

@@ -76,7 +76,7 @@ public function testBug548(): void
7676
[
7777
'Unresolvable Query: Seems the query is too dynamic to be resolved by query simulation.',
7878
10,
79-
UnresolvableQueryInvalidAfterSimulationException::getTip(),
79+
UnresolvableQueryDynamicFromException::getTip(),
8080
],
8181
]);
8282
}
@@ -87,7 +87,7 @@ public function testBug547(): void
8787
[
8888
'Unresolvable Query: Seems the query is too dynamic to be resolved by query simulation.',
8989
10,
90-
UnresolvableQueryInvalidAfterSimulationException::getTip(),
90+
UnresolvableQueryDynamicFromException::getTip(),
9191
],
9292
]);
9393
}

0 commit comments

Comments
 (0)