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/ConnectedDifferences.php (6 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
3
namespace Undemanding\Difference;
4
5
class ConnectedDifferences
6
{
7
    /**
8
     * @var array
9
     */
10
    private $bitmap;
11
12
    /**
13
     * @var int
14
     */
15
    private $width;
16
17
    /**
18
     * @var int
19
     */
20
    private $height;
21
22
    /**
23
     * @var array
24
     */
25
    private $boundaries = [];
26
27
    /**
28
     * @param Difference $difference
29
     */
30
    public function __construct(Difference $difference)
31
    {
32
        $this->bitmap = $difference->getBitmap();
33
        $this->width = $difference->getWidth();
34
        $this->height = $difference->getHeight();
35
36
        $this->boundaries = $this->findBoundaries();
37
    }
38
39
    /**
40
     * Find separate boundaries.
41
     *
42
     * @return array
43
     */
44
    private function findBoundaries()
45
    {
46
        $pixels = [];
47
48
        for ($y = 0; $y < $this->height; $y++) {
49
            for ($x = 0; $x < $this->width; $x++) {
50
                if ($this->bitmap[$y][$x] > 0) {
51
                    $pixels["{$x}:{$y}"] = [
52
                        "group" => null,
53
                        "x" => $x,
54
                        "y" => $y,
55
                    ];
56
                }
57
            }
58
        }
59
60
        $group = 1;
61
        $boundaries = [];
62
63
        foreach ($pixels as $i => $pixel) {
64
            $adjacent = $this->adjacent($pixel);
65
66
            if (!$pixel["group"]) {
67
                foreach ($adjacent as $key) {
68
                    if (isset($pixels[$key]) and $pixels[$key]["group"]) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
69
                        $pixel["group"] = $pixels[$key]["group"];
70
                    }
71
                }
72
            }
73
74
            if (!$pixel["group"]) {
75
                $pixel["group"] = $group++;
76
            }
77
78
            foreach ($adjacent as $key) {
79
                if (isset($pixels[$key])) {
80
                    $pixels[$key]["group"] = $pixel["group"];
81
                }
82
            }
83
        }
84
85
        $groups = array_values(array_unique(array_map(function($pixel) {
86
            return $pixel["group"];
87
        }, $pixels)));
88
89
        foreach ($groups as $group) {
90
            $filtered = array_filter($pixels, function($pixel) use ($group) {
91
                return $pixel["group"] === $group;
92
            });
93
94
            $ax = $this->width;
95
            $bx = 0;
96
            $ay = $this->height;
97
            $by = 0;
98
99
            foreach ($filtered as $pixel) {
100
                $x = $pixel["x"];
101
                $y = $pixel["y"];
102
103
                if ($x > $bx) {
104
                    $bx = $x;
105
                }
106
107
                if ($x < $ax) {
108
                    $ax = $x;
109
                }
110
111
                if ($y > $by) {
112
                    $by = $y;
113
                }
114
115
                if ($y < $ay) {
116
                    $ay = $y;
117
                }
118
            }
119
120
            array_push($boundaries, [
121
                "top" => $ay,
122
                "right" => $bx,
123
                "bottom" => $by,
124
                "left" => $ax,
125
            ]);
126
        }
127
128
        return $boundaries;
129
    }
130
131
    /**
132
     * @return ConnectedDifferences
133
     */
134
    public function withJoinedBoundaries()
135
    {
136
        $keep = [];
137
138
        foreach ($this->boundaries as $boundary) {
139
            foreach ($keep as $i => $kept) {
140
                if ($this->intersect($boundary, $kept)) {
141 View Code Duplication
                    if ($boundary["top"] < $kept["top"]) {
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...
142
                        $keep[$i]["top"] = $boundary["top"];
143
                    }
144
145 View Code Duplication
                    if ($boundary["right"] > $kept["right"]) {
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...
146
                        $keep[$i]["right"] = $boundary["right"];
147
                    }
148
149 View Code Duplication
                    if ($boundary["bottom"] > $kept["bottom"]) {
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...
150
                        $keep[$i]["bottom"] = $boundary["bottom"];
151
                    }
152
153 View Code Duplication
                    if ($boundary["left"] < $kept["left"]) {
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...
154
                        $keep[$i]["left"] = $boundary["left"];
155
                    }
156
157
                    continue 2;
158
                }
159
            }
160
161
            array_push($keep, $boundary);
162
        }
163
164
        return $this->cloneWith("boundaries", $keep);
165
    }
166
167
    /**
168
     * @param string $property
169
     * @param mixed $value
170
     *
171
     * @return ConnectedDifferences
172
     */
173
    private function cloneWith($property, $value)
174
    {
175
        $clone = clone $this;
176
        $clone->$property = $value;
177
178
        return $clone;
179
    }
180
181
    /**
182
     * Labels for adjacent pixels.
183
     *
184
     * @param array $pixel
185
     *
186
     * @return array
187
     */
188
    private function adjacent($pixel)
189
    {
190
        $adjacent = [
191
            ($pixel["x"] - 1) . ":" . ($pixel["y"] - 1),
192
            ($pixel["x"] - 1) . ":" . $pixel["y"],
193
            ($pixel["x"] - 1) . ":" . ($pixel["y"] + 1),
194
            $pixel["x"] . ":" . ($pixel["y"] - 1),
195
            $pixel["x"] . ":" . ($pixel["y"] + 1),
196
            ($pixel["x"] + 1) . ":" . ($pixel["y"] - 1),
197
            ($pixel["x"] + 1) . ":" . $pixel["y"],
198
            ($pixel["x"] + 1) . ":" . ($pixel["y"] + 1),
199
        ];
200
        return $adjacent;
201
    }
202
203
    /**
204
     * Tell if two boundaries overlap.
205
     *
206
     * @param array $p
207
     * @param array $q
208
     *
209
     * @return bool
210
     */
211
    private function intersect(array $p, array $q)
212
    {
213
        return max($p["left"], $q["left"]) <= min($p["right"], $q["right"]) and max($p["top"], $q["top"]) <= min($p["bottom"], $q["bottom"]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    public function boundaries()
220
    {
221
        return $this->boundaries;
222
    }
223
}
224