From 631b0ba2b5103254e78ce0f5c099977709d77a2c Mon Sep 17 00:00:00 2001 From: SF Date: Wed, 22 Apr 2026 19:35:14 +0800 Subject: [PATCH] fix: UTC DateTime + remove dead compensation code - Replace all bare `new \DateTime` (7 occurrences) with `new \DateTime('now', new \DateTimeZone('UTC'))` across WorkflowEngine, WorkflowInstance, LogAction, DelayAction. - Remove Step::$compensationAction, getCompensationAction(), hasCompensation(), toArray() 'compensation' key, and the compensationAction named arg in WorkflowDefinition::processSteps(). Saga/rollback is planned for a future version. - 116 package tests all pass. --- src/Actions/DelayAction.php | 2 +- src/Actions/LogAction.php | 2 +- src/Core/Step.php | 24 ------------------------ src/Core/WorkflowDefinition.php | 1 - src/Core/WorkflowEngine.php | 4 ++-- src/Core/WorkflowInstance.php | 20 ++++++++++---------- 6 files changed, 14 insertions(+), 39 deletions(-) diff --git a/src/Actions/DelayAction.php b/src/Actions/DelayAction.php index c68259d..b157c1b 100644 --- a/src/Actions/DelayAction.php +++ b/src/Actions/DelayAction.php @@ -43,7 +43,7 @@ protected function doExecute(WorkflowContext $context): ActionResult return ActionResult::success([ 'delayed_seconds' => $seconds, 'delayed_microseconds' => $microseconds, - 'delayed_at' => (new \DateTime)->format('c'), + 'delayed_at' => (new \DateTime('now', new \DateTimeZone('UTC')))->format('c'), ]); } } diff --git a/src/Actions/LogAction.php b/src/Actions/LogAction.php index 19b0dab..00c42e5 100644 --- a/src/Actions/LogAction.php +++ b/src/Actions/LogAction.php @@ -39,7 +39,7 @@ protected function doExecute(WorkflowContext $context): ActionResult return ActionResult::success([ 'logged_message' => $processedMessage, - 'logged_at' => (new \DateTime)->format('c'), + 'logged_at' => (new \DateTime('now', new \DateTimeZone('UTC')))->format('c'), ]); } diff --git a/src/Core/Step.php b/src/Core/Step.php index 68e078e..abb4be8 100644 --- a/src/Core/Step.php +++ b/src/Core/Step.php @@ -16,7 +16,6 @@ * - **Configuration**: Step-specific parameters and settings * - **Resilience**: Timeout and retry configuration for robust execution * - **Conditional Logic**: Support for conditional step execution - * - **Compensation**: Rollback actions for error scenarios * * ## Usage Examples * @@ -62,7 +61,6 @@ class Step * @param array $config Step-specific configuration parameters * @param string|null $timeout Maximum execution time (in seconds as string) * @param int $retryAttempts Number of retry attempts on failure (0-10) - * @param string|null $compensationAction Action class for rollback scenarios * @param array $conditions Array of condition expressions for conditional execution * @param array $prerequisites Array of prerequisite step IDs that must complete first */ @@ -72,7 +70,6 @@ public function __construct( private readonly array $config = [], private readonly ?string $timeout = null, private readonly int $retryAttempts = 0, - private readonly ?string $compensationAction = null, private readonly array $conditions = [], private readonly array $prerequisites = [] ) {} @@ -127,16 +124,6 @@ public function getRetryAttempts(): int return $this->retryAttempts; } - /** - * Get the compensation action class for rollback scenarios. - * - * @return string|null Compensation action class name, or null if none - */ - public function getCompensationAction(): ?string - { - return $this->compensationAction; - } - /** * Get the conditional expressions for this step. * @@ -167,16 +154,6 @@ public function hasAction(): bool return $this->actionClass !== null; } - /** - * Check if this step has a compensation action for rollback. - * - * @return bool True if a compensation action is defined - */ - public function hasCompensation(): bool - { - return $this->compensationAction !== null; - } - /** * Determine if this step can execute based on its conditions. * @@ -244,7 +221,6 @@ public function toArray(): array 'config' => $this->config, 'timeout' => $this->timeout, 'retry_attempts' => $this->retryAttempts, - 'compensation' => $this->compensationAction, 'conditions' => $this->conditions, 'prerequisites' => $this->prerequisites, ]; diff --git a/src/Core/WorkflowDefinition.php b/src/Core/WorkflowDefinition.php index d5804aa..27313bf 100644 --- a/src/Core/WorkflowDefinition.php +++ b/src/Core/WorkflowDefinition.php @@ -303,7 +303,6 @@ private function processSteps(array $stepsData): array config: $stepData['parameters'] ?? $stepData['config'] ?? [], timeout: $stepData['timeout'] ?? null, retryAttempts: $stepData['retry_attempts'] ?? 0, - compensationAction: $stepData['compensation'] ?? null, conditions: $stepData['conditions'] ?? [], prerequisites: $stepData['prerequisites'] ?? [] ); diff --git a/src/Core/WorkflowEngine.php b/src/Core/WorkflowEngine.php index 9852da2..4eceb94 100644 --- a/src/Core/WorkflowEngine.php +++ b/src/Core/WorkflowEngine.php @@ -124,8 +124,8 @@ public function start(string $workflowId, array $definition, array $context = [] definition: $workflowDef, state: WorkflowState::PENDING, data: $context, - createdAt: new \DateTime, - updatedAt: new \DateTime + createdAt: new \DateTime('now', new \DateTimeZone('UTC')), + updatedAt: new \DateTime('now', new \DateTimeZone('UTC')) ); // Save initial state diff --git a/src/Core/WorkflowInstance.php b/src/Core/WorkflowInstance.php index 811e003..f4f8cb7 100644 --- a/src/Core/WorkflowInstance.php +++ b/src/Core/WorkflowInstance.php @@ -136,8 +136,8 @@ public function __construct( ) { $this->state = $state; $this->data = $data; - $this->createdAt = $createdAt ?? new \DateTime; - $this->updatedAt = $updatedAt ?? new \DateTime; + $this->createdAt = $createdAt ?? new \DateTime('now', new \DateTimeZone('UTC')); + $this->updatedAt = $updatedAt ?? new \DateTime('now', new \DateTimeZone('UTC')); } /** @@ -187,7 +187,7 @@ public function setState(WorkflowState $state): void } $this->state = $state; - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } /** @@ -208,7 +208,7 @@ public function getData(): array public function setData(array $data): void { $this->data = $data; - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } /** @@ -219,7 +219,7 @@ public function setData(array $data): void public function mergeData(array $data): void { $this->data = array_merge($this->data, $data); - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } /** @@ -240,7 +240,7 @@ public function getCurrentStepId(): ?string public function setCurrentStepId(?string $stepId): void { $this->currentStepId = $stepId; - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } /** @@ -262,7 +262,7 @@ public function addCompletedStep(string $stepId): void { if (! in_array($stepId, $this->completedSteps)) { $this->completedSteps[] = $stepId; - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } } @@ -287,9 +287,9 @@ public function addFailedStep(string $stepId, string $error): void $this->failedSteps[] = [ 'step_id' => $stepId, 'error' => $error, - 'failed_at' => (new \DateTime)->format('c'), + 'failed_at' => (new \DateTime('now', new \DateTimeZone('UTC')))->format('c'), ]; - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } /** @@ -310,7 +310,7 @@ public function getErrorMessage(): ?string public function setErrorMessage(?string $errorMessage): void { $this->errorMessage = $errorMessage; - $this->updatedAt = new \DateTime; + $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC')); } /**