Failed Conditions
Push — develop ( b4c2cf...86369f )
by Sergei
18s queued 13s
created

Connection::connect()   C

Complexity

Conditions 13
Paths 33

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13.6922

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 38
ccs 21
cts 25
cp 0.84
rs 6.6166
c 0
b 0
f 0
cc 13
nc 33
nop 0
crap 13.6922

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