Failed Conditions
Pull Request — develop (#3367)
by Benjamin
11:06
created

PDOConnection::exceptionFromPDOException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\ParameterType;
6
use PDO;
7
use PDOException;
8
9
/**
10
 * PDO implementation of the Connection interface.
11
 *
12
 * Used by all PDO-based drivers.
13
 */
14
class PDOConnection implements Connection, ServerInfoAwareConnection
15
{
16
    /** @var PDO */
17
    private $connection;
18
19
    /**
20
     * @param string       $dsn
21
     * @param string|null  $user
22
     * @param string|null  $password
23
     * @param mixed[]|null $options
24
     *
25
     * @throws DriverException In case of an error.
26
     */
27 922
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
28
    {
29
        try {
30 922
            $this->connection = new PDO($dsn, $user, $password, $options);
31 891
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32 58
        } catch (PDOException $exception) {
33 58
            throw self::exceptionFromPDOException($exception);
34
        }
35 891
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2778
    public function exec(string $statement) : int
41
    {
42
        try {
43 2778
            return $this->connection->exec($statement);
44 1737
        } catch (PDOException $exception) {
45 1737
            throw self::exceptionFromPDOException($exception);
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 368
    public function getServerVersion()
53
    {
54 368
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2157
    public function prepare(string $sql) : Statement
61
    {
62
        try {
63 2157
            return $this->createStatement(
64 2157
                $this->connection->prepare($sql)
0 ignored issues
show
Bug introduced by
It seems like $this->connection->prepare($sql) can also be of type boolean; however, parameter $stmt of Doctrine\DBAL\Driver\PDO...tion::createStatement() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                /** @scrutinizer ignore-type */ $this->connection->prepare($sql)
Loading history...
65
            );
66 31
        } catch (PDOException $exception) {
67 31
            throw self::exceptionFromPDOException($exception);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2909
    public function query(string $sql) : ResultStatement
75
    {
76
        try {
77 2909
            return $this->createStatement(
78 2909
                $this->connection->query($sql)
0 ignored issues
show
Bug introduced by
It seems like $this->connection->query($sql) can also be of type boolean; however, parameter $stmt of Doctrine\DBAL\Driver\PDO...tion::createStatement() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                /** @scrutinizer ignore-type */ $this->connection->query($sql)
Loading history...
79
            );
80 99
        } catch (PDOException $exception) {
81 99
            throw self::exceptionFromPDOException($exception);
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 138
    public function quote($input, $type = ParameterType::STRING)
89
    {
90 138
        return $this->connection->quote($input, $type);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 82
    public function lastInsertId($name = null)
97
    {
98 82
        return $this->connection->lastInsertId($name);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 382
    public function requiresQueryForServerVersion()
105
    {
106 382
        return false;
107
    }
108
109
    /**
110
     * Creates a wrapped statement
111
     */
112 3671
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
113
    {
114 3671
        return new PDOStatement($stmt);
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 237
    public function beginTransaction()
121
    {
122 237
        return $this->connection->beginTransaction();
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 88
    public function commit()
129
    {
130 88
        return $this->connection->commit();
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136 139
    public function rollBack()
137
    {
138 139
        return $this->connection->rollBack();
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144
    public function errorCode()
145
    {
146
        return $this->connection->errorCode();
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152
    public function errorInfo()
153
    {
154
        return $this->connection->errorInfo();
155
    }
156
157 716
    public function getWrappedConnection() : PDO
158
    {
159 716
        return $this->connection;
160
    }
161
162
    /**
163
     * Creates a DriverException from a PDOException.
164
     */
165 2016
    public static function exceptionFromPDOException(PDOException $exception) : DriverException
166
    {
167 2016
        return new DriverException(
168 2016
            $exception->getMessage(),
169 2016
            $exception->errorInfo[0] ?? $exception->getCode(),
170 2016
            $exception->errorInfo[1] ?? $exception->getCode(),
171 2016
            $exception
172
        );
173
    }
174
}
175