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

DomainLogic::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JMGQ\AStar\Example\Graph;
4
5
use JMGQ\AStar\DomainLogicInterface;
6
7
class DomainLogic implements DomainLogicInterface
8
{
9 6
    public function __construct(private Graph $graph)
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

9
    public function __construct(/** @scrutinizer ignore-unused */ private Graph $graph)

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...
10
    {
11 6
    }
12
13
    /**
14
     * @param Coordinate $node
15
     * @return Coordinate[]
16
     */
17 3
    public function getAdjacentNodes(mixed $node): iterable
18
    {
19 3
        return $this->graph->getDirectSuccessors($node);
0 ignored issues
show
Bug Best Practice introduced by
The property graph does not exist on JMGQ\AStar\Example\Graph\DomainLogic. Did you maybe forget to declare it?
Loading history...
20
    }
21
22
    /**
23
     * @param Coordinate $node
24
     * @param Coordinate $adjacent
25
     * @return float|int
26
     */
27 4
    public function calculateRealCost(mixed $node, mixed $adjacent): float | int
28
    {
29 4
        if (!$this->graph->hasLink($node, $adjacent)) {
0 ignored issues
show
Bug Best Practice introduced by
The property graph does not exist on JMGQ\AStar\Example\Graph\DomainLogic. Did you maybe forget to declare it?
Loading history...
30 1
            throw new \DomainException('The provided nodes are not linked');
31
        }
32
33 3
        return $this->graph->getLink($node, $adjacent)->getDistance();
34
    }
35
36
    /**
37
     * @param Coordinate $fromNode
38
     * @param Coordinate $toNode
39
     * @return float|int
40
     */
41 3
    public function calculateEstimatedCost(mixed $fromNode, mixed $toNode): float | int
42
    {
43 3
        return $this->euclideanDistance($fromNode, $toNode);
44
    }
45
46 3
    private function euclideanDistance(Coordinate $a, Coordinate $b): float
47
    {
48 3
        $xFactor = ($a->getX() - $b->getX()) ** 2;
49 3
        $yFactor = ($a->getY() - $b->getY()) ** 2;
50
51 3
        return sqrt($xFactor + $yFactor);
52
    }
53
}
54