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

TimerReportFormatter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 91
ccs 42
cts 45
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A formatElapsed() 0 8 2
A simple() 0 7 2
A full() 0 21 2
A refineElapsed() 0 3 1
A process() 0 9 4
A refineSeconds() 0 4 2
A refineName() 0 6 2
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Reports\Formatters;
4
5
use AlecRabbit\Accessories\Pretty;
6
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
7
use AlecRabbit\Tools\Reports\TimerReport;
8
use Carbon\CarbonInterval;
9
use function AlecRabbit\typeOf;
10
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
11
12
class TimerReportFormatter extends ReportFormatter
13
{
14
    protected const MILLISECONDS_THRESHOLD = 10000;
15
16
    /** {@inheritdoc} */
17 7
    public function process(ReportInterface $report): string
18
    {
19 7
        if ($report instanceof TimerReport) {
20 6
            if (0 === $report->getCount() && DEFAULT_NAME === $report->getName()) {
21 4
                return $this->simple($report);
22
            }
23 2
            return $this->full($report);
24
        }
25 1
        $this->wrongReportType(TimerReport::class, $report);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
26
    }
27
28
    /**
29
     * @param TimerReport $report
30
     * @param bool $eol
31
     * @return string
32
     */
33 4
    protected function simple(TimerReport $report, bool $eol = true): string
34
    {
35
        return
36 4
            sprintf(
37 4
                self::ELAPSED . ': %s %s',
38 4
                $this->refineElapsed($report->getElapsed()),
39 4
                $eol ? PHP_EOL : ''
40
            );
41
    }
42
43
    /**
44
     * @param \DateInterval $elapsed
45
     * @return string
46
     */
47 6
    protected function refineElapsed(\DateInterval $elapsed): string
48
    {
49 6
        return static::formatElapsed($elapsed);
50
    }
51
52 12
    public static function formatElapsed(\DateInterval $elapsed): string
53
    {
54 12
        $c = CarbonInterval::instance($elapsed);
55 12
        if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) {
56
            return
57 12
                Pretty::milliseconds($c->totalMilliseconds);
58
        }
59
        return (string)$c;
60
    }
61
62
    /**
63
     * @param TimerReport $report
64
     * @param bool $eol
65
     * @return string
66
     */
67 2
    protected function full(TimerReport $report, bool $eol = true): string
68
    {
69 2
        $r = $report;
70 2
        return sprintf(
71 2
            self::TIMER . '%s: ' .
72 2
            self::AVERAGE . ': %s, ' .
73 2
            self::LAST . ': %s, ' .
74 2
            self::MIN . '(%s): %s, ' .
75 2
            self::MAX . '(%s): %s, ' .
76 2
            self::COUNT . ': %s, ' .
77 2
            self::ELAPSED . ': %s%s',
78 2
            $this->refineName($r->getName()),
79 2
            $this->refineSeconds($r->getAverageValue()),
80 2
            $this->refineSeconds($r->getLastValue()),
81 2
            $r->getMinValueIteration(),
82 2
            $this->refineSeconds($r->getMinValue()),
83 2
            $r->getMaxValueIteration(),
84 2
            $this->refineSeconds($r->getMaxValue()),
85 2
            $r->getCount(),
86 2
            $this->refineElapsed($r->getElapsed()),
87 2
            $eol ? PHP_EOL : ''
88
        );
89
    }
90
91 2
    protected function refineName(string $name): string
92
    {
93 2
        if (DEFAULT_NAME === $name) {
94
            return '';
95
        }
96 2
        return '[' . $name . ']';
97
    }
98
99 2
    protected function refineSeconds(?float $seconds): string
100
    {
101
        return
102 2
            $seconds ? Pretty::seconds($seconds) : 'NULL';
103
    }
104
}
105