Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
06:55
created

Connection::connect()   C

Complexity

Conditions 13
Paths 33

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13.33

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 35
ccs 21
cts 24
cp 0.875
rs 6.6166
c 0
b 0
f 0
cc 13
nc 33
nop 0
crap 13.33

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