• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wol-soft / php-workflow / 12009837953

25 Nov 2024 12:16PM UTC coverage: 98.408%. Remained the same
12009837953

push

github

wol-soft
PHP8.4 compatibility

371 of 377 relevant lines covered (98.41%)

38.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/src/State/ExecutionLog/ExecutionLog.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace PHPWorkflow\State\ExecutionLog;
6

7
use PHPWorkflow\State\ExecutionLog\OutputFormat\OutputFormat;
8
use PHPWorkflow\State\WorkflowState;
9
use PHPWorkflow\Step\WorkflowStep;
10

11
class ExecutionLog
12
{
13
    const STATE_SUCCESS = 'ok';
14
    const STATE_SKIPPED = 'skipped';
15
    const STATE_FAILED = 'failed';
16

17
    /** @var Step[][] */
18
    private array $stages = [];
19
    /** @var StepInfo[] Collect additional debug info concerning the current step */
20
    private array $stepInfo = [];
21
    /** @var string[][] Collect all warnings which occurred during the workflow execution */
22
    private array $warnings = [];
23
    private int $warningsDuringStep = 0;
24

25
    private float $startAt;
26

27
    private WorkflowState $workflowState;
28

29
    public function __construct(WorkflowState $workflowState)
30
    {
31
        $this->workflowState = $workflowState;
102✔
32
    }
33

34
    public function addStep(int $stage, Describable $step, string $state, ?string $reason): void {
35
        $this->stages[$stage][] = new Step($step, $state, $reason, $this->stepInfo, $this->warningsDuringStep);
102✔
36
        $this->stepInfo = [];
102✔
37
        $this->warningsDuringStep = 0;
102✔
38
    }
39

40
    public function debug(OutputFormat $formatter)
41
    {
42
        return $formatter->format($this->workflowState->getWorkflowName(), $this->stages);
102✔
43
    }
44

45
    public function attachStepInfo(string $info, array $context = []): void
46
    {
47
        $this->stepInfo[] = new StepInfo($info, $context);
102✔
48
    }
49

50
    public function addWarning(string $message, bool $workflowReportWarning = false): void
51
    {
52
        $this->warnings[$this->workflowState->getStage()][] = $message;
12✔
53

54
        if (!$workflowReportWarning) {
12✔
55
            $this->warningsDuringStep++;
6✔
56
        }
57
    }
58

59
    public function startExecution(): void
60
    {
61
        $this->startAt = microtime(true);
102✔
62
    }
63

64
    public function stopExecution(): void
65
    {
66
        $this->attachStepInfo('Execution time: ' . number_format(1000 * (microtime(true) - $this->startAt), 5) . 'ms');
102✔
67

68
        if ($this->warnings) {
102✔
69
            $warnings = sprintf(
12✔
70
                'Got %s warning%s during the execution:',
12✔
71
                $amount = count($this->warnings, COUNT_RECURSIVE) - count($this->warnings),
12✔
72
                $amount > 1 ? 's' : '',
12✔
73
            );
12✔
74

75
            foreach ($this->warnings as $stage => $stageWarnings) {
12✔
76
                $warnings .= implode(
12✔
77
                    '',
12✔
78
                    array_map(
12✔
79
                        fn (string $warning): string =>
12✔
80
                            sprintf(PHP_EOL . '        %s: %s', self::mapStage($stage), $warning),
12✔
81
                        $stageWarnings,
12✔
82
                    ),
12✔
83
                );
12✔
84
            }
85

86
            $this->attachStepInfo($warnings);
12✔
87
        }
88
    }
89

90
    public static function mapStage(int $stage): string
91
    {
92
        switch ($stage) {
93
            case WorkflowState::STAGE_PREPARE: return 'Prepare';
3✔
94
            case WorkflowState::STAGE_VALIDATE: return 'Validate';
10✔
95
            case WorkflowState::STAGE_BEFORE: return 'Before';
13✔
96
            case WorkflowState::STAGE_PROCESS: return 'Process';
89✔
97
            case WorkflowState::STAGE_ON_ERROR: return 'On Error';
6✔
98
            case WorkflowState::STAGE_ON_SUCCESS: return 'On Success';
5✔
99
            case WorkflowState::STAGE_AFTER: return 'After';
11✔
100
            case WorkflowState::STAGE_SUMMARY: return 'Summary';
102✔
101
        }
102
    }
103

104
    public function getWarnings(): array
105
    {
106
        return $this->warnings;
13✔
107
    }
108

109
    public function getLastStep(): WorkflowStep
110
    {
111
        return $this->workflowState->getCurrentStep();
×
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc