Passed
Push — master ( 152a70...0c8393 )
by Jose
03:00
created

SequencePrinter::printSequence()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 4
nop 0
crap 3
1
<?php
2
3
namespace JMGQ\AStar\Example\Graph;
4
5
class SequencePrinter
6
{
7
    /**
8
     * @param Graph $graph
9
     * @param Coordinate[] $sequence
10
     */
11 3
    public function __construct(private Graph $graph, private iterable $sequence)
0 ignored issues
show
Unused Code introduced by
The parameter $graph is not used and could be removed. ( Ignorable by Annotation )

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

11
    public function __construct(/** @scrutinizer ignore-unused */ private Graph $graph, private iterable $sequence)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sequence is not used and could be removed. ( Ignorable by Annotation )

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

11
    public function __construct(private Graph $graph, /** @scrutinizer ignore-unused */ private iterable $sequence)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
    {
13 3
    }
14
15 3
    public function printSequence(): void
16
    {
17 3
        $coordinatesAsString = [];
18
19 3
        foreach ($this->sequence as $coordinate) {
0 ignored issues
show
Bug Best Practice introduced by
The property sequence does not exist on JMGQ\AStar\Example\Graph\SequencePrinter. Did you maybe forget to declare it?
Loading history...
20 2
            $coordinatesAsString[] = $this->getCoordinateAsString($coordinate);
21
        }
22
23 3
        if (!empty($coordinatesAsString)) {
24 2
            echo implode(' => ', $coordinatesAsString);
25 2
            echo "\n";
26
        }
27
28 3
        echo 'Total cost: ' . $this->getTotalDistance();
29 3
    }
30
31 2
    private function getCoordinateAsString(Coordinate $coordinate): string
32
    {
33 2
        return "({$coordinate->getX()}, {$coordinate->getY()})";
34
    }
35
36 3
    private function getTotalDistance(): float | int
37
    {
38 3
        if (count($this->sequence) < 2) {
0 ignored issues
show
Bug Best Practice introduced by
The property sequence does not exist on JMGQ\AStar\Example\Graph\SequencePrinter. Did you maybe forget to declare it?
Loading history...
39 2
            return 0;
40
        }
41
42 1
        $totalDistance = 0;
43
44 1
        $sequence = $this->sequence;
45
46 1
        $previousNode = array_shift($sequence);
47 1
        foreach ($sequence as $node) {
48 1
            $totalDistance += $this->graph->getLink($previousNode, $node)->getDistance();
0 ignored issues
show
Bug Best Practice introduced by
The property graph does not exist on JMGQ\AStar\Example\Graph\SequencePrinter. Did you maybe forget to declare it?
Loading history...
49
50 1
            $previousNode = $node;
51
        }
52
53 1
        return $totalDistance;
54
    }
55
}
56