Issues (18)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Board.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
namespace Thunder\Logeek;
4
5
final class Board
6
{
7
    private $width;
8
    private $height;
9
    private $fieldTypes = [];
10
    private $fields = [];
11
    private $actors = [];
12
    private $exits = [];
13
    private $functions = [];
14
    private $variables = [];
15
    private $actions = [];
16
17
    private static $moveMap = [
18
        'left' => [0, -1],
19
        'right' => [0, 1],
20
        'up' => [-1, 0],
21
        'down' => [1, 0],
22
    ];
23
24
    public function __construct(int $width, int $height)
25
    {
26
        $this->width = $width;
27
        $this->height = $height;
28
    }
29
30
    /* --- METHODS --- */
31
32
    /**
33
     * @param ActionInterface[] $actions
34
     */
35
    public function addActions(array $actions)
36
    {
37
        foreach($actions as $action) {
38
            $this->addAction($action);
39
        }
40
    }
41
42
    public function addAction(ActionInterface $action)
43
    {
44
        $this->actions[$action->getAlias()] = $action;
45
    }
46
47
    public function getAction(string $alias): ActionInterface
48
    {
49
        if(!$this->hasAction($alias)) {
50
            throw new \RuntimeException(sprintf('Action %s does not exist!', $alias));
51
        }
52
53
        return $this->actions[$alias];
54
    }
55
56
    public function addFieldTypes(array $types)
57
    {
58
        foreach($types as $alias => $symbol) {
59
            $this->addFieldType($alias, $symbol);
60
        }
61
    }
62
63
    public function hasAction($alias): bool
64
    {
65
        return array_key_exists($alias, $this->actions);
66
    }
67
68
    public function addFieldType($alias, $symbol)
69
    {
70
        $this->fieldTypes[$alias] = $symbol;
71
    }
72
73
    public function loadFromString($string)
74
    {
75
        $types = array_flip($this->fieldTypes);
76
        $lines = explode("\n", trim($string));
77
        $height = \count($lines);
78
        $width = \strlen(trim($lines[0]));
79
        for($i = 0; $i < $height; $i++) {
80
            $line = trim($lines[$i]);
81
            for($j = 0; $j < $width; $j++) {
82
                $this->fields[$i][$j] = $types[$line[$j]];
83
            }
84
        }
85
    }
86
87
    public function addActor($alias, $x, $y, $direction)
88
    {
89
        $this->actors[$alias] = [
90
            'x' => $x,
91
            'y' => $y,
92
            'direction' => $direction,
93
            'pick' => null,
94
        ];
95
        $this->debug('Actor Alias[%s] Direction[%s] Position[%s:%s]', $alias, $direction, $y, $x);
96
    }
97
98
    public function addExit($alias, $x, $y)
99
    {
100
        $this->exits[$alias] = [
101
            'x' => $y,
102
            'y' => $x,
103
        ];
104
    }
105
106
    public function addFunctions(array $functions)
107
    {
108
        foreach($functions as $name => $program) {
109
            $this->functions[$name] = $program;
110
        }
111
    }
112
113
    public function addFunction($name, array $program)
114
    {
115
        $this->functions[$name] = $program;
116
    }
117
118
    public function isActorAtExit($actor, $exit): bool
119
    {
120
        $actor = $this->actors[$actor];
121
        $exit = $this->exits[$exit];
122
123
        return $actor['x'] === $exit['x'] && $actor['y'] === $exit['y'];
124
    }
125
126
    public function runActor($alias)
127
    {
128
        $this->runActorProgram($alias, $this->functions['main']);
129
    }
130
131
    public function runActorProgram($alias, array $program)
132
    {
133
        foreach($program as $operation) {
134
            $this->runActorOperation($alias, $operation);
135
        }
136
    }
137
138
    private function runActorOperation($alias, array $operation)
139
    {
140
        $this->getAction($operation['action'])->execute($this, $alias, $operation);
141
    }
142
143
    public function getActorNextMove($alias): array
144
    {
145
        list($x, $y) = $this->getActorPosition($alias);
146
        list($diffX, $diffY) = static::$moveMap[$this->getActorDirection($alias)];
0 ignored issues
show
Comprehensibility introduced by
Since Thunder\Logeek\Board is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
147
148
        return [$x + $diffX, $y + $diffY];
149
    }
150
151
    public function setVariable($name, $value)
152
    {
153
        $this->variables[$name] = $value;
154
    }
155
156
    public function getVariable($name)
157
    {
158
        return $this->variables[$name];
159
    }
160
161
    public function getFunction($name)
162
    {
163
        return $this->functions[$name];
164
    }
165
166
    public function moveActor($alias)
167
    {
168
        $moveMap = [
169
            'left' => [0, -1],
170
            'right' => [0, 1],
171
            'up' => [-1, 0],
172
            'down' => [1, 0],
173
        ];
174
175
        $actor = $this->actors[$alias];
176
        list($x, $y) = $moveMap[$actor['direction']];
177
        $newX = $actor['x'] + $x;
178
        $newY = $actor['y'] + $y;
179
180
        $this->debug('Move [%s:%s] -> [%s:%s]', $actor['y'], $actor['x'], $newY, $newX);
181
        if('wall' === $this->fields[$newX][$newY]) {
182
            throw new \RuntimeException(sprintf('Wall at [%s, %s]', $newX, $newY));
183
        }
184
185
        $actor['x'] = $newX;
186
        $actor['y'] = $newY;
187
        $this->actors[$alias] = $actor;
188
    }
189
190
    public function debug(...$args)
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
191
    {
192
    }
193
194
    public function setField($x, $y, $field)
195
    {
196
        $this->fields[$x][$y] = $field;
197
    }
198
199
    public function getField($x, $y)
200
    {
201
        return $this->fields[$x][$y];
202
    }
203
204
    public function getActorDirection($alias)
205
    {
206
        return $this->actors[$alias]['direction'];
207
    }
208
209
    public function setActorDirection($alias, $direction)
210
    {
211
        $this->actors[$alias]['direction'] = $direction;
212
    }
213
214
    public function getActorPick($alias)
215
    {
216
        return $this->actors[$alias]['pick'];
217
    }
218
219
    public function setActorPick($alias, $pick)
220
    {
221
        $this->actors[$alias]['pick'] = $pick;
222
    }
223
224
    public function getActorPosition($alias): array
225
    {
226
        return [$this->actors[$alias]['x'], $this->actors[$alias]['y']];
227
    }
228
229
    public function renderBoard(): string
230
    {
231
        $return = '';
232
        $return .= "\n";
233
        for($i = 0; $i < $this->height; $i++) {
234
            for($j = 0; $j < $this->width; $j++) {
235
                $symbol = $this->fieldTypes[$this->fields[$i][$j]];
236 View Code Duplication
                $isActor = array_reduce($this->actors, function($state, array $item) use ($i, $j) {
0 ignored issues
show
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...
237
                    $state += $item['x'] === $i && $item['y'] === $j;
238
                    return $state;
239
                }, 0);
240 View Code Duplication
                $isExit = array_reduce($this->exits, function($state, array $item) use ($i, $j) {
0 ignored issues
show
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...
241
                    $state += $item['x'] === $i && $item['y'] === $j;
242
                    return $state;
243
                }, 0);
244
                if($isExit) {
245
                    $symbol = 'E';
246
                }
247
                if($isActor) {
248
                    $symbol = 'A';
249
                }
250
                $return .= $symbol;
251
            }
252
            $return .= "\n";
253
        }
254
255
        return $return;
256
    }
257
258
    /* --- GETTERS --- */
259
260
    public function getWidth(): int
261
    {
262
        return $this->width;
263
    }
264
265
    public function getHeight(): int
266
    {
267
        return $this->height;
268
    }
269
}
270