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.

ASCIISyntax   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 173
Duplicated Lines 12.72 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 33
lcom 0
cbo 3
dl 22
loc 173
rs 9.3999
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToGrid() 0 14 4
A convertToMatrix() 0 11 2
A convertToSyntax() 0 16 4
C convertToSyntaxWithPath() 0 35 7
A findAndCreateNode() 11 15 4
A findAndCreateNodes() 11 18 4
C generateNodePath() 0 48 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Letournel\PathFinder\Converters\Grid;
4
5
use Letournel\PathFinder\Core\Node;
6
use Letournel\PathFinder\Core\NodeGrid;
7
use Letournel\PathFinder\Core\NodePath;
8
use Letournel\PathFinder\ConverterGrid;
9
10
class ASCIISyntax implements ConverterGrid
11
{
12
    const
13
        FREE = ' ',
14
        IN   = '>',
15
        OUT  = '<',
16
        STEP = '.',
17
        WALL = 'X';
18
    
19
    public function convertToGrid($syntax)
20
    {
21
        $matrix = $this->convertToMatrix($syntax);
22
        
23
        foreach($matrix as $x => $line)
24
        {
25
            foreach($line as $y => $char)
26
            {
27
                $matrix[$x][$y] = ($char !== self::WALL) ? 1 : 0;
28
            }
29
        }
30
        
31
        return new NodeGrid($matrix);
32
    }
33
    
34
    public function convertToMatrix($syntax)
35
    {
36
        $matrix = array_filter(explode("\n", $syntax));
37
        
38
        foreach($matrix as $key => $line)
39
        {
40
            $matrix[$key] = array_filter(str_split($line));
41
        }
42
        
43
        return $matrix;
44
    }
45
    
46
    public function convertToSyntax(NodeGrid $grid)
47
    {
48
        $syntax = '';
49
        
50
        $nodes = $grid->getNodes();
51
        foreach($nodes as $line)
52
        {
53
            foreach($line as $node)
54
            {
55
                $syntax .= ($node->isWalkable() ? self::FREE : self::WALL);
56
            }
57
            $syntax .= "\n";
58
        }
59
        
60
        return $syntax;
61
    }
62
    
63
    public function convertToSyntaxWithPath(NodeGrid $grid, NodePath $path)
64
    {
65
        $syntax = '';
66
        
67
        $nodes = $grid->getNodes();
68
        foreach($nodes as $line)
69
        {
70
            foreach($line as $node)
71
            {
72
                if(! $node->isWalkable())
73
                {
74
                    $syntax .= self::WALL;
75
                }
76
                elseif($node->toString() == $path->getStartNode()->toString())
77
                {
78
                    $syntax .= self::IN;
79
                }
80
                elseif($node->toString() == $path->getEndNode()->toString())
81
                {
82
                    $syntax .= self::OUT;
83
                }
84
                elseif($path->contains($node))
85
                {
86
                    $syntax .= self::STEP;
87
                }
88
                else
89
                {
90
                    $syntax .= self::FREE;
91
                }
92
            }
93
            $syntax .= "\n";
94
        }
95
        
96
        return $syntax;
97
    }
98
    
99
    public function findAndCreateNode($syntax, $charToFind)
100
    {
101
        $xCount = count($syntax);
102 View Code Duplication
        for($x = 0; $x < $xCount; $x++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
        {
104
            $yCount = count($syntax[$x]);
105
            for($y = 0; $y < $yCount; $y++)
106
            {
107
                if($syntax[$x][$y] === $charToFind)
108
                {
109
                    return new Node($x, $y);
110
                }
111
            }
112
        }
113
    }
114
    
115
    public function findAndCreateNodes($syntax, $charToFind)
116
    {
117
        $nodes = array();
118
        $xCount = count($syntax);
119 View Code Duplication
        for($x = 0; $x < $xCount; $x++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
        {
121
            $yCount = count($syntax[$x]);
122
            for($y = 0; $y < $yCount; $y++)
123
            {
124
                if($syntax[$x][$y] === $charToFind)
125
                {
126
                    $nodes[] = new Node($x, $y);
127
                }
128
            }
129
        }
130
        
131
        return $nodes;
132
    }
133
    
134
    public function generateNodePath($matrix)
135
    {
136
        $deltas = array(
137
            array(-1, -1), array(-1, +0), array(-1, +1),
138
            array(+0, -1),                array(+0, +1),
139
            array(+1, -1), array(+1, +0), array(+1, +1),
140
        );
141
        
142
        $node = $this->findAndCreateNode($matrix, self::IN);
143
        $target = $this->findAndCreateNode($matrix, self::OUT);
144
        $path = array($node);
145
        while($node->getId() !== $target->getId())
146
        {
147
            $newNode = null;
148
            foreach($deltas as $delta)
149
            {
150
                $x = $node->getX() + $delta[0];
151
                $y = $node->getY() + $delta[1];
152
                
153
                if(! array_key_exists($x, $matrix))
154
                {
155
                    continue;
156
                }
157
                
158
                if(! array_key_exists($y, $matrix[$x]))
159
                {
160
                    continue;
161
                }
162
                
163
                if($matrix[$x][$y] === self::STEP || $matrix[$x][$y] === self::OUT)
164
                {
165
                    $matrix[$x][$y] = self::FREE;
166
                    $newNode = new Node($x, $y);
167
                    break;
168
                }
169
            }
170
            
171
            if(! $newNode instanceof Node)
172
            {
173
                throw new \RuntimeException('Path is not continous in the grid');
174
            }
175
            
176
            $node = $newNode;
177
            $path[] = $node;
178
        }
179
        
180
        return new NodePath($path);
181
    }
182
}
183