1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Formatters; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Pretty; |
6
|
|
|
use AlecRabbit\Tools\Formattable; |
7
|
|
|
use AlecRabbit\Tools\Formatters\Core\ReportFormatter; |
8
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
9
|
|
|
use Carbon\CarbonInterval; |
10
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
11
|
|
|
|
12
|
|
|
class TimerReportFormatter extends ReportFormatter |
13
|
|
|
{ |
14
|
|
|
protected const MILLISECONDS_THRESHOLD = 10000; |
15
|
|
|
|
16
|
|
|
/** {@inheritdoc} */ |
17
|
|
|
public function process(Formattable $formattable): string |
18
|
|
|
{ |
19
|
|
|
if ($formattable instanceof TimerReport) { |
20
|
|
|
if (0 === $formattable->getCount() && DEFAULT_NAME === $formattable->getName()) { |
21
|
|
|
return $this->simple($formattable); |
22
|
|
|
} |
23
|
|
|
return $this->full($formattable); |
24
|
|
|
} |
25
|
|
|
$this->wrongFormattableType(TimerReport::class, $formattable); |
26
|
|
|
// @codeCoverageIgnoreStart |
27
|
|
|
return ''; // never executes |
28
|
|
|
// @codeCoverageIgnoreEnd |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param TimerReport $report |
33
|
|
|
* @param bool $eol |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected function simple(TimerReport $report, bool $eol = true): string |
37
|
|
|
{ |
38
|
|
|
return |
39
|
|
|
sprintf( |
40
|
|
|
self::ELAPSED . ': %s %s', |
41
|
|
|
$this->refineElapsed($report->getElapsed()), |
42
|
|
|
$eol ? PHP_EOL : '' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \DateInterval $elapsed |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function refineElapsed(\DateInterval $elapsed): string |
51
|
|
|
{ |
52
|
|
|
return static::formatElapsed($elapsed); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function formatElapsed(\DateInterval $elapsed): string |
56
|
|
|
{ |
57
|
|
|
$c = CarbonInterval::instance($elapsed); |
58
|
|
|
if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) { |
59
|
|
|
return |
60
|
|
|
Pretty::milliseconds($c->totalMilliseconds); |
61
|
|
|
} |
62
|
|
|
// @codeCoverageIgnoreStart |
63
|
|
|
return (string)$c; |
64
|
|
|
// @codeCoverageIgnoreEnd |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param TimerReport $report |
69
|
|
|
* @param bool $eol |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function full(TimerReport $report, bool $eol = true): string |
73
|
|
|
{ |
74
|
|
|
$r = $report; |
75
|
|
|
$count = $r->getCount(); |
76
|
|
|
$values = |
77
|
|
|
0 < $count ? |
78
|
|
|
sprintf( |
79
|
|
|
self::AVERAGE . ': %s, ' . |
80
|
|
|
self::LAST . ': %s, ' . |
81
|
|
|
self::MIN . '(%s): %s, ' . |
82
|
|
|
self::MAX . '(%s): %s, ' . |
83
|
|
|
self::MARKS . ': %s, ', |
84
|
|
|
$this->refineSeconds($r->getAverageValue()), |
85
|
|
|
$this->refineSeconds($r->getLastValue()), |
86
|
|
|
$r->getMinValueIteration(), |
87
|
|
|
$this->refineSeconds($r->getMinValue()), |
88
|
|
|
$r->getMaxValueIteration(), |
89
|
|
|
$this->refineSeconds($r->getMaxValue()), |
90
|
|
|
$count |
91
|
|
|
) : |
92
|
|
|
''; |
93
|
|
|
|
94
|
|
|
return |
95
|
|
|
sprintf( |
96
|
|
|
self::TIMER . '%s: %s' . |
97
|
|
|
self::ELAPSED . ': %s%s', |
98
|
|
|
$this->refineName($r->getName()), |
99
|
|
|
$values, |
100
|
|
|
$this->refineElapsed($r->getElapsed()), |
101
|
|
|
$eol ? PHP_EOL : '' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function refineSeconds(?float $seconds): string |
106
|
|
|
{ |
107
|
|
|
return |
108
|
|
|
$seconds ? Pretty::seconds($seconds) : 'NULL'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function refineName(string $name): string |
112
|
|
|
{ |
113
|
|
|
if (DEFAULT_NAME === $name) { |
114
|
|
|
return ''; |
115
|
|
|
} |
116
|
|
|
return '[' . $name . ']'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|