ResultPrinter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 3
b 0
f 0
dl 0
loc 54
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A orderResults() 0 5 1
A __construct() 0 2 1
A formatSolutionFound() 0 13 3
A display() 0 19 2
1
<?php
2
3
namespace JMGQ\AStar\Benchmark\Result;
4
5
use Symfony\Component\Console\Style\StyleInterface;
6
7
class ResultPrinter
8
{
9 7
    public function __construct(private StyleInterface $output)
10
    {
11 7
    }
12
13
    /**
14
     * @param AggregatedResult[] $results
15
     */
16 7
    public function display(array $results): void
17
    {
18 7
        $tableRows = [];
19
20 7
        $orderedResults = $this->orderResults($results);
21
22 7
        foreach ($orderedResults as $result) {
23 6
            $size = $result->getSize() . 'x' . $result->getSize();
24 6
            $averageDuration = $result->getAverageDuration() . 'ms';
25 6
            $minimumDuration = $result->getMinimumDuration() . 'ms';
26 6
            $maximumDuration = $result->getMaximumDuration() . 'ms';
27 6
            $solutionFound = $this->formatSolutionFound($result);
28
29 6
            $tableRows[] = [$size, $averageDuration, $minimumDuration, $maximumDuration, $solutionFound];
30
        }
31
32 7
        $tableHeaders = ['Size', 'Avg Duration', 'Min Duration', 'Max Duration', 'Solved?'];
33
34 7
        $this->output->table($tableHeaders, $tableRows);
35 7
    }
36
37
    /**
38
     * @param AggregatedResult[] $results
39
     * @return AggregatedResult[]
40
     */
41 7
    private function orderResults(array $results): array
42
    {
43 7
        usort($results, static fn (AggregatedResult $a, AggregatedResult $b) => $a->getSize() - $b->getSize());
44
45 7
        return $results;
46
    }
47
48 6
    private function formatSolutionFound(AggregatedResult $result): string
49
    {
50 6
        $allResultsAreSolved = $result->getNumberOfSolutions() === $result->getNumberOfTerrains();
51 6
        if ($allResultsAreSolved) {
52 4
            return 'Yes';
53
        }
54
55 2
        $allResultsAreUnsolved = $result->getNumberOfSolutions() === 0;
56 2
        if ($allResultsAreUnsolved) {
57 1
            return 'No';
58
        }
59
60 1
        return 'Sometimes';
61
    }
62
}
63