Skip to content

Commit e1fd5d2

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Improve unresolve template type checks for complex conditional types
1 parent 75c5574 commit e1fd5d2

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/Rules/FunctionCallParametersCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public function check(
355355
TypeTraverser::map(
356356
$parametersAcceptor->getReturnTypeWithUnresolvableTemplateTypes(),
357357
static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Type {
358-
if ($type instanceof ConditionalType) {
358+
while ($type instanceof ConditionalType && $type->isResolvable()) {
359359
$type = $type->resolve();
360360
}
361361

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ public function dataFileAsserts(): iterable
902902
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7341.php');
903903
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-strstr-specifying.php');
904904
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-strrchr-specifying.php');
905+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/conditional-complex-templates.php');
905906
}
906907

907908
/**

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,4 +2469,14 @@ public function testUnresolvableParameter(): void
24692469
]);
24702470
}
24712471

2472+
2473+
public function testConditionalComplexTemplates(): void
2474+
{
2475+
$this->checkThisOnly = false;
2476+
$this->checkNullables = true;
2477+
$this->checkUnionTypes = true;
2478+
$this->checkExplicitMixed = false;
2479+
$this->analyse([__DIR__ . '/data/conditional-complex-templates.php'], []);
2480+
}
2481+
24722482
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace ConditionalComplexTemplates;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @template T */
8+
interface PromiseInterface
9+
{
10+
/**
11+
* @template TFulfilled of mixed
12+
* @template TRejected of mixed
13+
* @param (callable(T): TFulfilled)|null $onFulfilled
14+
* @param (callable(mixed): TRejected)|null $onRejected
15+
* @return PromiseInterface<(
16+
* $onFulfilled is not null
17+
* ? ($onRejected is not null ? TFulfilled|TRejected : TFulfilled)
18+
* : ($onRejected is not null ? TRejected : T)
19+
* )>
20+
*/
21+
public function then(callable $onFulfilled = null, callable $onRejected = null);
22+
}
23+
24+
/**
25+
* @param PromiseInterface<true> $promise
26+
*/
27+
function test(PromiseInterface $promise): void
28+
{
29+
$passThroughBoolFn = static fn (bool $bool): bool => $bool;
30+
31+
assertType('ConditionalComplexTemplates\PromiseInterface<bool>', $promise->then($passThroughBoolFn));
32+
assertType('ConditionalComplexTemplates\PromiseInterface<bool>', $promise->then()->then($passThroughBoolFn));
33+
}

0 commit comments

Comments
 (0)