Statement   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 210
Duplicated Lines 3.81 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 23
c 7
b 1
f 0
lcom 1
cbo 3
dl 8
loc 210
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createStatement() 0 12 3
B execute() 8 25 4
A bindValue() 0 6 1
A bindParam() 0 6 1
A closeCursor() 0 4 1
A columnCount() 0 4 1
A errorCode() 0 4 1
A errorInfo() 0 4 1
A fetch() 0 4 1
A fetchAll() 0 4 1
A fetchColumn() 0 4 1
A rowCount() 0 4 1
A setFetchMode() 0 16 4
A getIterator() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace BsbDoctrineReconnect\DBAL;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver\Statement as DriverStatement;
7
use IteratorAggregate;
8
use PDO;
9
10
/**
11
 * Class Statement
12
 *
13
 * @package BsbDoctrineReconnect\DBAL
14
 */
15
class Statement implements IteratorAggregate, DriverStatement
16
{
17
    /**
18
     * @var string
19
     */
20
    private $sql;
21
22
    /**
23
     * @var \Doctrine\DBAL\Statement
24
     */
25
    private $stmt;
26
27
    /**
28
     * @var Connection
29
     */
30
    private $conn;
31
32
    /**
33
     * @var array binding values
34
     */
35
    private $values = [];
36
37
    /**
38
     * @var array binding parameters
39
     */
40
    private $params = [];
41
42
    /**
43
     * @param            string $sql
44
     * @param Connection        $conn
45
     */
46
    public function __construct($sql, Connection $conn)
47
    {
48
        $this->sql  = $sql;
49
        $this->conn = $conn;
50
51
        $this->createStatement();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    private function createStatement()
58
    {
59
        $this->stmt = $this->conn->prepareUnwrapped($this->sql);
0 ignored issues
show
Deprecated Code introduced by
The method BsbDoctrineReconnect\DBA...ion::prepareUnwrapped() has been deprecated.

This method has been deprecated.

Loading history...
60
61
        foreach ($this->values as $args) {
62
            $this->bindValue($args[0], $args[1], $args[2]);
63
        }
64
65
        foreach ($this->params as $args) {
66
            $this->bindParam($args[0], $args[1], $args[2]);
67
        }
68
    }
69
70
    /**
71
     * @param null $params
72
     * @return bool|null
73
     * @throws DBALException
74
     */
75
    public function execute($params = null)
76
    {
77
        $stmt    = null;
78
        $attempt = 0;
79
        $retry   = true;
80
81
        while ($retry) {
82
            $retry = false;
83
84
            try {
85
                $stmt = $this->stmt->execute($params);
86
            } catch (DBALException $e) {
87 View Code Duplication
                if ($this->conn->validateReconnectAttempt($e, $attempt)) {
0 ignored issues
show
Duplication introduced by
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...
88
                    $this->conn->close();
89
                    $this->createStatement();
90
                    $attempt++;
91
                    $retry = true;
92
                } else {
93
                    throw $e;
94
                }
95
            }
96
        }
97
98
        return $stmt;
99
    }
100
101
    /**
102
     * @param mixed $param
103
     * @param mixed $value
104
     * @param null  $type
105
     * @return bool
106
     */
107
    public function bindValue($param, $value, $type = null)
108
    {
109
        $this->values[$param] = [$param, $value, $type];
110
111
        return $this->stmt->bindValue($param, $value, $type);
112
    }
113
114
    /**
115
     * @param mixed    $column
116
     * @param mixed    $variable
117
     * @param int|null $type
118
     * @param null     $length
119
     * @return bool
120
     */
121
    public function bindParam($column, &$variable, $type = PDO::PARAM_STR, $length = null)
122
    {
123
        $this->values[$column] = [$column, &$variable, $type];
124
125
        return $this->stmt->bindParam($column, $variable, $type);
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function closeCursor()
132
    {
133
        return $this->stmt->closeCursor();
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function columnCount()
140
    {
141
        return $this->stmt->columnCount();
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function errorCode()
148
    {
149
        return $this->stmt->errorCode();
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    public function errorInfo()
156
    {
157
        return $this->stmt->errorInfo();
158
    }
159
160
    /**
161
     * @param int|null $fetchStyle
162
     * @return mixed
163
     */
164
    public function fetch($fetchStyle = PDO::FETCH_BOTH)
165
    {
166
        return $this->stmt->fetch($fetchStyle);
167
    }
168
169
    /**
170
     * @param int|null $fetchStyle
171
     * @return array
172
     */
173
    public function fetchAll($fetchStyle = PDO::FETCH_BOTH)
174
    {
175
        return $this->stmt->fetchAll($fetchStyle);
176
    }
177
178
    /**
179
     * @param int $columnIndex
180
     * @return mixed
181
     */
182
    public function fetchColumn($columnIndex = 0)
183
    {
184
        return $this->stmt->fetchColumn($columnIndex);
185
    }
186
187
    /**
188
     * @return int
189
     */
190
    public function rowCount()
191
    {
192
        return $this->stmt->rowCount();
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
199
    {
200
        // This thin wrapper is necessary to shield against the weird signature
201
        // of PDOStatement::setFetchMode(): even if the second and third
202
        // parameters are optional, PHP will not let us remove it from this
203
        // declaration.
204
        if ($arg2 === null && $arg3 === null) {
205
            return parent::setFetchMode($fetchMode);
206
        }
207
208
        if ($arg3 === null) {
209
            return parent::setFetchMode($fetchMode, $arg2);
210
        }
211
212
        return parent::setFetchMode($fetchMode, $arg2, $arg3);
213
    }
214
215
    /**
216
     * Required by interface IteratorAggregate.
217
     *
218
     * {@inheritdoc}
219
     */
220
    public function getIterator()
221
    {
222
        return $this->stmt;
223
    }
224
}
225