Skip to content

QueryResultTypeWalker: fix TypedExpression handling; COUNT(x) is always int #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PHPStan\Type\Doctrine\Query;

use BackedEnum;
use Doctrine\DBAL\Types\Types;
use Doctrine\DBAL\Types\Type as DbalType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
Expand Down Expand Up @@ -361,9 +361,11 @@
case $function instanceof AST\Functions\MaxFunction:
case $function instanceof AST\Functions\MinFunction:
case $function instanceof AST\Functions\SumFunction:
case $function instanceof AST\Functions\CountFunction:
return $function->getSql($this);

case $function instanceof AST\Functions\CountFunction:
return $this->marshalType(IntegerRangeType::fromInterval(0, null));

case $function instanceof AST\Functions\AbsFunction:
$exprType = $this->unmarshalType($this->walkSimpleArithmeticExpression($function->simpleArithmeticExpression));

Expand Down Expand Up @@ -816,28 +818,11 @@
$resultAlias = $selectExpression->fieldIdentificationVariable ?? $this->scalarResultCounter++;
$type = $this->unmarshalType($expr->dispatch($this));

if (class_exists(TypedExpression::class) && $expr instanceof TypedExpression) {
$enforcedType = $this->resolveDoctrineType(Types::INTEGER);
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($enforcedType): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof NullType) {
return $type;
}
if ($enforcedType->accepts($type, true)->yes()) {
return $type;
}
if ($enforcedType instanceof StringType) {
if ($type instanceof IntegerType || $type instanceof FloatType) {
return TypeCombinator::union($type->toString(), $type);
}
if ($type instanceof BooleanType) {
return TypeCombinator::union($type->toInteger()->toString(), $type);
}
}
return $enforcedType;
});
if ($expr instanceof TypedExpression) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was dead block of code as class_exists(TypedExpression::class) is always false.

$type = TypeCombinator::intersect(
$type,
$this->resolveDoctrineType(DbalType::lookupName($expr->getReturnType()), null, TypeCombinator::containsNull($type))

Check failure on line 824 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3)

Call to an undefined static method Doctrine\DBAL\Types\Type::lookupName().
);
} else {
// Expressions default to Doctrine's StringType, whose
// convertToPHPValue() is a no-op. So the actual type depends on
Expand Down
64 changes: 36 additions & 28 deletions tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Query\AST\TypedExpression;
use Doctrine\ORM\Tools\SchemaTool;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
Expand Down Expand Up @@ -611,19 +610,15 @@ public function getTestData(): iterable
],
[
new ConstantIntegerType(3),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
[
new ConstantIntegerType(4),
TypeCombinator::addNull($this->intStringified()),
],
[
new ConstantIntegerType(5),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
[
new ConstantIntegerType(6),
Expand All @@ -645,6 +640,29 @@ public function getTestData(): iterable
',
];

yield 'count' => [
$this->constantArray([
[
new ConstantIntegerType(1),
$this->uint(),
],
[
new ConstantIntegerType(2),
$this->uint(),
],
[
new ConstantIntegerType(3),
$this->uint(),
],
]),
'
SELECT COUNT(m.stringNullColumn),
COUNT(m.stringColumn),
COUNT(m)
FROM QueryResult\Entities\Many m
',
];

yield 'aggregate lowercase' => [
$this->constantArray([
[
Expand Down Expand Up @@ -678,9 +696,7 @@ public function getTestData(): iterable
],
[
new ConstantStringType('count'),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
]),
'
Expand Down Expand Up @@ -1346,29 +1362,28 @@ public function getTestData(): iterable
$this->constantArray([
[
new ConstantIntegerType(1),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
[
new ConstantIntegerType(2),
TypeCombinator::addNull(
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified()
$this->uint()
),
],
[
new ConstantIntegerType(3),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
[
new ConstantIntegerType(4),
$this->uint(),
],
]),
'
SELECT LENGTH(m.stringColumn),
LENGTH(m.stringNullColumn),
LENGTH(\'foo\')
LENGTH(\'foo\'),
LENGTH(COALESCE(m.stringNullColumn, \'\'))
FROM QueryResult\Entities\Many m
',
];
Expand Down Expand Up @@ -1554,9 +1569,7 @@ public function getTestData(): iterable
[new ConstantIntegerType(1), TypeCombinator::addNull($this->numericStringOrInt())],
[
new ConstantIntegerType(2),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
]),
'
Expand Down Expand Up @@ -1678,11 +1691,6 @@ private function unumericStringified(): Type
);
}

private function hasTypedExpressions(): bool
{
return class_exists(TypedExpression::class);
}

/**
* @param array<mixed[]> $arrays
*
Expand Down
Loading