Skip to content

Commit

Permalink
Use AttributeCollection instead of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 20, 2024
1 parent 0034c15 commit af78726
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/Factory/ActivityStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Temporal\Internal\Workflow\ActivityProxy;
use Temporal\Sugar\Attribute\RetryPolicy;
use Temporal\Sugar\Attribute\TaskQueue;
use Temporal\Sugar\Internal\Attribute\AttributeCollection;
use Temporal\Sugar\Internal\Attribute\AttributeForActivity;
use Temporal\Sugar\Internal\Attribute\AttributeReader;
use Temporal\Sugar\Internal\RetryOptions;
Expand Down Expand Up @@ -82,12 +83,12 @@ public static function activity(
retryMaxInterval: $retryMaxInterval,
retryBackoff: $retryBackoff,
nonRetryables: $nonRetryables,
attribute: $attributes[RetryPolicy::class] ?? null,
attribute: $attributes->first(RetryPolicy::class),
);

$options = ActivityOptions::new()->withRetryOptions($retryOptions);

$taskQueue ??= isset($attributes[TaskQueue::class][0]) ? $attributes[TaskQueue::class][0]->name : null;
$taskQueue ??= $attributes->first(TaskQueue::class)?->name;
$taskQueue === null or $options = $options->withTaskQueue($taskQueue);
// Timeouts
$scheduleToStartTimeout === 0 or $options = $options->withScheduleToStartTimeout($scheduleToStartTimeout);
Expand All @@ -102,11 +103,10 @@ public static function activity(
}

/**
* @param class-string $class
* @return array<class-string<AttributeForActivity>, AttributeForActivity>
* @param class-string $class Activity class name.
*/
private static function readAttributes(string $class): array
private static function readAttributes(string $class): AttributeCollection
{
return AttributeReader::fromClass($class, [AttributeForActivity::class]);
return AttributeReader::collectionFromClass($class, [AttributeForActivity::class]);
}
}
15 changes: 9 additions & 6 deletions src/Factory/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Temporal\Internal\Client\WorkflowProxy;
use Temporal\Internal\Workflow\ChildWorkflowProxy;
use Temporal\Sugar\Attribute\RetryPolicy;
use Temporal\Sugar\Attribute\TaskQueue;
use Temporal\Sugar\Internal\Attribute\AttributeCollection;
use Temporal\Sugar\Internal\Attribute\AttributeForWorkflow;
use Temporal\Sugar\Internal\Attribute\AttributeReader;
use Temporal\Sugar\Internal\RetryOptions;
Expand Down Expand Up @@ -102,11 +104,11 @@ public static function workflow(
retryMaxInterval: $retryMaxInterval,
retryBackoff: $retryBackoff,
nonRetryables: $nonRetryables,
attribute: $attributes[RetryPolicy::class] ?? null,
attribute: $attributes->first(RetryPolicy::class),
);

$options = WorkflowOptions::new()->withRetryOptions($retryOptions);
$taskQueue === null or $options = $options->withTaskQueue($taskQueue);
$taskQueue ??= $attributes->first(TaskQueue::class)?->name;
// Start options
$startDelay === 0 or $options = $options->withWorkflowStartDelay($startDelay);
$eagerStart and $options = $options->withEagerStart(true);
Expand Down Expand Up @@ -201,10 +203,12 @@ public static function childWorkflow(
$retryMaxInterval,
$retryBackoff,
$nonRetryables,
attribute: $attributes->first(RetryPolicy::class),

Check failure on line 206 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

UndefinedVariable

src/Factory/WorkflowStub.php:206:24: UndefinedVariable: Cannot find referenced variable $attributes (see https://psalm.dev/024)

Check failure on line 206 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedArgument

src/Factory/WorkflowStub.php:206:24: MixedArgument: Argument 6 of Temporal\Sugar\Internal\RetryOptions::create cannot be mixed, expecting Temporal\Sugar\Attribute\RetryPolicy|null (see https://psalm.dev/030)

Check failure on line 206 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedMethodCall

src/Factory/WorkflowStub.php:206:37: MixedMethodCall: Cannot determine the type of $attributes when calling method first (see https://psalm.dev/015)
);

$options = Workflow\ChildWorkflowOptions::new()->withRetryOptions($retryOptions);

$taskQueue ??= $attributes->first(TaskQueue::class)?->name;

Check failure on line 211 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedAssignment

src/Factory/WorkflowStub.php:211:9: MixedAssignment: Unable to determine the type that $taskQueue is being assigned to (see https://psalm.dev/032)

Check failure on line 211 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

UndefinedVariable

src/Factory/WorkflowStub.php:211:24: UndefinedVariable: Cannot find referenced variable $attributes (see https://psalm.dev/024)

Check failure on line 211 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedPropertyFetch

src/Factory/WorkflowStub.php:211:24: MixedPropertyFetch: Cannot fetch property on mixed var $__tmp_nullsafe__11617 (see https://psalm.dev/034)

Check failure on line 211 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedMethodCall

src/Factory/WorkflowStub.php:211:37: MixedMethodCall: Cannot determine the type of $attributes when calling method first (see https://psalm.dev/015)
$taskQueue === null or $options = $options->withTaskQueue($taskQueue);

Check failure on line 212 in src/Factory/WorkflowStub.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

MixedArgument

src/Factory/WorkflowStub.php:212:67: MixedArgument: Argument 1 of Temporal\Workflow\ChildWorkflowOptions::withTaskQueue cannot be mixed|non-empty-string, expecting string (see https://psalm.dev/030)
$namespace === null or $options = $options->withNamespace($namespace);
// Start and close options
Expand All @@ -230,11 +234,10 @@ public static function childWorkflow(
}

/**
* @param class-string $class
* @return array<class-string<AttributeForWorkflow>, AttributeForWorkflow>
* @param class-string $class Workflow class name.
*/
private static function readAttributes(string $class): array
private static function readAttributes(string $class): AttributeCollection
{
return AttributeReader::fromClass($class, [AttributeForWorkflow::class]);
return AttributeReader::collectionFromClass($class, [AttributeForWorkflow::class]);
}
}
20 changes: 17 additions & 3 deletions src/Internal/Attribute/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
*/
final class AttributeReader
{
/**
* @param class-string $class
* @param list<class-string> $attributes
*/
public static function collectionFromClass(
string $class,
array $attributes,
bool $merge = true,
bool $inheritance = true,
bool $interfaces = true,
): AttributeCollection {
return new AttributeCollection(self::arrayFromClass($class, $attributes, $merge, $inheritance, $interfaces));
}

/**
* @param class-string $class
* @param list<class-string> $attributes
*
* @return array<class-string, list<object>>
*/
public static function fromClass(
public static function arrayFromClass(
string $class,
array $attributes,
bool $merge = true,
Expand All @@ -34,7 +48,7 @@ public static function fromClass(
}

if ($parent = $reflection->getParentClass()) {
$attrs = self::fromClass($parent->getName(), $attributes, $merge, true, false);
$attrs = self::arrayFromClass($parent->getName(), $attributes, $merge, true, false);
$result = $merge
? \array_merge_recursive($result, $attrs)
: $result + $attrs;
Expand All @@ -45,7 +59,7 @@ public static function fromClass(
}

foreach (self::sortInterfaces($reflection) as $interface) {
$attrs = self::fromClass($interface, $attributes, $merge, false, false);
$attrs = self::arrayFromClass($interface, $attributes, $merge, false, false);
$result = $merge
? \array_merge_recursive($result, $attrs)
: $result + $attrs;
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Internal/AttributeReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AttributeReaderTest extends TestCase
{
public function testFromClass(): void
{
$result = AttributeReader::fromClass(
$result = AttributeReader::arrayFromClass(
Stub\Attributed\SimpleClass::class,
[TaskQueue::class]
);
Expand All @@ -26,7 +26,7 @@ public function testFromClass(): void

public function testFromExtendedClassWithInheritanceWithMerge(): void
{
$result = AttributeReader::fromClass(
$result = AttributeReader::arrayFromClass(
Stub\Attributed\ExtendedAttributed::class,
[TaskQueue::class],
merge: true,
Expand All @@ -47,7 +47,7 @@ public function testFromExtendedClassWithInheritanceWithMerge(): void

public function testFromInterfaceWithInheritance(): void
{
$result = AttributeReader::fromClass(
$result = AttributeReader::arrayFromClass(
Stub\Attributed\InterfaceAttributed::class,
[TaskQueue::class],
merge: true,
Expand Down

0 comments on commit af78726

Please sign in to comment.