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/EnsuranceTrait.php (3 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\Ensurance\Exception\EnsuranceException;
6
use Dgame\Type\Type;
7
use Dgame\Type\TypeFactory;
8
use Exception;
9
use Throwable;
10
11
/**
12
 * Trait EnsuranceTrait
13
 * @package Dgame\Ensurance
14
 */
15
trait EnsuranceTrait
16
{
17
    /**
18
     * @var mixed
19
     */
20
    private $value;
21
    /**
22
     * @var bool
23
     */
24
    private $ensured = true;
25
    /**
26
     * @var Throwable|null
27
     */
28
    private $throwable;
29
30
    /**
31
     * @throws Throwable
32
     */
33
    public function __destruct()
34
    {
35
        if ($this->throwable !== null) {
36
            throw $this->throwable;
37
        }
38
    }
39
40
    /**
41
     * @param callable $callback
42
     *
43
     * @return self
44
     */
45
    final public function is(callable $callback): self
46
    {
47
        try {
48
            $result = (bool) $callback($this->value);
49
        } catch (Throwable $t) {
50
            $result = false;
51
        }
52
53
        return $this->ensure($result);
54
    }
55
56
    /**
57
     * @param string $type
58
     *
59
     * @return self
60
     * @throws Exception
61
     */
62
    final public function isTypeOf(string $type): self
63
    {
64
        $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...
65
66
        return $this->ensure(TypeFactory::expression($this->value)->isSame($type));
67
    }
68
69
    /**
70
     * @param mixed $value
71
     *
72
     * @return mixed
73
     */
74
    final public function then($value)
75
    {
76
        return $this->disregardThrowable()->isEnsured() ? $value : $this->value;
77
    }
78
79
    /**
80
     * @param mixed $value
81
     *
82
     * @return mixed
83
     */
84
    final public function else($value)
85
    {
86
        return $this->disregardThrowable()->isEnsured() ? $this->value : $value;
87
    }
88
89
    /**
90
     * @param mixed $value
91
     *
92
     * @return Either
93
     */
94
    final public function either($value): Either
95
    {
96
        return new Either($value, $this->disregardThrowable()->isEnsured());
97
    }
98
99
    /**
100
     * @param mixed $default
101
     *
102
     * @return mixed
103
     */
104
    final public function get($default = null)
105
    {
106
        return $this->value ?? $default;
107
    }
108
109
    /**
110
     * @param bool $condition
111
     *
112
     * @return self
113
     */
114
    final protected function ensure(bool $condition): self
115
    {
116
        $this->ensured = $condition;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return bool
123
     */
124
    final public function isEnsured(): bool
125
    {
126
        return $this->ensured;
127
    }
128
129
    /**
130
     * @return self
131
     */
132
    final public function disregardThrowable(): self
133
    {
134
        $this->throwable = null;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @return Throwable|null
141
     */
142
    final public function releaseThrowable(): ?Throwable
143
    {
144
        try {
145
            return $this->throwable;
146
        } finally {
147
            $this->disregardThrowable();
0 ignored issues
show
$this->disregardThrowable(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
148
        }
149
    }
150
151
    /**
152
     * @param EnsuranceInterface $ensurance
153
     *
154
     * @return self
155
     */
156
    final public function transferEnsurance(EnsuranceInterface $ensurance): self
157
    {
158
        $this->value = $ensurance->get();
159
        $this->ensure($ensurance->isEnsured());
160
161
        $throwable = $ensurance->releaseThrowable();
162
        if ($throwable !== null) {
163
            $this->orThrowWith($throwable);
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param string $message
171
     * @param mixed  ...$args
172
     *
173
     * @return self
174
     */
0 ignored issues
show
Consider making the type for parameter $args a bit more specific; maybe use array.
Loading history...
175
    final public function orThrow(string $message, ...$args): self
176
    {
177
        if (!$this->isEnsured()) {
178
            $this->orThrowWith(new EnsuranceException(format($message, ...$args), $this->throwable));
179
        }
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return bool
186
     */
187
    final public function hasThrowable(): bool
188
    {
189
        return $this->throwable !== null;
190
    }
191
192
    /**
193
     * @param Throwable $throwable
194
     *
195
     * @return self
196
     * @deprecated Use "orThrowWith" instead
197
     *
198
     */
199
    final public function setThrowable(Throwable $throwable): self
200
    {
201
        return $this->orThrowWith($throwable);
202
    }
203
204
    /**
205
     * @param Throwable $throwable
206
     *
207
     * @return self
208
     */
209
    final public function orThrowWith(Throwable $throwable): self
210
    {
211
        if (!$this->isEnsured()) {
212
            $this->throwable = $throwable;
213
        }
214
215
        return $this;
216
    }
217
218
    /**
219
     * @return Throwable|null
220
     */
221
    final public function getThrowable(): ?Throwable
222
    {
223
        return $this->throwable;
224
    }
225
}
226