Skip to content

Commit 3a2153a

Browse files
Fix more tests
1 parent 157d521 commit 3a2153a

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\ShouldNotHappenException;
1111
use PHPStan\Type\Accessory\AccessoryArrayListType;
1212
use PHPStan\Type\ArrayType;
13+
use PHPStan\Type\BenevolentUnionType;
1314
use PHPStan\Type\Constant\ConstantIntegerType;
1415
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1516
use PHPStan\Type\DynamicMethodReturnTypeExtension;
@@ -21,6 +22,7 @@
2122
use PHPStan\Type\Type;
2223
use PHPStan\Type\TypeCombinator;
2324
use PHPStan\Type\TypeTraverser;
25+
use PHPStan\Type\TypeUtils;
2426
use PHPStan\Type\TypeWithClassName;
2527
use PHPStan\Type\VoidType;
2628
use function count;
@@ -156,7 +158,12 @@ private function getMethodReturnTypeForHydrationMode(
156158
case 'getSingleResult':
157159
return $queryResultType;
158160
case 'getOneOrNullResult':
159-
return TypeCombinator::addNull($queryResultType);
161+
$nullableQueryResultType = TypeCombinator::addNull($queryResultType);
162+
if ($queryResultType instanceof BenevolentUnionType) {
163+
$nullableQueryResultType = TypeUtils::toBenevolentUnion($nullableQueryResultType);
164+
}
165+
166+
return $nullableQueryResultType;
160167
case 'toIterable':
161168
return new IterableType(
162169
$queryKeyType->isNull()->yes() ? new IntegerType() : $queryKeyType,

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Doctrine\ORM\Tools\SchemaTool;
1414
use PHPStan\Testing\PHPStanTestCase;
1515
use PHPStan\Type\Accessory\AccessoryNumericStringType;
16-
use PHPStan\Type\BenevolentUnionType;
1716
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1817
use PHPStan\Type\Constant\ConstantIntegerType;
1918
use PHPStan\Type\Constant\ConstantStringType;
@@ -30,7 +29,6 @@
3029
use PHPStan\Type\Type;
3130
use PHPStan\Type\TypeCombinator;
3231
use PHPStan\Type\TypeUtils;
33-
use PHPStan\Type\UnionType;
3432
use PHPStan\Type\VerbosityLevel;
3533
use QueryResult\Entities\Embedded;
3634
use QueryResult\Entities\JoinedChild;
@@ -1258,7 +1256,7 @@ public function getTestData(): iterable
12581256
new ConstantIntegerType(2),
12591257
$this->hasTypedExpressions()
12601258
? $this->uint(true)
1261-
: $this->uintStringified(true)
1259+
: $this->uintStringified(true),
12621260
],
12631261
[
12641262
new ConstantIntegerType(3),
@@ -1585,7 +1583,7 @@ private function unumericStringified(bool $nullable = false): Type
15851583
$types = [
15861584
new FloatType(),
15871585
IntegerRangeType::fromInterval(0, null),
1588-
$this->numericString()
1586+
$this->numericString(),
15891587
];
15901588
if ($nullable) {
15911589
$types[] = new NullType();

tests/Type/Doctrine/data/QueryResult/queryResult.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -384,31 +384,31 @@ public function testReturnTypeOfQueryMethodsWithExplicitSingleScalarHydrationMod
384384
');
385385

386386
assertType(
387-
'int<0, max>|numeric-string',
387+
'(int<0, max>|numeric-string)',
388388
$query->getResult(AbstractQuery::HYDRATE_SINGLE_SCALAR)
389389
);
390390
assertType(
391-
'int<0, max>|numeric-string',
391+
'(int<0, max>|numeric-string)',
392392
$query->getSingleScalarResult()
393393
);
394394
assertType(
395-
'int<0, max>|numeric-string',
395+
'(int<0, max>|numeric-string)',
396396
$query->execute(null, AbstractQuery::HYDRATE_SINGLE_SCALAR)
397397
);
398398
assertType(
399-
'int<0, max>|numeric-string',
399+
'(int<0, max>|numeric-string)',
400400
$query->executeIgnoreQueryCache(null, AbstractQuery::HYDRATE_SINGLE_SCALAR)
401401
);
402402
assertType(
403-
'int<0, max>|numeric-string',
403+
'(int<0, max>|numeric-string)',
404404
$query->executeUsingQueryCache(null, AbstractQuery::HYDRATE_SINGLE_SCALAR)
405405
);
406406
assertType(
407-
'int<0, max>|numeric-string',
407+
'(int<0, max>|numeric-string)',
408408
$query->getSingleResult(AbstractQuery::HYDRATE_SINGLE_SCALAR)
409409
);
410410
assertType(
411-
'int<0, max>|numeric-string|null',
411+
'(int<0, max>|numeric-string|null)',
412412
$query->getOneOrNullResult(AbstractQuery::HYDRATE_SINGLE_SCALAR)
413413
);
414414
}

0 commit comments

Comments
 (0)