Skip to editor
Psalm
Docs
Playground
Plugins
Blog
Paid support
Share
Fix issues
Show original
PHP
Settings
<?php /** @template T */ interface PromiseInterface { /** * @template TReturn of mixed * @param callable(T): TReturn $callable * @return (TReturn is PromiseInterface ? TReturn : PromiseInterface<TReturn>) */ public function then($callable): PromiseInterface; } /** * @template T * @template-implements PromiseInterface<T> */ class Promise implements PromiseInterface { /** @var T */ private $a; public function __construct(callable $f) { $f(function ($a) { $this->a = $a; }); } public function then($callable): PromiseInterface { return new Promise(static function (callable $resolve) use ($callable): void { $resolve($callable($this->a)); }); } } /** * @template T * @param T $a * @return PromiseInterface<T> */ function resolve($a): PromiseInterface{ return new Promise(static function (callable $resolve) use ($a): void { $resolve($a); }); } /** * @template T * @param PromiseInterface<T> $a * @return T */ function await(PromiseInterface $a): mixed { $result = null; $a->then(function ($v) use (&$result): void { $result = $v; }); return $result; } /** * @template T * @param callable(): T $a * @return callable(): PromiseInterface<T> */ function async(callable $a){ return static fn (): PromiseInterface => new Promise(static function (callable $resolve) use ($a): void { $resolve($a()); }); } /** * @return PromiseInterface<string> */ function a(): PromiseInterface { return resolve('string'); } function user_code(string $a):void{ $promise = new Promise(static function (callable $resolve) use ($a): void { $resolve($a); }); /** @psalm-trace $promise */; a()->then(function (bool $_a) {}); a()->then(function (string $_a) {}); $b = await(a()); // Both PHPStan and Psalm should understand that $a will be a string /** @psalm-trace $b */; async(function (): string { $a = await(a()); // Both PHPStan and Psalm should understand that $a will be a string return strrev($a); })()->then(function (string $a) {}); }
Issues
Types
idle
Saved Mar 24 2022 07:28 UTC
⌘/Ctrl+Enter
run
⌘/Ctrl+S
share
F8
/
⇧F8
next/prev issue