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
82716a6
commit c7e3eed
Showing
3 changed files
with
65 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
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 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rvanvelzen
Author
Contributor
|
||
* @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)); | ||
} | ||
} |
What does the "as" mean? Is it just a synonym for "of", or does it behave differently? Or is this just a typo that slipped through?