Passed
Push — master ( 130a84...94bf46 )
by Jose
05:30 queued 02:40
created

ResultPrinter::orderResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
        // Suppressing the errors thrown by usort due to a PHP5 bug:
47
        // https://bugs.php.net/bug.php?id=50688
48
        // https://stackoverflow.com/q/3235387
49 7
        @usort($results, function ($a, $b) {
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for usort(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

49
        /** @scrutinizer ignore-unhandled */ @usort($results, function ($a, $b) {

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50 1
            return $a->getSize() > $b->getSize();
51 7
        });
52
53 7
        return $results;
54
    }
55
56
    /**
57
     * @param AggregatedResult $result
58
     * @return string
59
     */
60 6
    private function formatSolutionFound($result)
61
    {
62 6
        $allResultsAreSolved = $result->getNumberOfSolutions() === $result->getNumberOfTerrains();
63 6
        if ($allResultsAreSolved) {
64 4
            return 'Yes';
65
        }
66
67 2
        $allResultsAreUnsolved = $result->getNumberOfSolutions() === 0;
68 2
        if ($allResultsAreUnsolved) {
69 1
            return 'No';
70
        }
71
72 1
        return 'Sometimes';
73
    }
74
}
75