Skip to content

QueryResultTypeWalker: fix TypedExpression handling #579

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

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
34 changes: 11 additions & 23 deletions src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace PHPStan\Type\Doctrine\Query;

use BackedEnum;
use Doctrine\DBAL\Types\Types;
use Doctrine\DBAL\Types\StringType as DbalStringType;
use Doctrine\DBAL\Types\Type as DbalType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
Expand Down Expand Up @@ -817,28 +818,15 @@ public function walkSelectExpression($selectExpression): string
$resultAlias = $selectExpression->fieldIdentificationVariable ?? $this->scalarResultCounter++;
$type = $this->unmarshalType($expr->dispatch($this));

if (class_exists(TypedExpression::class) && $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, class_exists over interface is always false.

$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
&& !$expr->getReturnType() instanceof DbalStringType // StringType is no-op, so using TypedExpression with that does nothing
) {
$dbalTypeName = DbalType::getTypeRegistry()->lookupName($expr->getReturnType());
$type = TypeCombinator::intersect( // e.g. count is typed as int, but we infer int<0, max>
$type,
$this->resolveDoctrineType($dbalTypeName, null, TypeCombinator::containsNull($type))
);
} else {
// Expressions default to Doctrine's StringType, whose
// convertToPHPValue() is a no-op. So the actual type depends on
Expand Down
34 changes: 7 additions & 27 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 @@ -613,19 +612,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 Down Expand Up @@ -680,9 +675,7 @@ public function getTestData(): iterable
],
[
new ConstantStringType('count'),
$this->hasTypedExpressions()
? $this->uint()
: $this->uintStringified(),
$this->uint(),
],
]),
'
Expand Down Expand Up @@ -1358,23 +1351,17 @@ 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(),
],
]),
'
Expand Down Expand Up @@ -1566,9 +1553,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 @@ -1690,11 +1675,6 @@ private function unumericStringified(): Type
);
}

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

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