Completed
Push — develop ( fe1195...b733eb )
by Alec
05:48
created

TimerReportFormatter::full()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.1017

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 23
ccs 12
cts 17
cp 0.7059
crap 2.1017
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 10.12.18
5
 * Time: 14:22
6
 */
7
declare(strict_types=1);
8
9
namespace AlecRabbit\Tools\Reports\Formatters;
10
11
use AlecRabbit\Tools\Reports\TimerReport;
12
use function AlecRabbit\format_time_auto;
13
use const AlecRabbit\Constants\Accessories\DEFAULT_NAME;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\Accessories\DEFAULT_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
class TimerReportFormatter extends Formatter
16
{
17
    /** @var TimerReport */
18
    protected $report;
19
20 8
    public function setStyles(): void
21
    {
22 8
    }
23
24 2
    public function getString(): string
25
    {
26 2
        if (DEFAULT_NAME === $name = $this->report->getName()) {
27 2
            return $this->elapsed();
28
        }
29 1
        return $this->full($name);
30
    }
31
32
    /**
33
     * @return string
34
     * @throws \Throwable
35
     */
36 2
    public function elapsed(): string
37
    {
38
        return
39 2
            sprintf(
40 2
                'Elapsed: %s %s',
41 2
                $this->theme->comment(format_time_auto($this->report->getElapsed())),
42 2
                PHP_EOL
43
            );
44
    }
45
46
    /**
47
     * @param string $name
48
     * @return string
49
     * @throws \Throwable
50
     */
51 1
    public function full(string $name): string
52
    {
53
        try {
54 1
            $str = sprintf(
55 1
                'Timer:[%s] Average: %s, Last: %s, Min(%s): %s, Max(%s): %s, Count: %s' . PHP_EOL,
56 1
                $this->theme->info($name),
57 1
                $this->theme->comment(format_time_auto($this->report->getAverageValue())),
58 1
                format_time_auto($this->report->getLastValue()),
59 1
                $this->report->getMinValueIteration(),
60 1
                format_time_auto($this->report->getMinValue()),
61 1
                $this->report->getMaxValueIteration(),
62 1
                format_time_auto($this->report->getMaxValue()),
63 1
                $this->report->getCount()
64
            );
65
        } catch (\Throwable $e) {
66
            $str =
67
                sprintf(
68
                    'Timer:[%s] %s' . PHP_EOL,
69
                    $this->theme->info($name),
70
                    $this->theme->red('Exception encountered')
71
                );
72
        }
73 1
        return $str;
74
    }
75
}
76