Skip to content

Commit 59791e5

Browse files
committed
Add docs about VirtualPromise
1 parent 67cbd27 commit 59791e5

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,45 @@ composer require temporal-php/support
3434

3535
### VirtualPromise interface
3636

37+
Every time we use `yield` in a Workflow to wait for an action to complete, a Promise is actually yielded.
38+
At this point, the IDE and static analyzer usually get lost in type definitions,
39+
and we experience difficulties and inconveniences because of this.
40+
However, if the Promise interface had the `@yield` annotation, we could explain to the IDE what type of value we expect to be sent back into the generator from the coroutine.
41+
Since ReactPHP [isn't yet planning](https://github.com/orgs/reactphp/discussions/536) to add the `@yield` annotation to their promises (Temporal PHP uses ReactPHP promises),
42+
we suggest using our solution for typing - `VirtualPromise`.
43+
44+
```php
45+
use Temporal\Support\VirtualPromise;
46+
47+
#[\Temporal\Activity\ActivityInterface]
48+
class HelloService {
49+
/**
50+
* @param non-empty-string $name
51+
*
52+
* @return VirtualPromise<non-empty-string>
53+
*/
54+
public function greet(string $name) {
55+
// ...
56+
}
57+
}
58+
59+
#[\Temporal\Workflow\WorkflowInterface]
60+
class WorkflowClass {
61+
#[\Temporal\Workflow\WorkflowMethod]
62+
public function run(string $name) {
63+
$activity = \Temporal\Support\Factory\ActivityStub::activity(HelloService::class);
64+
65+
// IDE will know that $name is a non-empty-string
66+
$name = yield $activity->greet($name);
67+
// ...
68+
}
69+
}
70+
```
71+
72+
> Warning: don't implement the `VirtualPromise` interface yourself, use it only as a type hint.
73+
74+
> PHPStorm and Psalm can handle the @yield annotation, but PHPStan can't yet ([issue](https://github.com/phpstan/phpstan/issues/4245)).
75+
3776
### Attributes
3877

3978
## Contributing

0 commit comments

Comments
 (0)