Skip to content

Commit effad95

Browse files
committed
Support for conditional types in @param-out
1 parent e76f64e commit effad95

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/Reflection/ResolvedFunctionVariant.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ function (ParameterReflectionWithPhpDocs $param): ParameterReflectionWithPhpDocs
8484

8585
$paramOutType = $param->getOutType();
8686
if ($paramOutType !== null) {
87-
$paramOutType = TemplateTypeHelper::resolveTemplateTypes(
88-
$paramOutType,
89-
$this->resolvedTemplateTypeMap,
90-
$this->callSiteVarianceMap,
91-
TemplateTypeVariance::createCovariant(),
87+
$paramOutType = TypeUtils::resolveLateResolvableTypes(
88+
TemplateTypeHelper::resolveTemplateTypes(
89+
$this->resolveConditionalTypesForParameter($paramOutType),
90+
$this->resolvedTemplateTypeMap,
91+
$this->callSiteVarianceMap,
92+
TemplateTypeVariance::createCovariant(),
93+
),
94+
false,
9295
);
9396
}
9497

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ public function dataFileAsserts(): iterable
333333
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4500.php');
334334
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4504.php');
335335
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4436.php');
336+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10699.php');
336337
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Properties/data/bug-3777.php');
337338
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2549.php');
338339
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1945.php');
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Bug10699;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param int $flags
9+
* @param mixed $out
10+
*
11+
* @param-out ($flags is 2 ? 20 : 10) $out
12+
*
13+
* @return ($flags is 2 ? 20 : 10)
14+
*/
15+
function test(int $flags, &$out): int
16+
{
17+
$out = $flags === 2 ? 20 : 10;
18+
19+
return $out;
20+
}
21+
22+
function (): void {
23+
$res = test(1, $out);
24+
assertType('10', $res);
25+
assertType('10', $out);
26+
27+
$res = test(2, $out);
28+
assertType('20', $res);
29+
assertType('20', $out);
30+
};

0 commit comments

Comments
 (0)