Adodb   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 6
c 6
b 3
f 0
lcom 0
cbo 4
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapter() 0 19 4
A getADOConnectionId() 0 8 2
1
<?php
2
/**
3
 *  Soluble Components (http://belgattitude.github.io/solublecomponents)
4
 *
5
 *  @link      http://github.com/belgattitude/solublecomponents for the canonical source repository
6
 *  @copyright Copyright (c) 2013-2014 Sébastien Vanvelthem
7
 *  @license   https://github.com/belgattitude/solublecomponents/blob/master/LICENSE.txt MIT License
8
 */
9
namespace Soluble\Db\Compat;
10
11
use Zend\Db\Adapter\Adapter;
12
13
class Adodb
14
{
15
    /**
16
     * Return a Zend\Db\Adapter\Adapter connection from an existing ADODb connection
17
     *
18
     * @throws Exception\UnsupportedDriverException
19
     * @throws Exception\AdoNotConnectedException when connection is not initialized
20
     *
21
     * @param \ADOConnection $adoConnection
22
     * @return Adapter
23
     */
24
    public static function getAdapter(\ADOConnection $adoConnection)
25
    {
26
        $adoConnectionDriver = strtolower(get_class($adoConnection));
27
        switch ($adoConnectionDriver) {
28
            //case 'adodb_mysqlt':
29
            case 'adodb_mysqli':
30
                $connectionId = self::getADOConnectionId($adoConnection);
31
                $adapter = Mysqli::getAdapter($connectionId);
0 ignored issues
show
Bug introduced by
It seems like $connectionId defined by self::getADOConnectionId($adoConnection) on line 30 can also be of type object<PDO>; however, Soluble\Db\Compat\Mysqli::getAdapter() does only seem to accept object<mysqli>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
                break;
33
            case 'adodb_pdo':
34
            case 'adodb_pdo_mysql':
35
                $connectionId = self::getADOConnectionId($adoConnection);
36
                $adapter = PDO::getAdapter($connectionId);
0 ignored issues
show
Bug introduced by
It seems like $connectionId defined by self::getADOConnectionId($adoConnection) on line 35 can also be of type object<mysqli>; however, Soluble\Db\Compat\PDO::getAdapter() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
                break;
38
            default:
39
                throw new Exception\UnsupportedDriverException(__METHOD__ . ". Driver '$adoConnectionDriver' not supported");
40
        }
41
        return $adapter;
42
    }
43
44
    /**
45
     * Return internal adodb internal connection id
46
     * @throws Exception\AdoNotConnectedException when connection is not initialized
47
     * @param \ADOConnection $adoConnection
48
     * @return \MySQLI|\PDO
49
     */
50
    protected static function getADOConnectionId(\ADOConnection $adoConnection)
51
    {
52
        $connectionId = $adoConnection->_connectionID;
53
        if (!$connectionId) {
54
            throw new Exception\AdoNotConnectedException(__METHOD__ . ". Error: Invalid usage, adodb connection must be connected before use (see connect(), pconnect()");
55
        }
56
        return $connectionId;
57
    }
58
}
59