Issues (11)

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/Evacuator.php (11 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 declare(strict_types = 1);
2
/**
3
 * This file is part of Evacuator package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace Serafim\Evacuator;
9
10
/**
11
 * Class Evacuator
12
 * @package Serafim\Evacuator
13
 */
14
class Evacuator
15
{
16
    /**
17
     * @var string
18
     * @internal
19
     */
20
    const CATCH_ALL_EXCEPTIONS = '*';
21
22
    /**
23
     * @var int
24
     */
25
    const INFINITY_RETRIES = -100;
26
27
    /**
28
     * @var \Closure
29
     */
30
    private $context;
31
32
    /**
33
     * @var int
34
     */
35
    private $retries = 0;
36
37
    /**
38
     * @var array|\Closure[]
39
     */
40
    private $catches = [];
41
42
    /**
43
     * @var null|\Closure
44
     */
45
    private $then;
46
47
    /**
48
     * @var array|\Closure
49
     */
50
    private $everyError = [];
51
52
    /**
53
     * Evacuator constructor.
54
     * @param \Closure $context
55
     */
56
    public function __construct(\Closure $context)
57
    {
58
        $this->context = $context;
59
    }
60
61
    /**
62
     * @param array ...$args
63
     * @return mixed
64
     * @throws \Throwable
65
     */
66
    public function __invoke(...$args)
67
    {
68
        return $this->invoke(...$args);
69
    }
70
71
    /**
72
     * @param int $count
73
     * @return Evacuator
74
     * @throws \InvalidArgumentException
75
     */
76
    public function retries(int $count): Evacuator
77
    {
78
        if ($count < 0 && $count !== static::INFINITY_RETRIES) {
79
            throw new \InvalidArgumentException('Retries count must be greater than 0');
80
        }
81
82
        $this->retries = $count;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param \Closure $then
89
     * @return Evacuator|$this
90
     * @throws \InvalidArgumentException
91
     */
92 View Code Duplication
    public function catch(\Closure $then): Evacuator
0 ignored issues
show
This method seems to be duplicated in 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...
Possible parse error: non-abstract method defined as abstract
Loading history...
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
93
    {
94
        $exceptionClass = $this->resolveTypeHint($then, \Throwable::class);
95
96
        $this->catches[$exceptionClass] = $then;
0 ignored issues
show
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
97
98
        return $this;
0 ignored issues
show
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
99
    }
100
101
    /**
102
     * @param \Closure $then
103
     * @return Evacuator
104
     * @throws \InvalidArgumentException
105
     */
106 View Code Duplication
    public function onError(\Closure $then): Evacuator
0 ignored issues
show
This method seems to be duplicated in 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...
107
    {
108
        $exceptionClass = $this->resolveTypeHint($then, \Throwable::class);
109
110
        $this->everyError[$exceptionClass] = $then;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param \Closure $closure
117
     * @param string $instanceOf
118
     * @return string
119
     * @throws \InvalidArgumentException
120
     */
121
    private function resolveTypeHint(\Closure $closure, string $instanceOf): string
122
    {
123
        $parameters = (new \ReflectionFunction($closure))->getParameters();
124
125
        // Callback has ony one argument
126
        if (1 !== count($parameters)) {
127
            throw new \InvalidArgumentException(
128
                'Closure argument of catch(...) method required ' . (count($parameters) > 1 ? 'only ' :'') .
129
                '1 parameter ' . count($parameters) . ' given'
130
            );
131
        }
132
133
        // Callback has no type hints
134
        if (null === reset($parameters)->getType()) {
135
            return static::CATCH_ALL_EXCEPTIONS;
136
        }
137
138
        $typeHintClass = reset($parameters)->getClass();
139
140
        // Callback has primitive type hint
141
        if (null === $typeHintClass) {
142
            throw new \InvalidArgumentException(
143
                'Closure argument of catch(...) method type hint can not be a primitive'
144
            );
145
        }
146
147
        if (!($typeHintClass->newInstanceWithoutConstructor() instanceof $instanceOf)) {
148
            throw new \InvalidArgumentException(
149
                'Closure argument of catch(...) method type hint must be instance of ' . $instanceOf .
150
                    ', ' . $typeHintClass->name . ' given'
151
            );
152
        }
153
154
        return $typeHintClass->name;
155
    }
156
157
    /**
158
     * @param array ...$args
159
     * @return mixed
160
     * @throws \Throwable
161
     */
162
    public function invoke(...$args)
163
    {
164
        $result = null;
165
        $error  = null;
166
167
        try {
168
            $result = $this->callClosure(...$args);
169
        } catch (\Throwable $e) {
170
            $error = $e;
171
        }
172
173
        if ($this->then) {
174
            return ($this->then)($result ?? $error);
175
        }
176
177
        if ($error !== null) {
178
            throw $error;
179
        }
180
181
        return $result;
182
    }
183
184
    /**
185
     * @param array ...$args
186
     * @return mixed
187
     * @throws \Throwable
188
     */
189
    private function callClosure(...$args)
190
    {
191
        while (
192
            $this->retries === static::INFINITY_RETRIES ||
193
            ($this->retries-- + 1) > 0
194
        ) {
195
            try {
196
                return ($this->context)(...$args);
197
            } catch (\Throwable $e) {
198
                $this->throw($e, $this->everyError);
0 ignored issues
show
It seems like $this->everyError can also be of type object<Closure>; however, Serafim\Evacuator\Evacuator::throw() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
199
200
                if ($this->willBeThrows()) {
201
                    return $this->throw($e, $this->catches, true);
202
                }
203
            }
204
        }
205
206
        return null;
207
    }
208
209
    /**
210
     * @return bool
211
     * @throws \Throwable
212
     */
213
    private function willBeThrows(): bool
214
    {
215
        return $this->retries !== static::INFINITY_RETRIES && $this->retries < 0;
216
    }
217
218
    /**
219
     * @param \Throwable $e
220
     * @param array $callbacks
221
     * @param bool $throwAfter
222
     * @return mixed
223
     * @throws \Throwable
224
     */
225
    private function throw(\Throwable $e, array $callbacks, bool $throwAfter = false)
226
    {
227
        foreach ($callbacks as $name => $callback) {
228
            if ($e instanceof $name || $name === static::CATCH_ALL_EXCEPTIONS) {
229
                return $callback($e);
230
            }
231
        }
232
233
        if ($throwAfter) {
234
            throw $e;
235
        }
236
237
        return null;
238
    }
239
240
    /**
241
     * @param \Closure $then
242
     * @return Evacuator|$this
243
     */
244
    public function finally(\Closure $then): Evacuator
0 ignored issues
show
Possible parse error: non-abstract method defined as abstract
Loading history...
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
245
    {
246
        $this->then = $then;
247
248
        return $this;
0 ignored issues
show
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
249
    }
250
}
251