DefaultReporter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 30
c 2
b 1
f 0
dl 0
loc 55
ccs 33
cts 33
cp 1
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B report() 0 44 8
A measureString() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright (c) 2024 TEQneers GmbH & Co. KG
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE.md file that was distributed with this source code.
10
 *
11
 * @see https://github.com/teqneers/phpunit-stopwatch
12
 */
13
14
namespace TQ\Testing\Extension\Stopwatch\Reporter;
15
16
/**
17
 * @internal
18
 */
19
final class DefaultReporter implements Reporter
20
{
21 13
    public function report(string $headline, array $totals, ?array $current = null): string
22
    {
23 13
        $output    = '';
24 13
        $nameWidth = 50;
25
26 13
        if (null !== $current) {
27 9
            if (!empty($current)) {
28 9
                $output .= "\n\n{$headline}:\n";
29
30
                /** @var array $stopWatch */
31 9
                foreach ($current as $name => $stopWatch) {
32
                    /** @var array $total */
33 9
                    $total = $totals[$name];
34
35 9
                    $printName = \strlen($name) > 50 ? '...' . \substr($name, -46) : $name;
36 9
                    $output .= \sprintf(
37 9
                        "- %-{$nameWidth}s %-s TOTAL %-s\n",
38 9
                        $printName,
39 9
                        $this->measureString($stopWatch),
40 9
                        $this->measureString($total),
41 9
                    );
42
                }
43
            }
44
45 9
            return $output;
46
        }
47
48 4
        $nameWidth += 34;
49
50 4
        if (!empty($totals)) {
51 4
            $output .= "\n\nStopwatch TOTALS:\n";
52
53
            /** @var array $total */
54 4
            foreach ($totals as $name => $total) {
55 4
                $printName = \strlen($name) > 50 ? '...' . \substr($name, -46) : $name;
56 4
                $output .= \sprintf(
57 4
                    "- %-{$nameWidth}s TOTAL %-s\n",
58 4
                    $printName,
59 4
                    $this->measureString($total),
60 4
                );
61
            }
62
        }
63
64 4
        return $output;
65
    }
66
67 13
    private function measureString(array $dataPoint): string
68
    {
69 13
        return \sprintf(
70 13
            '%10.3fsecs (%5dx, Ø %6.2f)',
71 13
            $dataPoint['duration'],
72 13
            $dataPoint['times'],
73 13
            0 < $dataPoint['times'] ? $dataPoint['duration'] / $dataPoint['times'] : '-',
74 13
        );
75
    }
76
}
77