Failed Conditions
Pull Request — 2.10.x (#3977)
by Grégoire
19:24
created

PDOConnection::query()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 0
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\ParameterType;
6
use PDO;
7
use function assert;
8
use function func_get_args;
9
10
/**
11
 * PDO implementation of the Connection interface.
12
 * Used by all PDO-based drivers.
13
 */
14
class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection
15
{
16
    /**
17
     * @param string       $dsn
18
     * @param string|null  $user
19
     * @param string|null  $password
20
     * @param mixed[]|null $options
21
     *
22
     * @throws PDOException In case of an error.
23
     */
24 4575
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
25
    {
26
        try {
27 4575
            parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
28 4575
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
29 4575
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30 3991
        } catch (\PDOException $exception) {
31 3991
            throw new PDOException($exception);
32
        }
33 4575
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 4563
    public function exec($statement)
39
    {
40
        try {
41 4563
            return parent::exec($statement);
42 4561
        } catch (\PDOException $exception) {
43 4561
            throw new PDOException($exception);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4110
    public function getServerVersion()
51
    {
52 4110
        return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
53
    }
54
55
    /**
56
     * @param string          $prepareString
57
     * @param array<int, int> $driverOptions
58
     *
59
     * @return \PDOStatement
60
     */
61 4470
    public function prepare($prepareString, $driverOptions = [])
62
    {
63
        try {
64 4470
            $statement = parent::prepare($prepareString, $driverOptions);
65 4411
            assert($statement instanceof \PDOStatement);
66
67 4411
            return $statement;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $statement returns the type PDOStatement 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...
68 4468
        } catch (\PDOException $exception) {
69 4468
            throw new PDOException($exception);
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @return \PDOStatement
77
     */
78 4542
    public function query()
79
    {
80 4542
        $args = func_get_args();
81
82
        try {
83 4542
            $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

83
            $stmt = parent::query(/** @scrutinizer ignore-type */ ...$args);
Loading history...
84 4406
            assert($stmt instanceof \PDOStatement);
85
86 4406
            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...
87 4538
        } catch (\PDOException $exception) {
88 4538
            throw new PDOException($exception);
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 4156
    public function quote($input, $type = ParameterType::STRING)
96
    {
97 4156
        return parent::quote($input, $type);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 471
    public function lastInsertId($name = null)
104
    {
105
        try {
106 471
            if ($name === null) {
107 129
                return parent::lastInsertId();
108
            }
109
110 342
            return parent::lastInsertId($name);
111
        } catch (\PDOException $exception) {
112
            throw new PDOException($exception);
113
        }
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 4299
    public function requiresQueryForServerVersion()
120
    {
121 4299
        return false;
122
    }
123
}
124