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.

NodeGrid::getNodes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Letournel\PathFinder\Core;
4
5
class NodeGrid
6
{
7
    
8
    private
9
        $nodes,
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 $nodes.

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...
10
        $height,
11
        $width;
12
    
13
    /* Matrix Axes
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
     * .--------------> j (width) coord y
15
     * | 1,1 1,2 1,3
16
     * | 2,1 2,2 2,3
17
     * | 3,1 ...
18
     * |
19
     * i (height) coor x
20
     */
21
    public function __construct(array $matrix)
22
    {
23
        $this->height = $this->computeHeight($matrix);
24
        $this->width = $this->computeWidth($matrix);
25
        $this->nodes = $this->buildNodes($matrix);
26
    }
27
    
28
    private function computeHeight(array $matrix)
29
    {
30
        return count($matrix);
31
    }
32
    
33
    private function computeWidth(array $matrix)
34
    {
35
        $width = 0;
36
        foreach($matrix as $line)
37
        {
38
            $width = max(count($line), $width);
39
        }
40
        
41
        return $width;
42
    }
43
    
44
    private function buildNodes(array $matrix)
45
    {
46
        $nodes = array();
47
        
48
        for($i = 0; $i < $this->height; $i++)
49
        {
50
            $nodes[$i] = array();
51
            for($j = 0; $j < $this->width; $j++)
52
            {
53
                $walkable = isset($matrix[$i][$j]) ? $matrix[$i][$j] : false;
54
                $nodes[$i][$j] = new Node($i, $j, $walkable);
55
            }
56
        }
57
        
58
        return $nodes;
59
    }
60
    
61
    public function getNodes()
62
    {
63
        return $this->nodes;
64
    }
65
    
66
    public function buildWalkableNodesList()
67
    {
68
        $list = array();
69
        foreach($this->nodes as $line)
70
        {
71
            foreach($line as $node)
72
            {
73
                if($node->isWalkable())
74
                {
75
                    $list[] = $node;
76
                }
77
            }
78
        }
79
        
80
        return $list;
81
    }
82
    
83
    public function getNodeNumber($n)
84
    {
85
        $x = floor($n / $this->width);
86
        $y = $n % $this->width;
87
        
88
        return $this->nodes[$x][$y];
89
    }
90
    
91
    public function getNodesNb()
92
    {
93
        return $this->height * $this->width;
94
    }
95
    
96
    public function getWidth()
97
    {
98
        return $this->width;
99
    }
100
    
101
    public function getHeight()
102
    {
103
        return $this->height;
104
    }
105
    
106
    public function getWalkableNeighbors(Node $node)
107
    {
108
        if(! $node->isWalkable())
109
        {
110
            return array();
111
        }
112
        
113
        $deltas = array(
114
            array(-1, -1), array(-1, +0), array(-1, +1),
115
            array(+0, -1),                array(+0, +1),
116
            array(+1, -1), array(+1, +0), array(+1, +1),
117
        );
118
        
119
        $neighbors = array();
120
        foreach($deltas as $delta)
121
        {
122
            $x = $node->getX() + $delta[0];
123
            $y = $node->getY() + $delta[1];
124
            if($this->isWalkableAt($x, $y))
125
            {
126
                $neighbors[] = $this->getNodeAt($x, $y);
127
            }
128
        }
129
        
130
        return $neighbors;
131
    }
132
    
133
    private function getNodeAt($x, $y)
134
    {
135
        if(! array_key_exists($x, $this->nodes))
136
        {
137
            return null;
138
        }
139
        
140
        if(! array_key_exists($y, $this->nodes[$x]))
141
        {
142
            return null;
143
        }
144
        
145
        return $this->nodes[$x][$y];
146
    }
147
    
148
    private function isWalkableAt($x, $y)
149
    {
150
        $node = $this->getNodeAt($x, $y);
151
        if($node instanceof Node)
152
        {
153
            return $node->isWalkable();
154
        }
155
        
156
        return false;
157
    }
158
}
159