ProfilerReportFormatter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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