Skip to content

Commit 919945d

Browse files
committed
optimize warning output
1 parent 8132770 commit 919945d

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/State/ExecutionLog/ExecutionLog.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class ExecutionLog
1818
private array $stepInfo = [];
1919
/** @var string[][] Collect all warnings which occurred during the workflow execution */
2020
private array $warnings = [];
21+
private int $warningsDuringStep = 0;
22+
2123
private float $startAt;
24+
2225
private WorkflowState $workflowState;
2326

2427
public function __construct(WorkflowState $workflowState)
@@ -29,8 +32,9 @@ public function __construct(WorkflowState $workflowState)
2932
public function addStep(int $stage, string $step, string $state, ?string $reason): void {
3033
$stage = $this->mapStage($stage);
3134

32-
$this->stages[$stage][] = new Step($step, $state, $reason, $this->stepInfo);
35+
$this->stages[$stage][] = new Step($step, $state, $reason, $this->stepInfo, $this->warningsDuringStep);
3336
$this->stepInfo = [];
37+
$this->warningsDuringStep = 0;
3438
}
3539

3640
public function __toString(): string
@@ -56,6 +60,7 @@ public function attachStepInfo(string $info): void
5660
public function addWarning(string $message): void
5761
{
5862
$this->warnings[$this->mapStage($this->workflowState->getStage())][] = $message;
63+
$this->warningsDuringStep++;
5964
}
6065

6166
public function startExecution(): void
@@ -68,11 +73,15 @@ public function stopExecution(): void
6873
$this->attachStepInfo("Execution time: " . number_format(1000 * (microtime(true) - $this->startAt), 5) . 'ms');
6974

7075
if ($this->warnings) {
71-
$warnings = "Got " . count($this->warnings, COUNT_RECURSIVE) . " warnings during the execution:\n";
76+
$warnings = sprintf(
77+
"Got %s warning%s during the execution:\n ",
78+
$amount = count($this->warnings, COUNT_RECURSIVE) - count($this->warnings),
79+
$amount > 1 ? 's' : '',
80+
);
7281

7382
foreach ($this->warnings as $stage => $stageWarnings) {
7483
$warnings .= implode(
75-
"\n ",
84+
"\n ",
7685
array_map(
7786
fn (string $warning): string => sprintf("%s: %s", $stage, $warning),
7887
$stageWarnings,

src/State/ExecutionLog/Step.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ class Step
1010
private string $state;
1111
private ?string $reason;
1212
private array $stepInfo;
13+
private int $warnings;
1314

14-
public function __construct(string $step, string $state, ?string $reason, array $stepInfo)
15+
public function __construct(string $step, string $state, ?string $reason, array $stepInfo, int $warnings)
1516
{
1617
$this->step = $step;
1718
$this->state = $state;
1819
$this->reason = $reason;
1920
$this->stepInfo = $stepInfo;
21+
$this->warnings = $warnings;
2022
}
2123

2224
public function __toString(): string
2325
{
24-
$stepLog = "{$this->step}: {$this->state}" . ($this->reason ? " ({$this->reason})" : '');
26+
$stepLog = "{$this->step}: {$this->state}" .
27+
($this->reason ? " ({$this->reason})" : '') .
28+
($this->warnings ? " ({$this->warnings} warning" . ($this->warnings > 1 ? 's' : '') . ")" : '');
2529

2630
foreach ($this->stepInfo as $info) {
2731
$stepLog .= "\n - $info";

src/WorkflowControl.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PHPWorkflow;
66

7+
use Exception;
78
use PHPWorkflow\Exception\WorkflowControl\FailStepException;
89
use PHPWorkflow\Exception\WorkflowControl\FailWorkflowException;
910
use PHPWorkflow\Exception\WorkflowControl\SkipStepException;
@@ -80,9 +81,21 @@ public function attachStepInfo(string $info): void
8081
* All warnings will be collected and shown in the workflow debug log.
8182
*
8283
* @param string $message
84+
* @param Exception|null $exception An exception causing the warning
85+
* (if provided exception information will be added to the warning)
8386
*/
84-
public function warning(string $message): void
87+
public function warning(string $message, ?Exception $exception = null): void
8588
{
89+
if ($exception) {
90+
$message .= sprintf(
91+
" (%s%s in %s::%s)",
92+
trim(strrchr(get_class($exception), '\\'), '\\'),
93+
($exception->getMessage() ? ": {$exception->getMessage()}" : ''),
94+
$exception->getFile(),
95+
$exception->getLine(),
96+
);
97+
}
98+
8699
$this->executionLog->addWarning($message);
87100
}
88101
}

0 commit comments

Comments
 (0)