-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These annotations will aid static analyses like PHPStan and Psalm to enhance type-safety for this project and projects depending on it These changes make the following example understandable by PHPStan: ```php final readonly class User { public function __construct( public string $name, ) } /** * \React\Promise\PromiseInterface<User> */ function getCurrentUserFromDatabase(): \React\Promise\PromiseInterface { // The following line would do the database query and fetch the result from it // but keeping it simple for the sake of the example. return \React\Promise\resolve(new User('WyriHaximus')); } // For the sake of this example we're going to assume the following code runs // in \React\Async\async call echo await(getCurrentUserFromDatabase())->name; // This echos: WyriHaximus ```
- Loading branch information
1 parent
037cbf2
commit d1c9606
Showing
12 changed files
with
239 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,6 @@ jobs: | |
- 7.4 | ||
- 7.3 | ||
- 7.2 | ||
- 7.1 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: shivammathur/setup-php@v2 | ||
|
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
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,18 @@ | ||
<?php | ||
|
||
use function PHPStan\Testing\assertType; | ||
use function React\Async\await; | ||
use function React\Promise\resolve; | ||
|
||
assertType('bool', await(resolve(true))); | ||
|
||
final class AwaitExampleUser | ||
{ | ||
public string $name; | ||
|
||
public function __construct(string $name) { | ||
$this->name = $name; | ||
} | ||
} | ||
|
||
assertType('string', await(resolve(new AwaitExampleUser('WyriHaximus')))->name); |
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,60 @@ | ||
<?php | ||
|
||
use function PHPStan\Testing\assertType; | ||
use function React\Async\await; | ||
use function React\Async\coroutine; | ||
use function React\Promise\resolve; | ||
|
||
assertType('React\Promise\PromiseInterface<bool>', coroutine(static function () { | ||
return true; | ||
})); | ||
|
||
assertType('React\Promise\PromiseInterface<bool>', coroutine(static function () { | ||
return resolve(true); | ||
})); | ||
|
||
// assertType('React\Promise\PromiseInterface<bool>', coroutine(static function () { | ||
// return (yield resolve(true)); | ||
// })); | ||
|
||
assertType('React\Promise\PromiseInterface<int>', coroutine(static function () { | ||
// $bool = yield resolve(true); | ||
// assertType('bool', $bool); | ||
|
||
return time(); | ||
})); | ||
|
||
// assertType('React\Promise\PromiseInterface<bool>', coroutine(static function () { | ||
// $bool = yield resolve(true); | ||
// assertType('bool', $bool); | ||
|
||
// return $bool; | ||
// })); | ||
|
||
assertType('React\Promise\PromiseInterface<bool>', coroutine(static function () { | ||
yield resolve(time()); | ||
|
||
return true; | ||
})); | ||
|
||
assertType('React\Promise\PromiseInterface<bool>', coroutine(static function () { | ||
for ($i = 0; $i <= 10; $i++) { | ||
yield resolve($i); | ||
} | ||
|
||
return true; | ||
})); | ||
|
||
assertType('React\Promise\PromiseInterface<int>', coroutine(static function (int $a): int { return $a; }, 42)); | ||
assertType('React\Promise\PromiseInterface<int>', coroutine(static function (int $a, int $b): int { return $a + $b; }, 10, 32)); | ||
assertType('React\Promise\PromiseInterface<int>', coroutine(static function (int $a, int $b, int $c): int { return $a + $b + $c; }, 10, 22, 10)); | ||
assertType('React\Promise\PromiseInterface<int>', coroutine(static function (int $a, int $b, int $c, int $d): int { return $a + $b + $c + $d; }, 10, 22, 5, 5)); | ||
assertType('React\Promise\PromiseInterface<int>', coroutine(static function (int $a, int $b, int $c, int $d, int $e): int { return $a + $b + $c + $d + $e; }, 10, 12, 10, 5, 5)); | ||
|
||
assertType('bool', await(coroutine(static function () { | ||
return true; | ||
}))); | ||
|
||
// assertType('bool', await(coroutine(static function () { | ||
// return (yield resolve(true)); | ||
// }))); |
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,33 @@ | ||
<?php | ||
|
||
use React\Promise\PromiseInterface; | ||
use function PHPStan\Testing\assertType; | ||
use function React\Async\await; | ||
use function React\Async\parallel; | ||
use function React\Promise\resolve; | ||
|
||
assertType('React\Promise\PromiseInterface<array>', parallel([])); | ||
|
||
assertType('React\Promise\PromiseInterface<array<bool|float|int>>', parallel([ | ||
static function (): PromiseInterface { return resolve(true); }, | ||
static function (): PromiseInterface { return resolve(time()); }, | ||
static function (): PromiseInterface { return resolve(microtime(true)); }, | ||
])); | ||
|
||
assertType('React\Promise\PromiseInterface<array<bool|float|int>>', parallel([ | ||
static function (): bool { return true; }, | ||
static function (): int { return time(); }, | ||
static function (): float { return microtime(true); }, | ||
])); | ||
|
||
assertType('array<bool|float|int>', await(parallel([ | ||
static function (): PromiseInterface { return resolve(true); }, | ||
static function (): PromiseInterface { return resolve(time()); }, | ||
static function (): PromiseInterface { return resolve(microtime(true)); }, | ||
]))); | ||
|
||
assertType('array<bool|float|int>', await(parallel([ | ||
static function (): bool { return true; }, | ||
static function (): int { return time(); }, | ||
static function (): float { return microtime(true); }, | ||
]))); |
Oops, something went wrong.