• 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

96.88
/src/ProgressBars/AbstractSymfonyProgressBar.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 JBZoo\Utils\Arr;
20
use JBZoo\Utils\Dates;
21
use JBZoo\Utils\FS;
22
use JBZoo\Utils\Stats;
23
use JBZoo\Utils\Sys;
24
use Symfony\Component\Console\Helper\Helper as SymfonyHelper;
25
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar;
26

27
abstract class AbstractSymfonyProgressBar extends AbstractProgressBar
28
{
29
    /** @deprecated Use `throw new ExceptionBreak("Reason.")` */
30
    public const BREAK = ExceptionBreak::MESSAGE;
31

32
    public const MAX_LINE_LENGTH = 120;
33

34
    /** @var int[] */
35
    protected array $stepMemoryDiff = [];
36

37
    /** @var float[] */
38
    protected array $stepTimers = [];
39

40
    public static function setPlaceholder(string $name, callable $callable): void
41
    {
42
        SymfonyProgressBar::setPlaceholderFormatterDefinition($name, $callable);
16✔
43
    }
44

45
    /**
46
     * @SuppressWarnings(PHPMD.NPathComplexity)
47
     */
48
    protected function configureProgressBar(bool $optimizeMode = true): void
49
    {
50
        self::setPlaceholder(
16✔
51
            'jbzoo_time_elapsed',
8✔
52
            static fn (SymfonyProgressBar $bar): string => Dates::formatTime(\time() - $bar->getStartTime()),
16✔
53
        );
8✔
54

55
        self::setPlaceholder('jbzoo_time_estimated', static function (SymfonyProgressBar $bar): string {
16✔
56
            if ($bar->getMaxSteps() === 0) {
16✔
57
                return 'n/a';
×
58
            }
59

60
            if ($bar->getProgress() === 0) {
16✔
61
                $estimated = 0;
16✔
UNCOV
62
            } else {
63
                $estimated = \round((\time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
16✔
64
            }
65

66
            return Dates::formatTime($estimated);
16✔
67
        });
8✔
68

69
        self::setPlaceholder(
16✔
70
            'jbzoo_memory_current',
8✔
71
            static fn (): string => SymfonyHelper::formatMemory(\memory_get_usage(false)),
16✔
72
        );
8✔
73

74
        // Memory/Time optimizations
75
        if ($optimizeMode) {
16✔
76
            return;
12✔
77
        }
78

79
        self::setPlaceholder(
4✔
80
            'jbzoo_memory_peak',
2✔
81
            static fn (): string => SymfonyHelper::formatMemory(\memory_get_peak_usage(false)),
4✔
82
        );
2✔
83

84
        self::setPlaceholder('jbzoo_memory_limit', static fn (): string => Sys::iniGet('memory_limit'));
4✔
85

86
        self::setPlaceholder('jbzoo_memory_step_median', function (SymfonyProgressBar $bar): string {
4✔
UNCOV
87
            if (
88
                $bar->getMaxSteps() === 0
4✔
89
                || $bar->getProgress() === 0
4✔
90
                || \count($this->stepMemoryDiff) === 0
4✔
91
            ) {
92
                return 'n/a';
4✔
93
            }
94

95
            return FS::format((int)Stats::median($this->stepMemoryDiff));
4✔
96
        });
2✔
97

98
        self::setPlaceholder('jbzoo_memory_step_last', function (SymfonyProgressBar $bar): string {
4✔
UNCOV
99
            if (
100
                $bar->getMaxSteps() === 0
4✔
101
                || $bar->getProgress() === 0
4✔
102
                || \count($this->stepMemoryDiff) === 0
4✔
103
            ) {
104
                return 'n/a';
4✔
105
            }
106

107
            return FS::format(Arr::last($this->stepMemoryDiff));
4✔
108
        });
2✔
109

110
        self::setPlaceholder('jbzoo_time_remaining', static function (SymfonyProgressBar $bar): string {
4✔
111
            if ($bar->getMaxSteps() === 0) {
4✔
112
                return 'n/a';
×
113
            }
114

115
            if ($bar->getProgress() === 0) {
4✔
116
                $remaining = 0;
4✔
UNCOV
117
            } else {
118
                $remaining = \round(
4✔
119
                    (\time() - $bar->getStartTime())
4✔
120
                    / $bar->getProgress()
4✔
121
                    * ($bar->getMaxSteps() - $bar->getProgress()),
4✔
122
                );
2✔
123
            }
124

125
            return Dates::formatTime($remaining);
4✔
126
        });
2✔
127

128
        self::setPlaceholder('jbzoo_time_step_median', function (SymfonyProgressBar $bar): string {
4✔
UNCOV
129
            if (
130
                $bar->getMaxSteps() === 0
4✔
131
                || $bar->getProgress() === 0
4✔
132
                || \count($this->stepTimers) === 0
4✔
133
            ) {
134
                return 'n/a';
4✔
135
            }
136

137
            return \str_replace('±', '<blue>±</blue>', Stats::renderMedian($this->stepTimers)) . ' sec';
4✔
138
        });
2✔
139

140
        self::setPlaceholder('jbzoo_time_step_last', function (SymfonyProgressBar $bar): string {
4✔
UNCOV
141
            if (
142
                $bar->getMaxSteps() === 0
4✔
143
                || $bar->getProgress() === 0
4✔
144
                || \count($this->stepTimers) === 0
4✔
145
            ) {
146
                return 'n/a';
4✔
147
            }
148

149
            return Dates::formatTime(Arr::last($this->stepTimers));
4✔
150
        });
2✔
151
    }
152
}
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