Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unresolve template type checks for complex conditional types #1377

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function check(
TypeTraverser::map(
$parametersAcceptor->getReturnTypeWithUnresolvableTemplateTypes(),
static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Type {
if ($type instanceof ConditionalType) {
while ($type instanceof ConditionalType && $type->isResolvable()) {
$type = $type->resolve();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7341.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-strstr-specifying.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-strrchr-specifying.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/conditional-complex-templates.php');
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2469,4 +2469,14 @@ public function testUnresolvableParameter(): void
]);
}


public function testConditionalComplexTemplates(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/conditional-complex-templates.php'], []);
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Methods/data/conditional-complex-templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ConditionalComplexTemplates;

use function PHPStan\Testing\assertType;

/** @template T */
interface PromiseInterface
{
/**
* @template TFulfilled of mixed
* @template TRejected of mixed
* @param (callable(T): TFulfilled)|null $onFulfilled
* @param (callable(mixed): TRejected)|null $onRejected
* @return PromiseInterface<(
* $onFulfilled is not null
* ? ($onRejected is not null ? TFulfilled|TRejected : TFulfilled)
* : ($onRejected is not null ? TRejected : T)
* )>
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);
}

/**
* @param PromiseInterface<true> $promise
*/
function test(PromiseInterface $promise): void
{
$passThroughBoolFn = static fn (bool $bool): bool => $bool;

assertType('ConditionalComplexTemplates\PromiseInterface<bool>', $promise->then($passThroughBoolFn));
assertType('ConditionalComplexTemplates\PromiseInterface<bool>', $promise->then()->then($passThroughBoolFn));
}