DefaultReporter::measureString()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 2
rs 10
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