Skip to content

Commit e6ba740

Browse files
committed
throw unresolvable query while query resolving
1 parent e643e77 commit e6ba740

4 files changed

+28
-26
lines changed

Diff for: src/QueryReflection/QueryReflection.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use staabm\PHPStanDba\SchemaReflection\SchemaReflection;
3535
use staabm\PHPStanDba\SqlAst\ParserInference;
3636
use staabm\PHPStanDba\UnresolvableQueryException;
37+
use staabm\PHPStanDba\UnresolvableQueryDynamicFromException;
3738

3839
final class QueryReflection
3940
{
@@ -286,13 +287,7 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable
286287

287288
$queryString = $this->resolveQueryExpr($queryExpr, $scope);
288289
if (null !== $queryString) {
289-
$normalizedQuery = QuerySimulation::stripComments($this->normalizeQueryString($queryString));
290-
291-
// query simulation might lead in a invalid query, skip those
292-
$error = $this->validateQueryString($normalizedQuery);
293-
if ($error === null) {
294-
yield $normalizedQuery;
295-
}
290+
yield QuerySimulation::stripComments($this->normalizeQueryString($queryString));
296291
}
297292
}
298293

@@ -366,6 +361,13 @@ private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope, bool $res
366361
$leftString = $this->resolveQueryStringExpr($left, $scope);
367362
$rightString = $this->resolveQueryStringExpr($right, $scope);
368363

364+
// queries with a dynamic FROM are not resolvable
365+
if (QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
366+
if (str_ends_with(rtrim($leftString), 'FROM') && is_numeric(trim($rightString, '"\''))) {
367+
throw new UnresolvableQueryDynamicFromException('Seems the query is too dynamic to be resolved by query simulation');
368+
}
369+
}
370+
369371
if (null === $leftString || null === $rightString) {
370372
return null;
371373
}

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)