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

JBZoo / Cli / 7388213582

12 Dec 2023 10:40PM UTC coverage: 83.803% (-0.05%) from 83.848%
7388213582

push

github

web-flow
Feature flag to add `timestamp_real` (#23)

2 of 3 new or added lines in 1 file covered. (66.67%)

18 existing lines in 3 files now uncovered.

952 of 1136 relevant lines covered (83.8%)

138.74 hits per line

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

92.98
/src/ProgressBars/ProgressBarLight.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Cli.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Cli
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Cli\ProgressBars;
18

19
use function JBZoo\Utils\isStrEmpty;
20

21
class ProgressBarLight extends AbstractSymfonyProgressBar
22
{
23
    public function execute(): bool
24
    {
25
        if (!$this->init()) {
76✔
26
            return false;
12✔
27
        }
28

29
        if ($this->callbackOnStart !== null) {
64✔
30
            \call_user_func($this->callbackOnStart, $this);
×
31
        }
32

33
        $exceptionMessages = [];
64✔
34
        $currentIndex      = 0;
64✔
35

36
        foreach ($this->list as $stepIndex => $stepValue) {
64✔
37
            [$stepResult, $exceptionMessage] = $this->handleOneStep($stepValue, $stepIndex, $currentIndex);
64✔
38

39
            $exceptionMessages = \array_merge($exceptionMessages, (array)$exceptionMessage);
60✔
40

41
            $this->outputMode->_($stepResult);
60✔
42

43
            if (\is_string($stepResult) && \str_contains($stepResult, ExceptionBreak::MESSAGE)) {
60✔
44
                break;
8✔
45
            }
46

47
            $currentIndex++;
60✔
48
        }
49

50
        if ($this->callbackOnFinish !== null) {
56✔
51
            \call_user_func($this->callbackOnFinish, $this);
×
52
        }
53

54
        self::showListOfExceptions($exceptionMessages);
56✔
55

56
        return true;
48✔
57
    }
58

59
    private function init(): bool
60
    {
61
        $progresBarLevel = $this->getNestedLevel();
76✔
62
        $levelPostfix    = $progresBarLevel > 1 ? " Level: {$progresBarLevel}." : '';
76✔
63

64
        if ($this->max <= 0) {
76✔
65
            if (isStrEmpty($this->title)) {
12✔
66
                $this->outputMode->_("Number of items is 0 or less.{$levelPostfix}");
×
UNCOV
67
            } else {
68
                $this->outputMode->_("{$this->title}. Number of items is 0 or less.{$levelPostfix}");
12✔
69
            }
70

71
            return false;
12✔
72
        }
73

74
        if (isStrEmpty($this->title)) {
64✔
75
            $this->outputMode->_("Number of steps: {$this->max}.{$levelPostfix}");
4✔
UNCOV
76
        } else {
77
            $this->outputMode->_("Working on \"{$this->title}\". Number of steps: {$this->max}.{$levelPostfix}");
60✔
78
        }
79

80
        return true;
64✔
81
    }
82

83
    private function handleOneStep(mixed $stepValue, float|int|string $stepIndex, int $currentIndex): array
84
    {
85
        if ($this->callback === null) {
64✔
86
            throw new Exception('Callback function is not defined');
×
87
        }
88

89
        $exceptionMessage = null;
64✔
90

91
        $humanIndex    = $currentIndex + 1;
64✔
92
        $prefixMessage = $stepIndex === $currentIndex
64✔
93
            ? "Step={$humanIndex}/Max={$this->max}"
52✔
94
            : "Key={$currentIndex}/Step={$humanIndex}/Max={$this->max}";
12✔
95

96
        $callbackResults = [];
64✔
97

98
        $this->outputMode->catchModeStart();
64✔
99

UNCOV
100
        try {
101
            $callbackResults = (array)($this->callback)($stepValue, $stepIndex, $currentIndex);
64✔
102
        } catch (ExceptionBreak $exception) {
20✔
103
            $callbackResults[] = ExceptionBreak::MESSAGE . ' ' . $exception->getMessage();
4✔
104
        } catch (\Exception $exception) {
16✔
105
            if ($this->throwBatchException) {
16✔
106
                $errorMessage      = 'Exception: ' . $exception->getMessage();
8✔
107
                $callbackResults[] = $errorMessage;
8✔
108
                $exceptionMessage  = "({$prefixMessage}): {$errorMessage}";
8✔
UNCOV
109
            } else {
110
                throw $exception;
8✔
111
            }
112
        }
113

114
        $caughtMessages = $this->outputMode->catchModeFinish();
60✔
115
        if (\count($caughtMessages) > 0) {
60✔
116
            $callbackResults = \array_merge($callbackResults, $caughtMessages);
4✔
117
        }
118

119
        // Handle status messages
120
        if (\count($callbackResults) > 0) {
60✔
121
            $stepResult = "({$prefixMessage}): " . \implode('; ', $callbackResults);
52✔
UNCOV
122
        } else {
123
            $stepResult = "({$prefixMessage}): Empty Output";
20✔
124
        }
125

126
        return [$stepResult, $exceptionMessage];
60✔
127
    }
128

129
    private static function showListOfExceptions(array $exceptionMessages): void
130
    {
131
        if (\count($exceptionMessages) > 0) {
56✔
132
            $listOfErrors = \implode('; ', $exceptionMessages);
8✔
133
            $listOfErrors = \str_replace('Exception: ', '', $listOfErrors);
8✔
134

135
            throw new Exception('BatchExceptions: ' . $listOfErrors);
8✔
136
        }
137
    }
138
}
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

© 2025 Coveralls, Inc