Failed Conditions
Push — phpstan ( 752f9f...1acc4a )
by Michael
21:57
created

Connection::getPortability()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 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\Portability;
21
22
use Doctrine\DBAL\Cache\QueryCacheProfile;
23
use Doctrine\DBAL\ColumnCase;
24
use const CASE_LOWER;
25
use const CASE_UPPER;
26
use function func_get_args;
27
28
/**
29
 * Portability wrapper for a Connection.
30
 *
31
 * @link   www.doctrine-project.org
32
 * @since  2.0
33
 * @author Benjamin Eberlei <[email protected]>
34
 */
35
class Connection extends \Doctrine\DBAL\Connection
36
{
37
    const PORTABILITY_ALL               = 255;
38
    const PORTABILITY_NONE              = 0;
39
    const PORTABILITY_RTRIM             = 1;
40
    const PORTABILITY_EMPTY_TO_NULL     = 4;
41
    const PORTABILITY_FIX_CASE          = 8;
42
43
    const PORTABILITY_DB2               = 13;
44
    const PORTABILITY_ORACLE            = 9;
45
    const PORTABILITY_POSTGRESQL        = 13;
46
    const PORTABILITY_SQLITE            = 13;
47
    const PORTABILITY_OTHERVENDORS      = 12;
48
    const PORTABILITY_DRIZZLE           = 13;
49
    const PORTABILITY_SQLANYWHERE       = 13;
50
    const PORTABILITY_SQLSRV            = 13;
51
52
    /**
53
     * @var int
54
     */
55
    private $portability = self::PORTABILITY_NONE;
56
57
    /**
58
     * @var int
59
     */
60
    private $case;
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 102
    public function connect()
66
    {
67 102
        $ret = parent::connect();
68 102
        if ($ret) {
69 102
            $params = $this->getParams();
70 102
            if (isset($params['portability'])) {
71 102
                if ($this->getDatabasePlatform()->getName() === "oracle") {
72
                    $params['portability'] = $params['portability'] & self::PORTABILITY_ORACLE;
73 102
                } elseif ($this->getDatabasePlatform()->getName() === "postgresql") {
74 30
                    $params['portability'] = $params['portability'] & self::PORTABILITY_POSTGRESQL;
75 72
                } elseif ($this->getDatabasePlatform()->getName() === "sqlite") {
76 5
                    $params['portability'] = $params['portability'] & self::PORTABILITY_SQLITE;
77 67
                } elseif ($this->getDatabasePlatform()->getName() === "drizzle") {
78
                    $params['portability'] = $params['portability'] & self::PORTABILITY_DRIZZLE;
79 67
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
80
                    $params['portability'] = $params['portability'] & self::PORTABILITY_SQLANYWHERE;
81 67
                } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
82
                    $params['portability'] = $params['portability'] & self::PORTABILITY_DB2;
83 67
                } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
84 27
                    $params['portability'] = $params['portability'] & self::PORTABILITY_SQLSRV;
85
                } else {
86 40
                    $params['portability'] = $params['portability'] & self::PORTABILITY_OTHERVENDORS;
87
                }
88 102
                $this->portability = $params['portability'];
89
            }
90
91 102
            if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) {
92 85
                if ($this->_conn instanceof \Doctrine\DBAL\Driver\PDOConnection) {
93
                    // make use of c-level support for case handling
94 65
                    $this->_conn->setAttribute(\PDO::ATTR_CASE, $params['fetch_case']);
95
                } else {
96 20
                    $this->case = ($params['fetch_case'] === ColumnCase::LOWER) ? CASE_LOWER : CASE_UPPER;
97
                }
98
            }
99
        }
100
101 102
        return $ret;
102
    }
103
104
    /**
105
     * @return int
106
     */
107 102
    public function getPortability()
108
    {
109 102
        return $this->portability;
110
    }
111
112
    /**
113
     * @return int
114
     */
115 85
    public function getFetchCase()
116
    {
117 85
        return $this->case;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 34
    public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null)
124
    {
125 34
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
126 34
        $stmt->setFetchMode($this->defaultFetchMode);
127
128 34
        return $stmt;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 34
    public function prepare($statement)
135
    {
136 34
        $stmt = new Statement(parent::prepare($statement), $this);
137 34
        $stmt->setFetchMode($this->defaultFetchMode);
138
139 34
        return $stmt;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 85
    public function query()
146
    {
147 85
        $this->connect();
148
149 85
        $stmt = $this->_conn->query(...func_get_args());
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Driver\Connection::query() has too many arguments starting with func_get_args(). ( Ignorable by Annotation )

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

149
        /** @scrutinizer ignore-call */ 
150
        $stmt = $this->_conn->query(...func_get_args());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
The method query() does not exist on null. ( Ignorable by Annotation )

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

149
        /** @scrutinizer ignore-call */ 
150
        $stmt = $this->_conn->query(...func_get_args());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150 85
        $stmt = new Statement($stmt, $this);
151 85
        $stmt->setFetchMode($this->defaultFetchMode);
152
153 85
        return $stmt;
154
    }
155
}
156