Skip to content

Commit da099d4

Browse files
Handle aggregate function on litteral
1 parent f3f1dae commit da099d4

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Diff for: src/Type/Doctrine/Query/QueryResultTypeWalker.php

+20
Original file line numberDiff line numberDiff line change
@@ -933,12 +933,32 @@ public function walkAggregateExpression($aggExpression): string
933933
switch ($aggExpression->functionName) {
934934
case 'MAX':
935935
case 'MIN':
936+
$type = $this->unmarshalType(
937+
$this->walkSimpleArithmeticExpression($aggExpression->pathExpression)
938+
);
939+
940+
return $this->marshalType(TypeCombinator::addNull($type));
941+
936942
case 'AVG':
943+
$type = $this->unmarshalType(
944+
$this->walkSimpleArithmeticExpression($aggExpression->pathExpression)
945+
);
946+
947+
if (count($type->getConstantScalarValues()) !== 1) {
948+
$type = TypeUtils::generalizeType($type, GeneralizePrecision::lessSpecific());
949+
}
950+
951+
$type = TypeCombinator::union($type, $type->toFloat());
952+
953+
return $this->marshalType(TypeCombinator::addNull($type));
954+
937955
case 'SUM':
938956
$type = $this->unmarshalType(
939957
$this->walkSimpleArithmeticExpression($aggExpression->pathExpression)
940958
);
941959

960+
$type = TypeUtils::generalizeType($type, GeneralizePrecision::lessSpecific());
961+
942962
return $this->marshalType(TypeCombinator::addNull($type));
943963

944964
case 'COUNT':

Diff for: tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

+78
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\Testing\PHPStanTestCase;
1515
use PHPStan\Type\Accessory\AccessoryNumericStringType;
1616
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
17+
use PHPStan\Type\Constant\ConstantFloatType;
1718
use PHPStan\Type\Constant\ConstantIntegerType;
1819
use PHPStan\Type\Constant\ConstantStringType;
1920
use PHPStan\Type\ConstantTypeHelper;
@@ -681,6 +682,83 @@ public function getTestData(): iterable
681682
',
682683
];
683684

685+
yield 'aggregate on literal' => [
686+
$this->constantArray([
687+
[
688+
new ConstantIntegerType(1),
689+
TypeCombinator::union(
690+
new ConstantStringType('1'),
691+
new ConstantIntegerType(1),
692+
new NullType()
693+
),
694+
],
695+
[
696+
new ConstantIntegerType(2),
697+
TypeCombinator::union(
698+
new ConstantStringType('0'),
699+
new ConstantIntegerType(0),
700+
new ConstantStringType('1'),
701+
new ConstantIntegerType(1),
702+
new NullType()
703+
),
704+
],
705+
[
706+
new ConstantIntegerType(3),
707+
TypeCombinator::union(
708+
new ConstantStringType('1'),
709+
new ConstantIntegerType(1),
710+
new NullType()
711+
),
712+
],
713+
[
714+
new ConstantIntegerType(4),
715+
TypeCombinator::union(
716+
new ConstantStringType('0'),
717+
new ConstantIntegerType(0),
718+
new ConstantStringType('1'),
719+
new ConstantIntegerType(1),
720+
new NullType()
721+
),
722+
],
723+
[
724+
new ConstantIntegerType(5),
725+
TypeCombinator::union(
726+
new ConstantStringType('1'),
727+
new ConstantIntegerType(1),
728+
new ConstantFloatType(1.0),
729+
new NullType()
730+
),
731+
],
732+
[
733+
new ConstantIntegerType(6),
734+
TypeCombinator::union(
735+
$this->intStringified(),
736+
new FloatType(),
737+
new NullType()
738+
),
739+
],
740+
[
741+
new ConstantIntegerType(7),
742+
TypeCombinator::addNull($this->intStringified()),
743+
],
744+
[
745+
new ConstantIntegerType(8),
746+
TypeCombinator::addNull($this->intStringified()),
747+
],
748+
]),
749+
'
750+
SELECT MAX(1),
751+
MAX(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
752+
MIN(1),
753+
MIN(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
754+
AVG(1),
755+
AVG(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
756+
SUM(1),
757+
SUM(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END)
758+
FROM QueryResult\Entities\Many m
759+
',
760+
];
761+
684762
yield 'literal' => [
685763
$this->constantArray([
686764
[

0 commit comments

Comments
 (0)