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

Connection::connect()   C

Complexity

Conditions 13
Paths 33

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13.0976

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 33
nop 0
dl 0
loc 36
ccs 22
cts 24
cp 0.9167
crap 13.0976
rs 6.6166
c 0
b 0
f 0

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