Issues (16)

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/ArrayEnsurance.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
3
namespace Dgame\Ensurance;
4
5
use Dgame\Type\Type;
6
use Dgame\Type\TypeFactory;
7
8
/**
9
 * Class ArrayEnsurance
10
 * @package Dgame\Ensurance
11
 */
12
final class ArrayEnsurance implements EnsuranceInterface
13
{
14
    use EnsuranceTrait;
15
16
    /**
17
     * ArrayEnsurance constructor.
18
     *
19
     * @param EnsuranceInterface $ensurance
20
     */
21
    public function __construct(EnsuranceInterface $ensurance)
22
    {
23 9
        $this->transferEnsurance($ensurance);
24
        $this->value = $ensurance->else([]);
25 9
    }
26 9
27
    /**
28
     * @param callable $callback
29
     *
30
     * @return array
31
     */
32
    private function filterBy(callable $callback): array
33 1
    {
34
        return array_filter(array_map($callback, $this->value));
35 1
    }
36
37 1
    /**
38
     * @param callable $callback
39
     *
40
     * @return ArrayEnsurance
41
     */
42
    public function all(callable $callback): self
43
    {
44
        $this->ensure(count($this->filterBy($callback)) === count($this->value));
45 1
46
        return $this;
47 1
    }
48
49 1
    /**
50
     * @param callable $callback
51
     *
52
     * @return ArrayEnsurance
53
     */
54
    public function any(callable $callback): self
55
    {
56
        $this->ensure(!empty($this->filterBy($callback)));
57 1
58
        return $this;
59 1
    }
60 1
61
    /**
62 1
     * @param string $type
63
     *
64
     * @return ArrayEnsurance
0 ignored issues
show
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
     * @throws \Exception
66
     */
67
    public function allTypeOf(string $type): self
68
    {
69
        $type = Type::import($type);
0 ignored issues
show
Consider using a different name than the parameter $type. This often makes code more readable.
Loading history...
70 1
71
        return $this->all(static function ($value) use ($type): bool {
72 1
            return TypeFactory::expression($value)->isSame($type);
73 1
        });
74
    }
75 1
76
    /**
77
     * @param string $type
78
     *
79
     * @return ArrayEnsurance
0 ignored issues
show
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
80
     * @throws \Exception
81
     */
82
    public function anyTypeOf(string $type): self
83 1
    {
84
        $type = Type::import($type);
0 ignored issues
show
Consider using a different name than the parameter $type. This often makes code more readable.
Loading history...
85 1
86 1
        return $this->any(static function ($value) use ($type): bool {
87
            return TypeFactory::expression($value)->isSame($type);
88 1
        });
89
    }
90
91
    /**
92
     * @param mixed $key
93
     *
94
     * @return ArrayEnsurance
95
     */
96 1
    public function hasKey($key): self
97
    {
98 1
        $this->ensure(array_key_exists($key, $this->value))
99 1
             ->orThrow('Key "%s" is not contained in %s', $key, $this->value);
100
101 1
        return $this;
102
    }
103
104
    /**
105
     * @param mixed $value
106
     *
107
     * @return ArrayEnsurance
108
     */
109 1
    public function hasValue($value): self
110
    {
111 1
        $this->ensure(in_array($value, $this->value, true))
112 1
             ->orThrow('Value "%s" is not contained in ', $value, $this->value);
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * @param int $length
119
     *
120 1
     * @return ArrayEnsurance
121
     */
122 1
    public function hasLengthOf(int $length): self
123 1
    {
124
        $count = count($this->value);
125 1
        $this->ensure($count === $length)->orThrow('array has not the length %d (%d)', $length, $count);
126
127
        return $this;
128
    }
129
130
    /**
131 1
     * @param int $length
132
     *
133 1
     * @return ArrayEnsurance
134 1
     */
135
    public function isShorterThan(int $length): self
136 1
    {
137
        $count = count($this->value);
138
        $this->ensure($count < $length)->orThrow('array is not shorter than %d (%d)', $length, $count);
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param int $length
145
     *
146
     * @return ArrayEnsurance
147
     */
148
    public function isShorterOrEqualsTo(int $length): self
149
    {
150
        $count = count($this->value);
151
        $this->ensure($count <= $length)->orThrow('array is not shorter or equal to %d (%d)', $length, $count);
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param int $length
158
     *
159
     * @return ArrayEnsurance
160
     */
161
    public function isLongerThan(int $length): self
162
    {
163
        $count = count($this->value);
164
        $this->ensure($count > $length)->orThrow('array is longer than %d (%d)', $length, $count);
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param int $length
171
     *
172
     * @return ArrayEnsurance
173
     */
174
    public function isLongerOrEqualTo(int $length): self
175
    {
176
        $count = count($this->value);
177
        $this->ensure($count >= $length)->orThrow('array is not longer or equal to %d (%d)', $length, $count);
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return ArrayEnsurance
184
     */
185
    public function isAssociative(): self
186
    {
187
        $count = count($this->value);
188
        $this->ensure(array_keys($this->value) !== range(0, $count - 1))
189
             ->orThrow('array %s is not associative', $this->value);
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return ArrayEnsurance
196
     */
197
    public function isNotAssociative(): self
198
    {
199
        $count = count($this->value);
200
        $this->ensure(array_keys($this->value) === range(0, $count - 1))
201
             ->orThrow('array %s is associative', $this->value);
202
203
        return $this;
204
    }
205
206
    /**
207
     * @return ArrayEnsurance
208
     */
209
    public function isCallable(): self
210
    {
211
        $this->ensure(is_callable($this->value))->orThrow('Value is not a callable');
212
213
        return $this;
214
    }
215
}
216