Skip to content

Commit 92ecec8

Browse files
authored
Merge pull request #627: Add PromiseInterface stub for IDE
1 parent 86cdf75 commit 92ecec8

File tree

10 files changed

+80
-16
lines changed

10 files changed

+80
-16
lines changed

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,7 @@
15921592
<code><![CDATA[PromiseInterface<bool>]]></code>
15931593
<code><![CDATA[PromiseInterface<int>]]></code>
15941594
<code><![CDATA[PromiseInterface<mixed>]]></code>
1595+
<code><![CDATA[PromiseInterface<mixed>]]></code>
15951596
<code><![CDATA[PromiseInterface<null>]]></code>
15961597
</TooManyTemplateParams>
15971598
</file>
File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace React\Promise;
6+
7+
/**
8+
* @note This is a stub interface for better IDE support.
9+
*
10+
* @yield T
11+
* @template-covariant T
12+
*/
13+
interface PromiseInterface
14+
{
15+
/**
16+
* Transforms a promise's value by applying a function to the promise's fulfillment
17+
* or rejection value. Returns a new promise for the transformed result.
18+
*
19+
* The `then()` method registers new fulfilled and rejection handlers with a promise
20+
* (all parameters are optional):
21+
*
22+
* * `$onFulfilled` will be invoked once the promise is fulfilled and passed
23+
* the result as the first argument.
24+
* * `$onRejected` will be invoked once the promise is rejected and passed the
25+
* reason as the first argument.
26+
* * `$onProgress` (deprecated) will be invoked whenever the producer of the promise
27+
* triggers progress notifications and passed a single argument (whatever it
28+
* wants) to indicate progress.
29+
*
30+
* It returns a new promise that will fulfill with the return value of either
31+
* `$onFulfilled` or `$onRejected`, whichever is called, or will reject with
32+
* the thrown exception if either throws.
33+
*
34+
* A promise makes the following guarantees about handlers registered in
35+
* the same call to `then()`:
36+
*
37+
* 1. Only one of `$onFulfilled` or `$onRejected` will be called,
38+
* never both.
39+
* 2. `$onFulfilled` and `$onRejected` will never be called more
40+
* than once.
41+
* 3. `$onProgress` (deprecated) may be called multiple times.
42+
*
43+
* @template TFulfilled
44+
* @template TRejected
45+
* @param ?(callable((T is void ? null : T)): (PromiseInterface<TFulfilled>|TFulfilled)) $onFulfilled
46+
* @param ?(callable(\Throwable): (PromiseInterface<TRejected>|TRejected)) $onRejected
47+
* @return PromiseInterface<($onRejected is null ? ($onFulfilled is null ? T : TFulfilled) : ($onFulfilled is null ? T|TRejected : TFulfilled|TRejected))>
48+
*/
49+
public function then(?callable $onFulfilled = null, ?callable $onRejected = null, ?callable $onProgress = null);
50+
}

src/Internal/Workflow/WorkflowContext.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,9 @@ public function getLastCompletionResultValues(): ?ValuesInterface
183183
return $this->lastCompletionResult;
184184
}
185185

186-
public function getLastCompletionResult($type = null)
186+
public function getLastCompletionResult(mixed $type = null): mixed
187187
{
188-
if ($this->lastCompletionResult === null) {
189-
return null;
190-
}
191-
192-
return $this->lastCompletionResult->getValue(0, $type);
188+
return $this->lastCompletionResult?->getValue(0, $type);
193189
}
194190

195191
public function getClient(): ClientInterface

src/Workflow.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public static function getInput(): ValuesInterface
183183
* You can see more information about the capabilities of the child
184184
* asynchronous task in {@see CancellationScopeInterface} interface.
185185
*
186+
* @template TReturn
187+
* @param callable(): (TReturn|\Generator<mixed, mixed, mixed, TReturn>) $task
188+
* @return CancellationScopeInterface<TReturn>
189+
*
186190
* @throws OutOfContextException in the absence of the workflow execution context.
187191
*/
188192
public static function async(callable $task): CancellationScopeInterface
@@ -231,6 +235,10 @@ public static function async(callable $task): CancellationScopeInterface
231235
*
232236
* Use asyncDetached to handle cleanup and compensation logic.
233237
*
238+
* @template TReturn
239+
* @param callable(): (TReturn|\Generator<mixed, mixed, mixed, TReturn>) $task
240+
* @return CancellationScopeInterface<TReturn>
241+
*
234242
* @throws OutOfContextException in the absence of the workflow execution context.
235243
*/
236244
public static function asyncDetached(callable $task): CancellationScopeInterface
@@ -315,10 +323,9 @@ public static function awaitWithTimeout($interval, callable|Mutex|PromiseInterfa
315323
* Returns value of last completion result, if any.
316324
*
317325
* @param Type|TypeEnum|mixed $type
318-
* @return mixed
319326
* @throws OutOfContextException in the absence of the workflow execution context.
320327
*/
321-
public static function getLastCompletionResult($type = null)
328+
public static function getLastCompletionResult($type = null): mixed
322329
{
323330
return self::getCurrentContext()->getLastCompletionResult($type);
324331
}
@@ -685,7 +692,10 @@ public static function newContinueAsNewStub(string $class, ?ContinueAsNewOptions
685692
* }
686693
* ```
687694
*
695+
* @param non-empty-string $type
696+
* @param list<mixed> $args
688697
* @param Type|string|\ReflectionType|\ReflectionClass|null $returnType
698+
* @return PromiseInterface<mixed>
689699
*
690700
* @throws OutOfContextException in the absence of the workflow execution context.
691701
*/
@@ -879,6 +889,7 @@ public static function newUntypedExternalWorkflowStub(WorkflowExecution $executi
879889
* }
880890
* ```
881891
*
892+
* @param non-empty-string $type
882893
* @param ActivityOptions|null $options
883894
* @return PromiseInterface<mixed>
884895
* @throws OutOfContextException in the absence of the workflow execution context.

src/Workflow/CancellationScopeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use React\Promise\PromiseInterface;
1515

1616
/**
17-
* @template T
17+
* @template-covariant T
1818
* @yield T
1919
* @extends PromiseInterface<T>
2020
*/

src/Workflow/ScopedContextInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ interface ScopedContextInterface extends WorkflowContextInterface
2121
/**
2222
* The method calls an asynchronous task and returns a promise.
2323
*
24+
* @template TReturn
25+
* @param callable(): (TReturn|\Generator<mixed, mixed, mixed, TReturn>) $handler
26+
* @return CancellationScopeInterface<TReturn>
27+
*
2428
* @see Workflow::async()
2529
*/
2630
public function async(callable $handler): CancellationScopeInterface;
@@ -29,6 +33,10 @@ public function async(callable $handler): CancellationScopeInterface;
2933
* Cancellation scope which does not react to parent cancel and completes
3034
* in background.
3135
*
36+
* @template TReturn
37+
* @param callable(): (TReturn|\Generator<mixed, mixed, mixed, TReturn>) $handler
38+
* @return CancellationScopeInterface<TReturn>
39+
*
3240
* @see Workflow::asyncDetached()
3341
*/
3442
public function asyncDetached(callable $handler): CancellationScopeInterface;

src/Workflow/WorkflowContextInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ public function getInput(): ValuesInterface;
4949
* @see Workflow::getLastCompletionResult()
5050
*
5151
* @param Type|string|null $type
52-
* @return mixed
5352
*/
54-
public function getLastCompletionResult($type = null);
53+
public function getLastCompletionResult(mixed $type = null): mixed;
5554

5655
/**
5756
* A method that allows you to dynamically register additional query

tests/Acceptance/Extra/Versioning/VersioningTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function handle()
4444
$version = yield Workflow::getVersion('test', Workflow::DEFAULT_VERSION, 2);
4545

4646
if ($version === 1) {
47-
yield Workflow::sideEffect(static fn() => 'test');
47+
yield Workflow::sideEffect(static fn(): string => 'test');
4848
return 'v1';
4949
}
5050

tests/Fixtures/src/Workflow/SideEffectWorkflow.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Temporal\Tests\Workflow;
1313

1414
use Temporal\Activity\ActivityOptions;
15-
use Temporal\Common\Uuid;
1615
use Temporal\Workflow;
1716
use Temporal\Workflow\WorkflowMethod;
1817
use Temporal\Tests\Activity\SimpleActivity;
@@ -25,13 +24,13 @@ public function handler(string $input): iterable
2524
{
2625
$simple = Workflow::newActivityStub(
2726
SimpleActivity::class,
28-
ActivityOptions::new()->withStartToCloseTimeout(5)
27+
ActivityOptions::new()->withStartToCloseTimeout(5),
2928
);
3029

3130
$result = yield Workflow::sideEffect(
32-
function () use ($input) {
31+
static function () use ($input): string {
3332
return $input . '-42';
34-
}
33+
},
3534
);
3635

3736
return yield $simple->lower($result);

0 commit comments

Comments
 (0)