Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 3 additions & 43 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,34 +342,11 @@
<InvalidArgument>
<code><![CDATA[$message->getDetails()]]></code>
</InvalidArgument>
<MismatchingDocblockParamType>
<code><![CDATA[\ArrayAccess<int, Any>&RepeatedField]]></code>
</MismatchingDocblockParamType>
<MismatchingDocblockPropertyType>
<code><![CDATA[\ArrayAccess<int, Any>&RepeatedField]]></code>
</MismatchingDocblockPropertyType>
<MismatchingDocblockReturnType>
<code><![CDATA[\ArrayAccess<int, Any>&RepeatedField]]></code>
</MismatchingDocblockReturnType>
</file>
<file src="src/Exception/Client/ServiceClientException.php">
<ImplementedReturnTypeMismatch>
<code><![CDATA[RepeatedField]]></code>
</ImplementedReturnTypeMismatch>
<InvalidReturnStatement>
<code><![CDATA[$this->status->getDetails()]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[RepeatedField]]></code>
</InvalidReturnType>
</file>
<file src="src/Exception/Client/UnpackDetailsTrait.php">
<RawObjectIteration>
<code><![CDATA[$details]]></code>
</RawObjectIteration>
<UndefinedInterfaceMethod>
<code><![CDATA[count]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Exception/Client/WorkflowException.php">
<UnsafeInstantiation>
Expand Down Expand Up @@ -827,9 +804,6 @@
<NullableReturnStatement>
<code><![CDATA[$mapper]]></code>
</NullableReturnStatement>
<PossibleRawObjectIteration>
<code><![CDATA[$value]]></code>
</PossibleRawObjectIteration>
<PossiblyNullArgument>
<code><![CDATA[$metadata->getDetails()]]></code>
<code><![CDATA[$metadata->getSummary()]]></code>
Expand Down Expand Up @@ -997,13 +971,6 @@
<code><![CDATA[$reflection->getName()]]></code>
</PropertyTypeCoercion>
</file>
<file src="src/Internal/Support/Facade.php">
<InvalidDocblock>
<code><![CDATA[object<T>|null]]></code>
<code><![CDATA[private static ?object $ctx = null;]]></code>
<code><![CDATA[public static function getCurrentContext(): object]]></code>
</InvalidDocblock>
</file>
<file src="src/Internal/Support/Inheritance.php">
<PossiblyFalseArgument>
<code><![CDATA[$implements]]></code>
Expand Down Expand Up @@ -1513,16 +1480,9 @@
</UnsafeInstantiation>
</file>
<file src="src/Workflow.php">
<InvalidReturnStatement>
<code><![CDATA[self::getCurrentContext()->newActivityStub($class, $options)]]></code>
<code><![CDATA[self::getCurrentContext()->registerQuery($queryType, $handler, $description)]]></code>
<code><![CDATA[self::getCurrentContext()->registerSignal($name, $handler, $description)]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[ScopedContextInterface]]></code>
<code><![CDATA[ScopedContextInterface]]></code>
<code><![CDATA[T]]></code>
</InvalidReturnType>
<UndefinedInterfaceMethod>
<code><![CDATA[getUpdateContext]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Workflow/ChildWorkflowOptions.php">
<PossiblyNullReference>
Expand Down
26 changes: 15 additions & 11 deletions src/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@
use Temporal\DataConverter\Type;
use Temporal\DataConverter\ValuesInterface;
use Temporal\Exception\OutOfContextException;
use Temporal\Internal\Activity\ActivityContext;
use Temporal\Internal\Support\Facade;

/**
* @template-extends Facade<ActivityContextInterface>
*/
final class Activity extends Facade
{
/**
* Get the current Activity context.
* @throws OutOfContextException
*/
public static function getCurrentContext(): ActivityContextInterface
{
$ctx = parent::getCurrentContext();
/** @var ActivityContext $ctx */
$ctx::class === ActivityContext::class or throw new OutOfContextException(
'The Activity facade can only be used in the context of an activity execution.',
);
return $ctx;
}

/**
* Returns information about current activity execution.
*
* @throws OutOfContextException in the absence of the activity execution context.
*/
public static function getInfo(): ActivityInfo
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

return $context->getInfo();
Expand All @@ -57,7 +68,6 @@ public static function getInfo(): ActivityInfo
*/
public static function getInput(): ValuesInterface
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

return $context->getInput();
Expand All @@ -72,7 +82,6 @@ public static function getInput(): ValuesInterface
*/
public static function hasHeartbeatDetails(): bool
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

return $context->hasHeartbeatDetails();
Expand All @@ -88,7 +97,6 @@ public static function hasHeartbeatDetails(): bool
*/
public static function getHeartbeatDetails($type = null): mixed
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

return $context->getLastHeartbeatDetails($type);
Expand All @@ -101,7 +109,6 @@ public static function getHeartbeatDetails($type = null): mixed
*/
public static function getCancellationDetails(): ?ActivityCancellationDetails
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

return $context->getCancellationDetails();
Expand All @@ -118,7 +125,6 @@ public static function getCancellationDetails(): ?ActivityCancellationDetails
*/
public static function doNotCompleteOnReturn(): void
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

$context->doNotCompleteOnReturn();
Expand Down Expand Up @@ -150,7 +156,6 @@ public static function doNotCompleteOnReturn(): void
*/
public static function heartbeat($details): void
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

$context->heartbeat($details);
Expand All @@ -161,7 +166,6 @@ public static function heartbeat($details): void
*/
public static function getInstance(): object
{
/** @var ActivityContextInterface $context */
$context = self::getCurrentContext();

return $context->getInstance();
Expand Down
8 changes: 0 additions & 8 deletions src/Internal/Support/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

use Temporal\Exception\OutOfContextException;

/**
* @template T of object
*/
abstract class Facade
{
/**
Expand All @@ -26,9 +23,6 @@ abstract class Facade
'from the currently running process'
;

/**
* @var object<T>|null
*/
private static ?object $ctx = null;

/**
Expand All @@ -40,7 +34,6 @@ private function __construct()
}

/**
* @param object<T>|null $ctx
* @internal
*/
public static function setCurrentContext(?object $ctx): void
Expand All @@ -49,7 +42,6 @@ public static function setCurrentContext(?object $ctx): void
}

/**
* @return object<T>
* @throws OutOfContextException
*/
public static function getCurrentContext(): object
Expand Down
1 change: 1 addition & 0 deletions src/Internal/Transport/Router/InvokeActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static function (ActivityInput $input) use ($handler, $context): mixed {
'handleActivityInbound',
)(new ActivityInput($context->getInput(), $context->getHeader()));

/** @var ActivityContext $context */
$context = Activity::getCurrentContext();
if ($context->isDoNotCompleteOnReturn()) {
$resolver->reject(DoNotCompleteOnResultException::create());
Expand Down
2 changes: 0 additions & 2 deletions src/Internal/Transport/Router/StartWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Temporal\Internal\Workflow\WorkflowContext;
use Temporal\Worker\FeatureFlags;
use Temporal\Worker\Transport\Command\ServerRequestInterface;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInfo;

final class StartWorkflow extends Route
Expand Down Expand Up @@ -90,7 +89,6 @@ public function handle(ServerRequestInterface $request, array $headers, Deferred
);
$runId = $request->getID();

Workflow::setCurrentContext($context);
$process = new Process($this->services, $runId, $instance);
$this->services->running->add($process);
$resolver->resolve(EncodedValues::fromValues([null]));
Expand Down
5 changes: 1 addition & 4 deletions src/Internal/Workflow/ActivityStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public function execute(

protected function request(RequestInterface $request): PromiseInterface
{
/** @var Workflow\WorkflowContextInterface $context */
$context = Workflow::getCurrentContext();

return $context->request($request);
return Workflow::getCurrentContext()->request($request);
}
}
5 changes: 1 addition & 4 deletions src/Internal/Workflow/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ function (WorkflowExecution $execution) use ($name, $args) {

protected function request(RequestInterface $request, bool $cancellable = true): PromiseInterface
{
/** @var Workflow\WorkflowContextInterface $context */
$context = Workflow::getCurrentContext();

return $context->request($request, cancellable: $cancellable);
return Workflow::getCurrentContext()->request($request, cancellable: $cancellable);
}

private function getOptionArray(): array
Expand Down
1 change: 0 additions & 1 deletion src/Internal/Workflow/ExternalWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function cancel(): PromiseInterface
private function request(RequestInterface $request): PromiseInterface
{
// todo intercept
/** @var Workflow\WorkflowContextInterface $context */
$context = Workflow::getCurrentContext();

return $context->request($request);
Expand Down
42 changes: 28 additions & 14 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Exception\OutOfContextException;
use Temporal\Internal\Support\Facade;
use Temporal\Internal\Workflow\ScopeContext;
use Temporal\Workflow\ActivityStubInterface;
use Temporal\Workflow\CancellationScopeInterface;
use Temporal\Workflow\ChildWorkflowOptions;
Expand All @@ -45,18 +44,27 @@
*
* This is main class you can use in your workflow code.
*
* @method static ScopeContext getCurrentContext() Get current workflow context.
*
* @psalm-import-type TypeEnum from Type
* @psalm-import-type DateIntervalValue from DateInterval
* @see DateInterval
*
* @template-extends Facade<ScopedContextInterface>
*/
final class Workflow extends Facade
{
public const DEFAULT_VERSION = -1;

/**
* Get the current Workflow context.
* @throws OutOfContextException
*/
public static function getCurrentContext(): WorkflowContextInterface
{
$ctx = parent::getCurrentContext();
$ctx instanceof WorkflowContextInterface or throw new OutOfContextException(
'The Workflow facade can be used only inside workflow code.',
);
return $ctx;
}

/**
* Returns current datetime.
*
Expand Down Expand Up @@ -192,7 +200,9 @@ public static function getInput(): ValuesInterface
*/
public static function async(callable $task): CancellationScopeInterface
{
return self::getCurrentContext()->async($task);
$ctx = self::getCurrentContext();
\assert($ctx instanceof ScopedContextInterface);
return $ctx->async($task);
}

/**
Expand Down Expand Up @@ -244,7 +254,9 @@ public static function async(callable $task): CancellationScopeInterface
*/
public static function asyncDetached(callable $task): CancellationScopeInterface
{
return self::getCurrentContext()->asyncDetached($task);
$ctx = self::getCurrentContext();
\assert($ctx instanceof ScopedContextInterface);
return $ctx->asyncDetached($task);
}

/**
Expand Down Expand Up @@ -352,7 +364,9 @@ public static function registerQuery(
callable $handler,
string $description = '',
): ScopedContextInterface {
return self::getCurrentContext()->registerQuery($queryType, $handler, $description);
$ctx = self::getCurrentContext();
\assert($ctx instanceof ScopedContextInterface);
return $ctx->registerQuery($queryType, $handler, $description);
}

/**
Expand All @@ -372,7 +386,9 @@ public static function registerQuery(
*/
public static function registerSignal(string $name, callable $handler, string $description = ''): ScopedContextInterface
{
return self::getCurrentContext()->registerSignal($name, $handler, $description);
$ctx = self::getCurrentContext();
\assert($ctx instanceof ScopedContextInterface);
return $ctx->registerSignal($name, $handler, $description);
}

/**
Expand Down Expand Up @@ -491,7 +507,9 @@ public static function registerUpdate(
?callable $validator = null,
string $description = '',
): ScopedContextInterface {
return self::getCurrentContext()->registerUpdate($name, $handler, $validator, $description);
$ctx = self::getCurrentContext();
\assert($ctx instanceof ScopedContextInterface);
return $ctx->registerUpdate($name, $handler, $validator, $description);
}

/**
Expand Down Expand Up @@ -995,7 +1013,6 @@ public static function getStackTrace(): string
*/
public static function allHandlersFinished(): bool
{
/** @var ScopedContextInterface $context */
$context = self::getCurrentContext();

return $context->allHandlersFinished();
Expand Down Expand Up @@ -1081,7 +1098,6 @@ public static function upsertTypedSearchAttributes(SearchAttributeUpdate ...$upd
*/
public static function uuid(): PromiseInterface
{
/** @var ScopedContextInterface $context */
$context = self::getCurrentContext();

return $context->uuid();
Expand All @@ -1094,7 +1110,6 @@ public static function uuid(): PromiseInterface
*/
public static function uuid4(): PromiseInterface
{
/** @var ScopedContextInterface $context */
$context = self::getCurrentContext();

return $context->uuid4();
Expand All @@ -1111,7 +1126,6 @@ public static function uuid4(): PromiseInterface
*/
public static function uuid7(?\DateTimeInterface $dateTime = null): PromiseInterface
{
/** @var ScopedContextInterface $context */
$context = self::getCurrentContext();

return $context->uuid7($dateTime);
Expand Down
5 changes: 5 additions & 0 deletions src/Workflow/WorkflowContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,9 @@ public function uuid7(?\DateTimeInterface $dateTime = null): PromiseInterface;
* Logs in replay mode are omitted unless {@see WorkerOptions::$enableLoggingInReplay} is set to true.
*/
public function getLogger(): LoggerInterface;

/**
* Get the currently running Workflow instance.
*/
public function getInstance(): object;
}
Loading