Completed
Push — master ( 11b67b...3a0a1d )
by Sergei
19:09 queued 19:04
created

Connection::connect()   C

Complexity

Conditions 14
Paths 37

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 14.3012

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 37
rs 6.2666
c 0
b 0
f 0
ccs 23
cts 26
cp 0.8846
cc 14
nc 37
nop 0
crap 14.3012

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\DBAL\Portability;
4
5
use Doctrine\DBAL\Cache\QueryCacheProfile;
6
use Doctrine\DBAL\ColumnCase;
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use PDO;
9
use const CASE_LOWER;
10
use const CASE_UPPER;
11
use function func_get_args;
12
13
/**
14
 * Portability wrapper for a Connection.
15
 */
16
class Connection extends \Doctrine\DBAL\Connection
17
{
18
    public const PORTABILITY_ALL           = 255;
19
    public const PORTABILITY_NONE          = 0;
20
    public const PORTABILITY_RTRIM         = 1;
21
    public const PORTABILITY_EMPTY_TO_NULL = 4;
22
    public const PORTABILITY_FIX_CASE      = 8;
23
24
    public const PORTABILITY_DB2          = 13;
25
    public const PORTABILITY_ORACLE       = 9;
26
    public const PORTABILITY_POSTGRESQL   = 13;
27
    public const PORTABILITY_SQLITE       = 13;
28
    public const PORTABILITY_OTHERVENDORS = 12;
29
    public const PORTABILITY_DRIZZLE      = 13;
30
    public const PORTABILITY_SQLANYWHERE  = 13;
31
    public const PORTABILITY_SQLSRV       = 13;
32
33
    /** @var int */
34
    private $portability = self::PORTABILITY_NONE;
35
36
    /** @var int */
37
    private $case;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 138
    public function connect()
43
    {
44 138
        $ret = parent::connect();
45 138
        if ($ret) {
46 138
            $params = $this->getParams();
47 138
            if (isset($params['portability'])) {
48 138
                if ($this->getDatabasePlatform()->getName() === 'oracle') {
49 10
                    $params['portability'] &= self::PORTABILITY_ORACLE;
50 128
                } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
51 35
                    $params['portability'] &= self::PORTABILITY_POSTGRESQL;
52 93
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
53
                    $params['portability'] &= self::PORTABILITY_SQLITE;
54 93
                } elseif ($this->getDatabasePlatform()->getName() === 'drizzle') {
55
                    $params['portability'] &= self::PORTABILITY_DRIZZLE;
56 93
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
57
                    $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
58 93
                } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
59 5
                    $params['portability'] &= self::PORTABILITY_DB2;
60 88
                } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
61 9
                    $params['portability'] &= self::PORTABILITY_SQLSRV;
62
                } else {
63 79
                    $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
64
                }
65 138
                $this->portability = $params['portability'];
66
            }
67
68 138
            if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) {
69 138
                if ($this->_conn instanceof PDOConnection) {
70
                    // make use of c-level support for case handling
71 89
                    $this->_conn->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
72
                } else {
73 49
                    $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
74
                }
75
            }
76
        }
77
78 138
        return $ret;
79
    }
80
81
    /**
82
     * @return int
83
     */
84 138
    public function getPortability()
85
    {
86 138
        return $this->portability;
87
    }
88
89
    /**
90
     * @return int
91
     */
92 138
    public function getFetchCase()
93
    {
94 138
        return $this->case;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 132
    public function executeQuery($query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null)
101
    {
102 132
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
103 132
        $stmt->setFetchMode($this->defaultFetchMode);
104
105 132
        return $stmt;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 132
    public function prepare($statement)
112
    {
113 132
        $stmt = new Statement(parent::prepare($statement), $this);
114 132
        $stmt->setFetchMode($this->defaultFetchMode);
115
116 132
        return $stmt;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 138
    public function query()
123
    {
124 138
        $connection = $this->getWrappedConnection();
125
126 138
        $stmt = $connection->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

126
        /** @scrutinizer ignore-call */ 
127
        $stmt = $connection->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...
127 138
        $stmt = new Statement($stmt, $this);
128 138
        $stmt->setFetchMode($this->defaultFetchMode);
129
130 138
        return $stmt;
131
    }
132
}
133