Safely   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 29
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A apply() 0 3 1
A estimate() 0 6 2
A environment() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Pathfinder\Estimate;
4
5
use Stratadox\Pathfinder\Environment;
6
use Stratadox\Pathfinder\Heuristic;
7
8
final class Safely implements Heuristic
9
{
10
    private $heuristic;
11
    private $environment;
12
13
    private function __construct(
14
        Heuristic $heuristic,
15
        Environment $environment
16
    ) {
17
        $this->heuristic = $heuristic;
18
        $this->environment = $environment;
19
    }
20
21
    public static function apply(Heuristic $heuristic): self
22
    {
23
        return new self($heuristic, $heuristic->environment());
24
    }
25
26
    public function estimate(string $start, string $goal): float
27
    {
28
        if ($this->environment->areNeighbours($start, $goal)) {
29
            return $this->environment->movementCostBetween($start, $goal);
30
        }
31
        return $this->heuristic->estimate($start, $goal);
32
    }
33
34
    public function environment(): Environment
35
    {
36
        return $this->environment;
37
    }
38
}
39