-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent "unable to resolve template type T" in conditional branches
- Loading branch information
1 parent
e4e9e08
commit 0834597
Showing
5 changed files
with
93 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/PHPStan/Rules/Methods/data/template-type-in-one-branch-of-conditional.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TemplateTypeInOneBranchOfConditional; | ||
|
||
use stdClass; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class Connection {} | ||
|
||
class ChildConnection extends Connection {} | ||
|
||
class DriverManager | ||
{ | ||
/** | ||
* @param array{wrapperClass?: class-string<T>} $params | ||
* @phpstan-return ($params is array{wrapperClass:mixed} ? T : Connection) | ||
* @template T of Connection | ||
*/ | ||
public static function getConnection(array $params): Connection { | ||
return new Connection(); | ||
} | ||
|
||
public static function test(): void | ||
{ | ||
assertType(Connection::class, DriverManager::getConnection([])); | ||
assertType(ChildConnection::class, DriverManager::getConnection(['wrapperClass' => ChildConnection::class])); | ||
assertType(Connection::class, DriverManager::getConnection(['wrapperClass' => stdClass::class])); | ||
} | ||
} |