Completed
Pull Request — master (#3455)
by Daniel
25:57
created

PDOConnection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 14
eloc 29
dl 0
loc 98
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresQueryForServerVersion() 0 3 1
A prepare() 0 6 2
A __construct() 0 8 2
A query() 0 9 2
A getServerVersion() 0 3 1
A exec() 0 6 2
A lastInsertId() 0 10 3
A quote() 0 3 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\ParameterType;
6
use PDO;
7
use function assert;
8
9
/**
10
 * PDO implementation of the Connection interface.
11
 * Used by all PDO-based drivers.
12
 */
13
class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
14
{
15
    /**
16
     * @param string       $dsn
17
     * @param string|null  $user
18
     * @param string|null  $password
19
     * @param mixed[]|null $options
20
     *
21
     * @throws PDOException In case of an error.
22
     */
23 1114
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
24
    {
25
        try {
26 1114
            parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
27 1075
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
28 1075
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
29 71
        } catch (\PDOException $exception) {
30 71
            throw new PDOException($exception);
31
        }
32 1075
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 3444
    public function exec($statement)
38
    {
39
        try {
40 3444
            return parent::exec($statement);
41 1984
        } catch (\PDOException $exception) {
42 1984
            throw new PDOException($exception);
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 425
    public function getServerVersion()
50
    {
51 425
        return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2617
    public function prepare($prepareString, $driverOptions = [])
58
    {
59
        try {
60 2617
            return parent::prepare($prepareString, $driverOptions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::prepare($...String, $driverOptions) returns the type PDOStatement|boolean which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::prepare() of Doctrine\DBAL\Driver\Statement.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
61 36
        } catch (\PDOException $exception) {
62 36
            throw new PDOException($exception);
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 3602
    public function query(...$args)
70
    {
71
        try {
72 3602
            $stmt = 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

72
            $stmt = parent::query(/** @scrutinizer ignore-type */ ...$args);
Loading history...
73 3483
            assert($stmt instanceof \PDOStatement);
74
75 3483
            return $stmt;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $stmt returns the type PDOStatement which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::query() of Doctrine\DBAL\Driver\Statement.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
76 119
        } catch (\PDOException $exception) {
77 119
            throw new PDOException($exception);
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 159
    public function quote($input, $type = ParameterType::STRING)
85
    {
86 159
        return parent::quote($input, $type);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 95
    public function lastInsertId($name = null)
93
    {
94
        try {
95 95
            if ($name === null) {
96 18
                return parent::lastInsertId();
97
            }
98
99 77
            return parent::lastInsertId($name);
100
        } catch (\PDOException $exception) {
101
            throw new PDOException($exception);
102
        }
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 442
    public function requiresQueryForServerVersion()
109
    {
110 442
        return false;
111
    }
112
}
113