GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AStar   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setGrid() 0 4 1
A computeLength() 0 6 1
C computePath() 0 50 8
1
<?php
2
3
namespace Letournel\PathFinder\Algorithms\ShortestPath;
4
5
use Letournel\PathFinder\AlgorithmShortestPath;
6
use Letournel\PathFinder\Core\Heuristic;
7
use Letournel\PathFinder\Core\Node;
8
use Letournel\PathFinder\Core\NodeGrid;
9
use Letournel\PathFinder\Core\NodeMap;
10
use Letournel\PathFinder\Core\NodePath;
11
use Letournel\PathFinder\Core\NodePriorityQueueMin;
12
use Letournel\PathFinder\Distance;
13
14
class AStar implements AlgorithmShortestPath
15
{
16
    /*
17
     * For more info see
18
     * http://en.wikipedia.org/wiki/A*_search_algorithm
19
     */
20
    
21
    private
22
        $distance,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $distance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
23
        $grid,
24
        $heuristic;
25
    
26
    public function __construct(Distance $distance, Heuristic $heuristic)
27
    {
28
        $this->distance = $distance;
29
        $this->heuristic = $heuristic;
30
    }
31
    
32
    public function setGrid(NodeGrid $grid)
33
    {
34
        $this->grid = $grid;
35
    }
36
    
37
    public function computeLength(Node $source, Node $target)
38
    {
39
        $shortestPath = $this->computePath($source, $target);
40
        
41
        return $shortestPath->computeLength($this->distance);
42
    }
43
    
44
    public function computePath(Node $source, Node $target)
45
    {
46
        if(! $this->grid instanceof NodeGrid)
47
        {
48
            throw new \RuntimeException('Invalid Grid');
49
        }
50
        
51
        $fScorePriorityQueue = new NodePriorityQueueMin();
52
        $gScoreMap = new NodeMap();
53
        $openedMap = new NodeMap();
54
        $closedMap = new NodeMap();
55
        $previousMap = new NodeMap();
56
        
57
        $fScorePriorityQueue->insert($source, 0);
58
        $gScoreMap->insert($source, 0);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        $openedMap->insert($source);
60
        
61
        while(! $fScorePriorityQueue->isEmpty())
62
        {
63
            $node = $fScorePriorityQueue->extract();
64
            $closedMap->insert($node);
65
            
66
            if($node->getId() === $target->getId())
67
            {
68
                return new NodePath($previousMap->lookupFrom($node));
69
            }
70
            
71
            $neighbors = $this->grid->getWalkableNeighbors($node);
72
            foreach($neighbors as $neighbor)
73
            {
74
                if($closedMap->exists($neighbor))
75
                {
76
                    continue;
77
                }
78
                
79
                $gScore = $gScoreMap->lookup($node) + $this->distance->compute($node, $neighbor);
80
                if(! $openedMap->exists($neighbor) || $gScore < $gScoreMap->lookup($neighbor))
81
                {
82
                    $fScore = $gScore + $this->heuristic->compute($node, $target);
83
                    $fScorePriorityQueue->insert($neighbor, $fScore);
84
                    $gScoreMap->insert($neighbor, $gScore);
0 ignored issues
show
Documentation introduced by
$gScore is of type double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
                    $openedMap->insert($neighbor);
86
                    $previousMap->insert($neighbor, $node);
87
                }
88
            }
89
        }
90
        
91
        // no path found
92
        return array();
93
    }
94
}
95