Skip to content

Commit cbbedb2

Browse files
authored
SQL AST: COUNT cannot return null (#584)
1 parent 1540223 commit cbbedb2

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

src/SqlAst/PositiveIntReturnTypeExtension.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPStan\Type\IntegerRangeType;
88
use PHPStan\Type\Type;
9-
use PHPStan\Type\TypeCombinator;
109
use SqlFtw\Sql\Expression\BuiltInFunction;
1110
use SqlFtw\Sql\Expression\FunctionCall;
1211

@@ -31,12 +30,6 @@ public function isFunctionSupported(FunctionCall $expression): bool
3130

3231
public function getReturnType(FunctionCall $expression, QueryScope $scope): Type
3332
{
34-
$positiveInt = IntegerRangeType::fromInterval(0, null);
35-
36-
if ($expression->getFunction()->getName() === BuiltInFunction::COUNT) {
37-
return TypeCombinator::addNull($positiveInt);
38-
}
39-
40-
return $positiveInt;
33+
return IntegerRangeType::fromInterval(0, null);
4134
}
4235
}

tests/sqlAst/data/sql-ast-narrowing.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class Foo
1010
public function count(PDO $pdo): void
1111
{
1212
$stmt = $pdo->query('SELECT count(*) as myemail from ada');
13-
assertType('PDOStatement<array{myemail: int<0, max>|null, 0: int<0, max>|null}>', $stmt);
13+
assertType('PDOStatement<array{myemail: int<0, max>, 0: int<0, max>}>', $stmt);
1414

1515
$stmt = $pdo->query('SELECT count(email) as myemail from ada');
16-
assertType('PDOStatement<array{myemail: int<0, max>|null, 0: int<0, max>|null}>', $stmt);
16+
assertType('PDOStatement<array{myemail: int<0, max>, 0: int<0, max>}>', $stmt);
1717

1818
$stmt = $pdo->query('SELECT count(email) as myemail, count(email) from ada');
19-
assertType('PDOStatement<array{myemail: int<0, max>|null, 0: int<0, max>|null, count(email): int<0, max>|null, 1: int<0, max>|null}>', $stmt);
19+
assertType('PDOStatement<array{myemail: int<0, max>, 0: int<0, max>, count(email): int<0, max>, 1: int<0, max>}>', $stmt);
2020
}
2121

2222
public function coalesce(PDO $pdo): void

tests/stringify/data/ast-narrowed-stringify.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class SqlAstNarrowing
1010
public function count(PDO $pdo): void
1111
{
1212
$stmt = $pdo->query('SELECT count(email) as myemail from ada');
13-
assertType('PDOStatement<array{myemail: numeric-string|null, 0: numeric-string|null}>', $stmt);
13+
assertType('PDOStatement<array{myemail: numeric-string, 0: numeric-string}>', $stmt);
1414

1515
$stmt = $pdo->query('SELECT count(email) as myemail, count(email) from ada');
16-
assertType('PDOStatement<array{myemail: numeric-string|null, 0: numeric-string|null, count(email): numeric-string|null, 1: numeric-string|null}>', $stmt);
16+
assertType('PDOStatement<array{myemail: numeric-string, 0: numeric-string, count(email): numeric-string, 1: numeric-string}>', $stmt);
1717
}
1818
}

0 commit comments

Comments
 (0)