Skip to content

Commit ab5b1cd

Browse files
authored
Fix sql ast type inference on raw-sql expression without aliases (#582)
1 parent 34e3a21 commit ab5b1cd

15 files changed

+40998
-2246
lines changed

.phpstan-dba-pdo-mysql.cache

Lines changed: 545 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"vlucas/phpdotenv": "^5.4"
2626
},
2727
"conflict": {
28-
"sqlftw/sqlftw": "<0.1.6 || >0.1.7"
28+
"sqlftw/sqlftw": "<0.1.10"
2929
},
3030
"autoload": {
3131
"psr-4": {
@@ -81,6 +81,7 @@
8181
"phpstan": [
8282
"phpstan analyse -c phpstan.neon.dist",
8383
"phpstan analyse -c tests/default/config/phpstan.neon.dist",
84+
"phpstan analyse -c tests/sqlAst/config/phpstan.neon.dist",
8485
"phpstan analyse -c tests/rules/config/phpstan.neon.dist",
8586
"phpstan analyse -c tests/stringify/config/phpstan.neon.dist",
8687
"phpstan analyse -c tests/defaultFetchAssoc/config/phpstan.neon.dist",
@@ -93,6 +94,7 @@
9394
],
9495
"phpunit": [
9596
"phpunit -c tests/default/config/phpunit.xml",
97+
"phpunit -c tests/sqlAst/config/phpunit.xml",
9698
"phpunit -c tests/rules/config/phpunit.xml",
9799
"phpunit -c tests/stringify/config/phpunit.xml",
98100
"phpunit -c tests/defaultFetchAssoc/config/phpunit.xml",

src/SqlAst/ParserInference.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public function narrowResultType(string $queryString, ConstantArrayType $resultT
140140
if ($exprName !== null) {
141141
$nameType = new ConstantStringType($exprName);
142142
}
143+
$rawExpressionType = null;
144+
if ($column->getRawExpression() !== null) {
145+
$rawExpressionType = new ConstantStringType($column->getRawExpression());
146+
}
143147
$aliasOffsetType = null;
144148
if (null !== $column->getAlias()) {
145149
$aliasOffsetType = new ConstantStringType($column->getAlias());
@@ -152,6 +156,12 @@ public function narrowResultType(string $queryString, ConstantArrayType $resultT
152156
$valueType = $type;
153157
}
154158

159+
if (null !== $rawExpressionType && $resultType->hasOffsetValueType($rawExpressionType)->yes()) {
160+
$resultType = $resultType->setOffsetValueType(
161+
$rawExpressionType,
162+
$valueType
163+
);
164+
}
155165
if (null !== $aliasOffsetType && $resultType->hasOffsetValueType($aliasOffsetType)->yes()) {
156166
$resultType = $resultType->setOffsetValueType(
157167
$aliasOffsetType,

tests/default/DbaInferenceTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ public function dataFileAsserts(): iterable
6262
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli-union-result.php');
6363
yield from $this->gatherAssertTypes(__DIR__ . '/data/pdo-default-fetch-types.php');
6464
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug372.php');
65-
66-
if (\PHP_VERSION_ID >= 70400 && 'pdo-pgsql' !== getenv('DBA_REFLECTOR')) {
67-
yield from $this->gatherAssertTypes(__DIR__ . '/data/sql-ast-narrowing.php');
68-
}
6965
}
7066

7167
/**

0 commit comments

Comments
 (0)