Skip to content

Commit

Permalink
Fix psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Apr 5, 2024
1 parent 3ff4de8 commit b9ab40b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/Factory/ActivityStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ final class ActivityStub
* completed, cancel accepted). {@see \Temporal\Activity\ActivityCancellationType}
*
* @return ($class is class-string ? T|ActivityProxy : ActivityStubInterface)
*
* @psalm-suppress LessSpecificReturnStatement,MoreSpecificReturnType
*/
public static function activity(
?string $class = null,
Expand Down Expand Up @@ -98,7 +100,7 @@ public static function activity(
$heartbeatTimeout === 0 or $options = $options->withHeartbeatTimeout($heartbeatTimeout);
// Activity ID
$activityId === null or $options = $options->withActivityId((string)$activityId);
$cancellationType === null or $options = $options->withCancellationType($cancellationType);
$cancellationType === 0 or $options = $options->withCancellationType($cancellationType);

return $class === null
? Workflow::newUntypedActivityStub($options)
Expand Down
42 changes: 32 additions & 10 deletions src/Factory/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ final class WorkflowStub
* @param list<mixed> $memo Specifies additional non-indexed information in result of list workflow.
*
* @return ($type is class-string ? T|WorkflowProxy : WorkflowStubInterface)
*
* @psalm-suppress LessSpecificReturnStatement,MoreSpecificReturnType
*/
public static function workflow(
WorkflowClientInterface $workflowClient,
Expand All @@ -97,8 +99,11 @@ public static function workflow(
array $searchAttributes = [],
array $memo = [],
): object {
$isUntyped = !\class_exists($type) && !\interface_exists($type);
$attributes = $isUntyped ? new AttributeCollection([]) : self::readAttributes($type);
$isTyped = self::isClassOrInterface($type);
/** @psalm-suppress ArgumentTypeCoercion */
$attributes = $isTyped
? self::readAttributes($type)
: new AttributeCollection([]);

// Retry options
$retryOptions = RetryOptions::create(
Expand Down Expand Up @@ -129,9 +134,10 @@ public static function workflow(
$searchAttributes === [] or $options = $options->withSearchAttributes($searchAttributes);
$memo === [] or $options = $options->withMemo($memo);

return $isUntyped
? $workflowClient->newUntypedWorkflowStub($type, $options)
: $workflowClient->newWorkflowStub($type, $options);
/** @psalm-suppress ArgumentTypeCoercion */
return $isTyped
? $workflowClient->newWorkflowStub($type, $options)
: $workflowClient->newUntypedWorkflowStub($type, $options);
}

/**
Expand Down Expand Up @@ -181,6 +187,8 @@ public static function workflow(
* @param list<mixed> $memo Specifies additional non-indexed information in result of list workflow.
*
* @return ($type is class-string ? T|ChildWorkflowProxy : ChildWorkflowStubInterface)
*
* @psalm-suppress LessSpecificReturnStatement,MoreSpecificReturnType
*/
public static function childWorkflow(
string $type,
Expand All @@ -202,8 +210,11 @@ public static function childWorkflow(
array $searchAttributes = [],
array $memo = [],
): object {
$isUntyped = !\class_exists($type) && !\interface_exists($type);
$attributes = $isUntyped ? new AttributeCollection([]) : self::readAttributes($type);
$isTyped = self::isClassOrInterface($type);
/** @psalm-suppress ArgumentTypeCoercion */
$attributes = $isTyped
? self::readAttributes($type)
: new AttributeCollection([]);

// Retry options
$retryOptions = RetryOptions::create(
Expand Down Expand Up @@ -239,9 +250,10 @@ public static function childWorkflow(
$searchAttributes === [] or $options = $options->withSearchAttributes($searchAttributes);
$memo === [] or $options = $options->withMemo($memo);

return $isUntyped
? Workflow::newUntypedChildWorkflowStub($type, $options)
: Workflow::newChildWorkflowStub($type, $options);
/** @psalm-suppress ArgumentTypeCoercion */
return $isTyped
? Workflow::newChildWorkflowStub($type, $options)
: Workflow::newUntypedChildWorkflowStub($type, $options);
}

/**
Expand All @@ -251,4 +263,14 @@ private static function readAttributes(string $class): AttributeCollection
{
return AttributeReader::collectionFromClass($class, [AttributeForWorkflow::class]);
}

/**
* @param non-empty-string $type
* @psalm-assert-if-true class-string $type
* @psalm-assert-if-false non-empty-string $type
*/
private static function isClassOrInterface(string $type): bool
{
return \class_exists($type) || \interface_exists($type);
}
}
11 changes: 5 additions & 6 deletions src/Internal/Attribute/AttributeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,19 @@ public function first(string $class): ?object
return $result;
}

public function has(string $class): bool
{
return $this->count($this->attributes[$class]) > 0;
}

/**
* @param class-string $class
* @return int<0, max>
*/
public function count(string $class): int
{
$this->makeIndex($class);
return \count($this->attributes[$class]);
return \count($this->attributes[$class] ?? []);
}

/**
* @param class-string $class
*/
private function makeIndex(string $class): void
{
if (\array_key_exists($class, $this->attributes)) {
Expand Down
3 changes: 3 additions & 0 deletions src/Internal/Attribute/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ private static function fetchAttributes(\ReflectionClass $reflection, array $fil
return $result;
}

/**
* @return list<class-string>
*/
private static function sortInterfaces(\ReflectionClass $class): array
{
$result = [];
Expand Down
1 change: 1 addition & 0 deletions src/Internal/RetryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static function create(
$retryAttempts > 0 and $retryOptions = $retryOptions->withMaximumAttempts($retryAttempts);
$retryInitInterval === null or $retryOptions = $retryOptions->withInitialInterval($retryInitInterval);
$retryMaxInterval === null or $retryOptions = $retryOptions->withMaximumInterval($retryMaxInterval);
/** @psalm-suppress PossiblyNullArgument */
$retryBackoff >= 1.0 and $retryOptions = $retryOptions->withBackoffCoefficient($retryBackoff);
$nonRetryables === [] or $retryOptions = $retryOptions->withNonRetryableExceptions($nonRetryables);
return $retryOptions;
Expand Down
2 changes: 2 additions & 0 deletions src/VirtualPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* }
* }
* ```
*
* @psalm-suppress TooManyTemplateParams
*/
interface VirtualPromise extends PromiseInterface
{
Expand Down

0 comments on commit b9ab40b

Please sign in to comment.