Skip to content

Commit fadb187

Browse files
authored
Merge pull request #630: Process special Temporal builtin prefixes
2 parents f4c4962 + d355edb commit fadb187

File tree

24 files changed

+1424
-62
lines changed

24 files changed

+1424
-62
lines changed

psalm-baseline.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,11 +1054,6 @@
10541054
<code><![CDATA[new static()]]></code>
10551055
</UnsafeInstantiation>
10561056
</file>
1057-
<file src="src/Internal/Support/StackRenderer.php">
1058-
<PossiblyInvalidArgument>
1059-
<code><![CDATA[$path]]></code>
1060-
</PossiblyInvalidArgument>
1061-
</file>
10621057
<file src="src/Internal/Transport/Client.php">
10631058
<PossiblyInvalidArgument>
10641059
<code><![CDATA[$command->getID()]]></code>
@@ -1272,6 +1267,9 @@
12721267
<code><![CDATA[$parent]]></code>
12731268
<code><![CDATA[$scope]]></code>
12741269
</PropertyNotSetInConstructor>
1270+
<UnsupportedPropertyReferenceUsage>
1271+
<code><![CDATA[$ctx->trace = &$context->trace]]></code>
1272+
</UnsupportedPropertyReferenceUsage>
12751273
</file>
12761274
<file src="src/Internal/Workflow/WorkflowContext.php">
12771275
<ArgumentTypeCoercion>

src/Client/Common/Paginator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace Temporal\Client\Common;
66

7-
use IteratorAggregate;
8-
97
/**
108
* Paginator that allows to iterate over all pages.
119
*
1210
* @template TItem
13-
* @implements IteratorAggregate<TItem>
11+
* @implements \IteratorAggregate<TItem>
1412
*/
1513
final class Paginator implements \IteratorAggregate, \Countable
1614
{

src/Client/GRPC/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private function __construct()
3232
->withInitialInterval(CarbonInterval::millisecond(500));
3333

3434
$this->metadata = [
35-
'client-name' => ['temporal-php-2'],
35+
'client-name' => [SdkVersion::SDK_NAME],
3636
'client-version' => [SdkVersion::getSdkVersion()],
3737
];
3838
}

src/Client/Workflow/WorkflowExecutionHistory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Temporal\Client\Workflow;
66

7-
use IteratorAggregate;
87
use Temporal\Api\History\V1\History;
98
use Temporal\Api\History\V1\HistoryEvent;
109
use Temporal\Api\Workflowservice\V1\GetWorkflowExecutionHistoryResponse;
@@ -17,7 +16,7 @@
1716
*
1817
* @see History
1918
*
20-
* @implements IteratorAggregate<int, HistoryEvent>
19+
* @implements \IteratorAggregate<int, HistoryEvent>
2120
* @internal
2221
*/
2322
final class WorkflowExecutionHistory implements \IteratorAggregate

src/Common/SdkVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
final class SdkVersion
1010
{
11+
/**
12+
* The name of the SDK in Temporal nomenclature.
13+
*/
14+
public const SDK_NAME = 'temporal-php-2';
15+
1116
public const VERSION_REGX = '/^(\d++\.\d++\.\d++(?:-[\\w\\-.]+)?)/';
1217
public const PACKAGE_NAME = 'temporal/sdk';
1318

src/Interceptor/HeaderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @psalm-type TValue=mixed
2020
* @psalm-import-type TypeEnum from Type
2121
*
22-
* @extends IteratorAggregate<TKey, TValue>
22+
* @extends \IteratorAggregate<TKey, TValue>
2323
*/
2424
interface HeaderInterface extends \Countable, IteratorAggregate
2525
{
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Internal\Declaration;
6+
7+
/**
8+
* Temporal entity name validator.
9+
*
10+
* @internal
11+
*/
12+
final class EntityNameValidator
13+
{
14+
public const COMMON_BUILTIN_PREFIX = '__temporal_';
15+
public const QUERY_TYPE_STACK_TRACE = "__stack_trace";
16+
public const ENHANCED_QUERY_TYPE_STACK_TRACE = "__enhanced_stack_trace";
17+
18+
/**
19+
* @throws \InvalidArgumentException
20+
*/
21+
public static function validateWorkflow(string $name): void
22+
{
23+
self::validateCommonPrefix($name, 'Workflow name');
24+
}
25+
26+
/**
27+
* @throws \InvalidArgumentException
28+
*/
29+
public static function validateQueryMethod(string $name): void
30+
{
31+
if ($name === self::QUERY_TYPE_STACK_TRACE || $name === self::ENHANCED_QUERY_TYPE_STACK_TRACE) {
32+
throw new \InvalidArgumentException(
33+
"The Query method name `$name` is reserved for built-in functionality and cannot be used.",
34+
);
35+
}
36+
37+
self::validateCommonPrefix($name, 'Query method');
38+
}
39+
40+
/**
41+
* @throws \InvalidArgumentException
42+
*/
43+
public static function validateSignalMethod(string $name): void
44+
{
45+
self::validateCommonPrefix($name, 'Signal method');
46+
}
47+
48+
/**
49+
* @throws \InvalidArgumentException
50+
*/
51+
public static function validateUpdateMethod(string $name): void
52+
{
53+
self::validateCommonPrefix($name, 'Update method');
54+
}
55+
56+
/**
57+
* @throws \InvalidArgumentException
58+
*/
59+
public static function validateActivity(string $name): void
60+
{
61+
self::validateCommonPrefix($name, 'Activity type');
62+
}
63+
64+
/**
65+
* @throws \InvalidArgumentException
66+
*/
67+
public static function validateTaskQueue(string $name): void
68+
{
69+
self::validateCommonPrefix($name, 'Task Queue name');
70+
}
71+
72+
/**
73+
* Throws an exception if the name starts with the common built-in prefix.
74+
*
75+
* @throws \InvalidArgumentException
76+
*/
77+
private static function validateCommonPrefix(string $name, string $target): void
78+
{
79+
\str_starts_with($name, self::COMMON_BUILTIN_PREFIX) and throw new \InvalidArgumentException(
80+
\sprintf(
81+
"%s must not start with the internal prefix `%s`.",
82+
$target,
83+
self::COMMON_BUILTIN_PREFIX,
84+
),
85+
);
86+
}
87+
}

src/Internal/Declaration/Prototype/ActivityPrototype.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Temporal\Activity\LocalActivityInterface;
1616
use Temporal\Common\MethodRetry;
1717
use Temporal\Internal\Declaration\ActivityInstance;
18+
use Temporal\Internal\Declaration\EntityNameValidator;
1819

1920
final class ActivityPrototype extends Prototype
2021
{
@@ -32,6 +33,7 @@ public function __construct(
3233
\ReflectionMethod $handler,
3334
\ReflectionClass $class,
3435
) {
36+
EntityNameValidator::validateActivity($name);
3537
$this->isLocalActivity = $interface instanceof LocalActivityInterface;
3638

3739
parent::__construct($name, $handler, $class);

src/Internal/Declaration/Prototype/Prototype.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515

1616
abstract class Prototype implements PrototypeInterface
1717
{
18-
protected string $name;
19-
protected ?\ReflectionMethod $handler;
20-
private \ReflectionClass $class;
21-
22-
public function __construct(string $name, ?\ReflectionMethod $handler, \ReflectionClass $class)
23-
{
24-
$this->handler = $handler;
25-
$this->name = $name;
26-
$this->class = $class;
27-
}
18+
public function __construct(
19+
protected string $name,
20+
protected ?\ReflectionMethod $handler,
21+
private readonly \ReflectionClass $class,
22+
) {}
2823

2924
/**
3025
* @template T of PrototypeInterface

src/Internal/Declaration/Prototype/WorkflowPrototype.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Temporal\Common\CronSchedule;
1515
use Temporal\Common\MethodRetry;
16+
use Temporal\Internal\Declaration\EntityNameValidator;
1617
use Temporal\Workflow\ReturnType;
1718
use Temporal\Workflow\WorkflowInit;
1819

@@ -43,6 +44,15 @@ final class WorkflowPrototype extends Prototype
4344
private ?ReturnType $returnType = null;
4445
private bool $hasInitializer = false;
4546

47+
public function __construct(
48+
string $name,
49+
?\ReflectionMethod $handler,
50+
\ReflectionClass $class,
51+
) {
52+
EntityNameValidator::validateWorkflow($name);
53+
parent::__construct($name, $handler, $class);
54+
}
55+
4656
/**
4757
* Indicates if the workflow has a constructor with {@see WorkflowInit} attribute.
4858
*/
@@ -88,6 +98,8 @@ public function setReturnType(?ReturnType $attribute): void
8898

8999
public function addQueryHandler(QueryDefinition $definition): void
90100
{
101+
EntityNameValidator::validateQueryMethod($definition->name);
102+
91103
$this->queryHandlers[$definition->name] = $definition;
92104
}
93105

@@ -101,6 +113,8 @@ public function getQueryHandlers(): array
101113

102114
public function addSignalHandler(SignalDefinition $definition): void
103115
{
116+
EntityNameValidator::validateSignalMethod($definition->name);
117+
104118
$this->signalHandlers[$definition->name] = $definition;
105119
}
106120

@@ -114,6 +128,8 @@ public function getSignalHandlers(): array
114128

115129
public function addUpdateHandler(UpdateDefinition $definition): void
116130
{
131+
EntityNameValidator::validateUpdateMethod($definition->name);
132+
117133
$this->updateHandlers[$definition->name] = $definition;
118134
}
119135

0 commit comments

Comments
 (0)