Issues (22)

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/Statement.php (1 issue)

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
declare(strict_types=1);
4
5
namespace Adgoal\DBALFaultTolerance;
6
7
use Adgoal\DBALFaultTolerance\Events\Args\ReconnectEventArgs;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use IteratorAggregate;
10
use PDO;
11
use Throwable;
12
use Traversable;
13
14
/**
15
 * Class Statement.
16
 */
17
class Statement implements IteratorAggregate, DriverStatement
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $sql;
23
24
    /**
25
     * @var \Doctrine\DBAL\Statement
26
     */
27
    protected $stmt;
28
29
    /**
30
     * @var ConnectionInterface
31
     */
32
    protected $conn;
33
34
    /**
35
     * @var array
36
     */
37
    private $boundValues = [];
38
39
    /**
40
     * @var array
41
     */
42
    private $boundParams = [];
43
44
    /**
45
     * @var array|null
46
     */
47
    private $fetchMode;
48
49
    /**
50
     * Statement constructor.
51
     *
52
     * @param string              $sql
53
     * @param ConnectionInterface $conn
54
     */
55 8
    public function __construct(string $sql, ConnectionInterface $conn)
56
    {
57 8
        $this->sql = $sql;
58 8
        $this->conn = $conn;
59 8
        $this->createStatement();
60
    }
61
62
    /**
63
     * @param array|null $params
64
     *
65
     * @return bool
66
     *
67
     * @throws Throwable
68
     */
69 6
    public function execute($params = null)
70
    {
71 6
        $stmt = false;
72 6
        $attempt = 0;
73 6
        $retry = true;
74 6
        while ($retry) {
75 6
            $retry = false;
76
77
            try {
78 6
                $stmt = $this->stmt->execute($params);
79 5
            } catch (Throwable $e) {
80
                if (
81 5
                    $this->conn->canTryAgain($attempt)
82
                    &&
83 5
                    $this->conn->isRetryableException($e, $this->sql)
84
                ) {
85 4
                    $this->conn->close();
86 4
                    $this->recreateStatement();
87 4
                    ++$attempt;
88 4
                    $retry = true;
89
90 4
                    $this->conn->getEventManager()->dispatchEvent(
91 4
                        Events\Events::RECONNECT_TO_DATABASE,
92 4
                        new ReconnectEventArgs(__FUNCTION__, $attempt, $this->sql)
93
                    );
94
                } else {
95 2
                    throw $e;
96
                }
97
            }
98
        }
99
100 4
        return $stmt;
101
    }
102
103
    /**
104
     * @param string $name
105
     * @param mixed  $value
106
     * @param mixed  $type
107
     *
108
     * @return bool
109
     */
110 3
    public function bindValue($name, $value, $type = PDO::PARAM_STR)
111
    {
112 3
        if ($this->stmt->bindValue($name, $value, $type)) {
113 2
            $this->boundValues[$name] = [$name, $value, $type];
114
115 2
            return true;
116
        }
117
118 1
        return false;
119
    }
120
121
    /**
122
     * @param string   $name
123
     * @param mixed    $var
124
     * @param int      $type
125
     * @param int|null $length
126
     *
127
     * @return bool
128
     */
129 3
    public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null)
130
    {
131 3
        if ($this->stmt->bindParam($name, $var, $type, $length)) {
132 2
            $this->boundParams[$name] = [$name, &$var, $type, $length];
133
134 2
            return true;
135
        }
136
137 1
        return false;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function closeCursor()
144
    {
145
        return $this->stmt->closeCursor();
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function columnCount()
152
    {
153
        return $this->stmt->columnCount();
154
    }
155
156
    /**
157
     * @return string|int|bool
158
     */
159
    public function errorCode()
160
    {
161
        return $this->stmt->errorCode();
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function errorInfo()
168
    {
169
        return $this->stmt->errorInfo();
170
    }
171
172
    /**
173
     * @param int   $fetchMode
174
     * @param mixed $arg2
175
     * @param mixed $arg3
176
     *
177
     * @return bool
178
     */
179 3
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
180
    {
181 3
        if ($this->stmt->setFetchMode($fetchMode, $arg2, $arg3)) {
182 2
            $this->fetchMode = [$fetchMode, $arg2, $arg3];
183
184 2
            return true;
185
        }
186
187 1
        return false;
188
    }
189
190
    /**
191
     * @return Traversable
192
     */
193
    public function getIterator()
194
    {
195
        return $this->stmt;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->stmt; (Doctrine\DBAL\Statement) is incompatible with the return type declared by the interface IteratorAggregate::getIterator of type Traversable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
196
    }
197
198
    /**
199
     * @param int|null $fetchMode
200
     * @param int      $cursorOrientation Only for doctrine/DBAL >= 2.6
201
     * @param int      $cursorOffset      Only for doctrine/DBAL >= 2.6
202
     *
203
     * @return mixed
204
     */
205
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
206
    {
207
        return $this->stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset);
208
    }
209
210
    /**
211
     * @param int|null $fetchMode
212
     * @param int      $fetchArgument Only for doctrine/DBAL >= 2.6
213
     * @param null     $ctorArgs      Only for doctrine/DBAL >= 2.6
214
     *
215
     * @return mixed
216
     */
217
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
218
    {
219
        return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
220
    }
221
222
    /**
223
     * @param int $columnIndex
224
     *
225
     * @return mixed
226
     */
227
    public function fetchColumn($columnIndex = 0)
228
    {
229
        return $this->stmt->fetchColumn($columnIndex);
230
    }
231
232
    /**
233
     * @return int
234
     */
235
    public function rowCount()
236
    {
237
        return $this->stmt->rowCount();
238
    }
239
240
    /**
241
     * Create Statement.
242
     */
243 8
    private function createStatement()
244
    {
245 8
        $this->stmt = $this->conn->prepareUnwrapped($this->sql);
246
    }
247
248
    /**
249
     * Recreate statement for retry.
250
     */
251 4
    private function recreateStatement()
252
    {
253 4
        $this->createStatement();
254 4
        if ($this->fetchMode !== null) {
255 1
            call_user_func_array([$this->stmt, 'setFetchMode'], $this->fetchMode);
256
        }
257 4
        foreach ($this->boundValues as $boundValue) {
258 1
            call_user_func_array([$this->stmt, 'bindValue'], $boundValue);
259
        }
260 4
        foreach ($this->boundParams as $boundParam) {
261 1
            call_user_func_array([$this->stmt, 'bindParam'], $boundParam);
262
        }
263
    }
264
}
265