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

ProfilerReportFormatter::countersStrings()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 5.0729
rs 9.6111
c 0
b 0
f 0
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