Passed
Push — develop ( 8e5858...26158a )
by Alec
02:57
created

ProfilerReportFormatter::timersStrings()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
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\AbstractCounter;
7
use AlecRabbit\Tools\Reports\AbstractCounterReport;
8
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
9
use AlecRabbit\Tools\Reports\SimpleCounterReport;
10
use AlecRabbit\Tools\Reports\ProfilerReport;
11
use AlecRabbit\Tools\Reports\TimerReport;
12
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
13
use function AlecRabbit\typeOf;
14
15
class ProfilerReportFormatter extends ReportFormatter
16
{
17
    /** @var string */
18
    private $elapsed = '';
19
20
    /** {@inheritdoc} */
21 3
    public function process(ReportInterface $report): string
22
    {
23 3
        if ($report instanceof ProfilerReport) {
24
            return
25 2
                sprintf(
26 2
                    '%s%s%s',
27 2
                    $this->countersStrings($report),
28 2
                    $this->timersStrings($report),
29 2
                    $this->elapsed
30
                );
31
        }
32 1
        $this->wrongReportType(ProfilerReport::class, $report);
33
        // @codeCoverageIgnoreStart
34
        return '';
35
        // @codeCoverageIgnoreEnd
36
    }
37
38
    /**
39
     * @param ProfilerReport $report
40
     * @return string
41
     */
42 2
    protected function countersStrings(ProfilerReport $report): string
43
    {
44 2
        $r = '';
45 2
        foreach ($report->getCountersReports() as $counterReport) {
46 2
            if ($counterReport instanceof AbstractCounterReport && DEFAULT_NAME === $counterReport->getName()) {
47 2
                $r .= $counterReport->isStarted() ? $counterReport : '';
48
            } else {
49
                $r .= $counterReport;
50
            }
51
        }
52 2
        return $r;
53
    }
54
55
    /**
56
     * @param ProfilerReport $report
57
     * @return string
58
     */
59 2
    protected function timersStrings(ProfilerReport $report): string
60
    {
61 2
        $r = '';
62 2
        foreach ($report->getTimersReports() as $timerReport) {
63 2
            if ($timerReport instanceof TimerReport && DEFAULT_NAME === $timerReport->getName()) {
64 2
                $this->elapsed = (string)$timerReport;
65
//                $r .= $timerReport;
66
            } else {
67
                $r .= $timerReport;
68
            }
69
        }
70 2
        return $r;
71
    }
72
}
73