Skip to content

Commit 2ef94e1

Browse files
Fix more tests
1 parent 4ac5a29 commit 2ef94e1

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

Diff for: 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,

Diff for: 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;
@@ -1257,7 +1255,7 @@ public function getTestData(): iterable
12571255
new ConstantIntegerType(2),
12581256
$this->hasTypedExpressions()
12591257
? $this->uint(true)
1260-
: $this->uintStringified(true)
1258+
: $this->uintStringified(true),
12611259
],
12621260
[
12631261
new ConstantIntegerType(3),
@@ -1584,7 +1582,7 @@ private function unumericStringified(bool $nullable = false): Type
15841582
$types = [
15851583
new FloatType(),
15861584
IntegerRangeType::fromInterval(0, null),
1587-
$this->numericString()
1585+
$this->numericString(),
15881586
];
15891587
if ($nullable) {
15901588
$types[] = new NullType();

Diff for: tests/Type/Doctrine/data/QueryResult/queryResult.php

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

385385
assertType(
386-
'int<0, max>|numeric-string',
386+
'(int<0, max>|numeric-string)',
387387
$query->getResult(AbstractQuery::HYDRATE_SINGLE_SCALAR)
388388
);
389389
assertType(
390-
'int<0, max>|numeric-string',
390+
'(int<0, max>|numeric-string)',
391391
$query->getSingleScalarResult()
392392
);
393393
assertType(
394-
'int<0, max>|numeric-string',
394+
'(int<0, max>|numeric-string)',
395395
$query->execute(null, AbstractQuery::HYDRATE_SINGLE_SCALAR)
396396
);
397397
assertType(
398-
'int<0, max>|numeric-string',
398+
'(int<0, max>|numeric-string)',
399399
$query->executeIgnoreQueryCache(null, AbstractQuery::HYDRATE_SINGLE_SCALAR)
400400
);
401401
assertType(
402-
'int<0, max>|numeric-string',
402+
'(int<0, max>|numeric-string)',
403403
$query->executeUsingQueryCache(null, AbstractQuery::HYDRATE_SINGLE_SCALAR)
404404
);
405405
assertType(
406-
'int<0, max>|numeric-string',
406+
'(int<0, max>|numeric-string)',
407407
$query->getSingleResult(AbstractQuery::HYDRATE_SINGLE_SCALAR)
408408
);
409409
assertType(
410-
'int<0, max>|numeric-string|null',
410+
'(int<0, max>|numeric-string|null)',
411411
$query->getOneOrNullResult(AbstractQuery::HYDRATE_SINGLE_SCALAR)
412412
);
413413
}

0 commit comments

Comments
 (0)