forked from phpstan/phpstan-src
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support conditional return types by union-ing their branches
First baby step for phpstan/phpstan#3853
- Loading branch information
1 parent
b220021
commit 08b4afd
Showing
2 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Bug3853; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
abstract class Test | ||
{ | ||
/** | ||
* @template TKey as array-key | ||
* @template TArray as array<TKey, mixed> | ||
* | ||
* @param TArray $array | ||
* | ||
* @return (TArray is non-empty-array ? non-empty-list<TKey> : list<TKey>) | ||
*/ | ||
abstract public function arrayKeys(array $array); | ||
|
||
/** | ||
* @param array $array | ||
* @param non-empty-array $nonEmptyArray | ||
* | ||
* @param array<int, int> $intArray | ||
* @param non-empty-array<int, int> $nonEmptyIntArray | ||
*/ | ||
public function testArrayKeys(array $array, array $nonEmptyArray, array $intArray, array $nonEmptyIntArray): void | ||
{ | ||
assertType('array<int, (int|string)>', $this->arrayKeys($array)); | ||
assertType('array<int, int>', $this->arrayKeys($intArray)); | ||
|
||
// TODO resolve correctly | ||
//assertType('non-empty-array<int, (int|string)>', $this->arrayKeys($nonEmptyArray)); | ||
//assertType('non-empty-array<int, int>', $this->arrayKeys($nonEmptyIntArray)); | ||
|
||
assertType('array<int, (int|string)>', $this->arrayKeys($nonEmptyArray)); | ||
assertType('array<int, int>', $this->arrayKeys($nonEmptyIntArray)); | ||
} | ||
|
||
/** | ||
* @return ($as_float is true ? float : string) | ||
*/ | ||
abstract public function microtime(bool $as_float = false); | ||
|
||
public function testMicrotime(): void | ||
{ | ||
// TODO resolve correctly | ||
//assertType('float', $this->microtime(true)); | ||
//assertType('string', $this->microtime(false)); | ||
|
||
assertType('float|string', $this->microtime(true)); | ||
assertType('float|string', $this->microtime(false)); | ||
} | ||
} |