Passed
Push — develop ( 0a2c3f...8e5858 )
by Alec
03:05
created

ProfilerReportFormatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 53
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 2
A timersStrings() 0 12 4
A countersStrings() 0 11 5
1
<?php
2
declare(strict_types=1);
3
4
namespace AlecRabbit\Tools\Reports\Formatters;
5
6
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
7
use AlecRabbit\Tools\Reports\SimpleCounterReport;
8
use AlecRabbit\Tools\Reports\ProfilerReport;
9
use AlecRabbit\Tools\Reports\TimerReport;
10
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
11
12
class ProfilerReportFormatter extends ReportFormatter
13
{
14
    /** @var string */
15
    private $elapsed = '';
16
17
    /** {@inheritdoc} */
18 1
    public function process(ReportInterface $report): string
19
    {
20 1
        if ($report instanceof ProfilerReport) {
21
            return
22 1
                sprintf(
23 1
                    '%s %s %s',
24 1
                    $this->countersStrings($report),
25 1
                    $this->timersStrings($report),
26 1
                    $this->elapsed
27
                );
28
        }
29
        return '';
30
    }
31
32
    /**
33
     * @param ProfilerReport $report
34
     * @return string
35
     */
36 1
    protected function countersStrings(ProfilerReport $report): string
37
    {
38 1
        $r = '';
39 1
        foreach ($report->getCountersReports() as $countersReport) {
40 1
            if ($countersReport instanceof SimpleCounterReport && DEFAULT_NAME === $countersReport->getName()) {
41 1
                $r .= $countersReport->isStarted() ? $countersReport : '';
42
            } else {
43
                $r .= $countersReport;
44
            }
45
        }
46 1
        return $r;
47
    }
48
49
    /**
50
     * @param ProfilerReport $report
51
     * @return string
52
     */
53 1
    protected function timersStrings(ProfilerReport $report): string
54
    {
55 1
        $r = '';
56 1
        foreach ($report->getTimersReports() as $timerReport) {
57 1
            if ($timerReport instanceof TimerReport && DEFAULT_NAME === $timerReport->getName()) {
58
//                $this->elapsed = (string)$timerReport;
59 1
                $r .= $timerReport;
60
            } else {
61
                $r .= $timerReport;
62
            }
63
        }
64 1
        return $r;
65
    }
66
}
67