Completed
Push — master ( 152dcd...9d2605 )
by Alec
05:33 queued 02:40
created

ProfilerReportFormatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

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