Completed
Push — master ( 3a0a1d...372fe8 )
by Sergei
19s queued 14s
created

Connection::connect()   C

Complexity

Conditions 14
Paths 37

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 14.0891

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 37
ccs 24
cts 26
cp 0.9231
rs 6.2666
c 0
b 0
f 0
cc 14
nc 37
nop 0
crap 14.0891

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 128
    public function connect()
43
    {
44 128
        $ret = parent::connect();
45 128
        if ($ret) {
46 128
            $params = $this->getParams();
47 128
            if (isset($params['portability'])) {
48 128
                if ($this->getDatabasePlatform()->getName() === 'oracle') {
49 10
                    $params['portability'] &= self::PORTABILITY_ORACLE;
50 118
                } elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
51 35
                    $params['portability'] &= self::PORTABILITY_POSTGRESQL;
52 83
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
53 4
                    $params['portability'] &= self::PORTABILITY_SQLITE;
54 79
                } elseif ($this->getDatabasePlatform()->getName() === 'drizzle') {
55
                    $params['portability'] &= self::PORTABILITY_DRIZZLE;
56 79
                } elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
57
                    $params['portability'] &= self::PORTABILITY_SQLANYWHERE;
58 79
                } elseif ($this->getDatabasePlatform()->getName() === 'db2') {
59 5
                    $params['portability'] &= self::PORTABILITY_DB2;
60 74
                } elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
61 9
                    $params['portability'] &= self::PORTABILITY_SQLSRV;
62
                } else {
63 65
                    $params['portability'] &= self::PORTABILITY_OTHERVENDORS;
64
                }
65 128
                $this->portability = $params['portability'];
66
            }
67
68 128
            if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) {
69 128
                if ($this->_conn instanceof PDOConnection) {
70
                    // make use of c-level support for case handling
71 83
                    $this->_conn->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
72
                } else {
73 45
                    $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
74
                }
75
            }
76
        }
77
78 128
        return $ret;
79
    }
80
81
    /**
82
     * @return int
83
     */
84 128
    public function getPortability()
85
    {
86 128
        return $this->portability;
87
    }
88
89
    /**
90
     * @return int
91
     */
92 128
    public function getFetchCase()
93
    {
94 128
        return $this->case;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 122
    public function executeQuery($query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null)
101
    {
102 122
        $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
103 122
        $stmt->setFetchMode($this->defaultFetchMode);
104
105 122
        return $stmt;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 122
    public function prepare($statement)
112
    {
113 122
        $stmt = new Statement(parent::prepare($statement), $this);
114 122
        $stmt->setFetchMode($this->defaultFetchMode);
115
116 122
        return $stmt;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 128
    public function query()
123
    {
124 128
        $connection = $this->getWrappedConnection();
125
126 128
        $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 128
        $stmt = new Statement($stmt, $this);
128 128
        $stmt->setFetchMode($this->defaultFetchMode);
129
130 128
        return $stmt;
131
    }
132
}
133