Passed
Branch benchmark (b81757)
by Jose
02:27
created

ResultPrinter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 65
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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