Failed Conditions
Pull Request — master (#2958)
by Sergei
38:14
created

PDOConnection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 103
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresQueryForServerVersion() 0 3 1
A prepare() 0 8 2
A __construct() 0 7 2
A query() 0 10 2
A getServerVersion() 0 3 1
A exec() 0 6 2
A createStatement() 0 3 1
A lastInsertId() 0 3 1
A quote() 0 3 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver;
21
22
use PDO;
23
24
/**
25
 * PDO implementation of the Connection interface.
26
 * Used by all PDO-based drivers.
27
 *
28
 * @since 2.0
29
 */
30
class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
31
{
32
    /**
33
     * @param string      $dsn
34
     * @param string|null $user
35
     * @param string|null $password
36
     * @param array|null  $options
37
     *
38
     * @throws PDOException in case of an error.
39
     */
40 47
    public function __construct($dsn, $user = null, $password = null, array $options = null)
41
    {
42
        try {
43 47
            parent::__construct($dsn, $user, $password, $options);
44 46
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
45 2
        } catch (\PDOException $exception) {
46 2
            throw new PDOException($exception);
47
        }
48 46
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 148
    public function exec($statement)
54
    {
55
        try {
56 148
            return parent::exec($statement);
57 54
        } catch (\PDOException $exception) {
58 54
            throw new PDOException($exception);
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getServerVersion()
66
    {
67
        return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
0 ignored issues
show
Bug Best Practice introduced by
The method PDO::getAttribute() is not static, but was called statically. ( Ignorable by Annotation )

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

67
        return PDO::/** @scrutinizer ignore-call */ getAttribute(PDO::ATTR_SERVER_VERSION);
Loading history...
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 131
    public function prepare($prepareString, $driverOptions = [])
74
    {
75
        try {
76 131
            return $this->createStatement(
77 131
                parent::prepare($prepareString, $driverOptions)
78
            );
79 3
        } catch (\PDOException $exception) {
80 3
            throw new PDOException($exception);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 173
    public function query()
88
    {
89 173
        $args = func_get_args();
90
91
        try {
92 173
            return $this->createStatement(
93 173
                parent::query(...$args)
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $statement of PDO::query() does not expect variable arguments. ( Ignorable by Annotation )

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

93
                parent::query(/** @scrutinizer ignore-type */ ...$args)
Loading history...
94
            );
95 6
        } catch (\PDOException $exception) {
96 6
            throw new PDOException($exception);
97
        }
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 4
    public function quote($input, $type = Statement::PARAM_STR)
104
    {
105 4
        return parent::quote($input, $type);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function lastInsertId($name = null)
112
    {
113 2
        return parent::lastInsertId($name);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1
    public function requiresQueryForServerVersion()
120
    {
121 1
        return false;
122
    }
123
124
    /**
125
     * Creates a wrapped statement
126
     *
127
     * @param \PDOStatement $stmt
128
     * @return PDOStatement
129
     */
130 234
    private function createStatement(\PDOStatement $stmt) : PDOStatement
131
    {
132 234
        return new PDOStatement($stmt);
133
    }
134
}
135