Failed Conditions
Pull Request — master (#3339)
by Sergei
40:43
created

PDOConnection::exec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\ParameterType;
6
use PDO;
7
use function count;
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 1057
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
25
    {
26
        try {
27 1057
            parent::__construct($dsn, $user, $password, $options);
28 1021
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]);
29 1021
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30 66
        } catch (\PDOException $exception) {
31 66
            throw new PDOException($exception);
32
        }
33 1021
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3151
    public function exec($statement)
39
    {
40
        try {
41 3151
            return parent::exec($statement);
42 1990
        } catch (\PDOException $exception) {
43 1990
            throw new PDOException($exception);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 395
    public function getServerVersion()
51
    {
52 395
        return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2441
    public function prepare($prepareString, $driverOptions = [])
59
    {
60
        try {
61 2441
            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...
62 32
        } catch (\PDOException $exception) {
63 32
            throw new PDOException($exception);
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3298
    public function query()
71
    {
72 3298
        $args      = func_get_args();
73 3298
        $argsCount = count($args);
74
75
        try {
76 3298
            if ($argsCount === 4) {
77
                return parent::query($args[0], $args[1], $args[2], $args[3]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::query($ar...1], $args[2], $args[3]) returns the type PDOStatement|boolean 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...
78
            }
79
80 3298
            if ($argsCount === 3) {
81
                return parent::query($args[0], $args[1], $args[2]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::query($args[0], $args[1], $args[2]) returns the type PDOStatement|boolean 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...
82
            }
83
84 3298
            if ($argsCount === 2) {
85
                return parent::query($args[0], $args[1]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::query($args[0], $args[1]) returns the type PDOStatement|boolean 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...
86
            }
87
88 3298
            return parent::query($args[0]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::query($args[0]) returns the type PDOStatement|boolean 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...
89 109
        } catch (\PDOException $exception) {
90 109
            throw new PDOException($exception);
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 148
    public function quote($input, $type = ParameterType::STRING)
98
    {
99 148
        return parent::quote($input, $type);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 93
    public function lastInsertId($name = null)
106
    {
107
        try {
108 93
            return parent::lastInsertId($name);
109
        } catch (\PDOException $exception) {
110
            throw new PDOException($exception);
111
        }
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 411
    public function requiresQueryForServerVersion()
118
    {
119 411
        return false;
120
    }
121
}
122