Skip to content

Commit 42c4b04

Browse files
Handle aggregate function on litteral
1 parent f3f1dae commit 42c4b04

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-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(), $type->toFloat()->toString());
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

+79
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,84 @@ 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 ConstantStringType('1.0'),
729+
new ConstantFloatType(1.0),
730+
new NullType()
731+
),
732+
],
733+
[
734+
new ConstantIntegerType(6),
735+
TypeCombinator::union(
736+
$this->intStringified(),
737+
new FloatType(),
738+
new NullType()
739+
),
740+
],
741+
[
742+
new ConstantIntegerType(7),
743+
TypeCombinator::addNull($this->intStringified()),
744+
],
745+
[
746+
new ConstantIntegerType(8),
747+
TypeCombinator::addNull($this->intStringified()),
748+
],
749+
]),
750+
'
751+
SELECT MAX(1),
752+
MAX(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
753+
MIN(1),
754+
MIN(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
755+
AVG(1),
756+
AVG(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END),
757+
SUM(1),
758+
SUM(CASE WHEN m.intColumn = 0 THEN 1 ELSE 0 END)
759+
FROM QueryResult\Entities\Many m
760+
',
761+
];
762+
684763
yield 'literal' => [
685764
$this->constantArray([
686765
[

0 commit comments

Comments
 (0)