Completed
Push — master ( 558b0d...64816c )
by Sébastien
04:12
created

ZendDb2Adapter::query()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.1669

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 25
ccs 14
cts 19
cp 0.7368
rs 5.3846
cc 8
eloc 17
nc 11
nop 2
crap 9.1669
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter\Zend;
4
5
use Soluble\DbWrapper\Exception;
6
use Soluble\DbWrapper\Result\Resultset;
7
use Soluble\DbWrapper\Adapter\AdapterInterface;
8
use Soluble\DbWrapper\Connection\Zend\ZendDb2Connection;
9
10
class ZendDb2Adapter implements AdapterInterface
11
{
12
13
    /**
14
     *
15
     * @var \Zend\Db\Adapter\Adapter
16
     */
17
    protected $zendAdapter;
18
19
    /**
20
     *
21
     * @var ZendDb2Connection
22
     */
23
    protected $connection;
24
25
26
    /**
27
     * Constructor
28
     *
29
     * @param \Zend\Db\Adapter\Adapter $zendAdapter
30
     */
31 6
    public function __construct(\Zend\Db\Adapter\Adapter $zendAdapter)
32
    {
33 6
        $this->zendAdapter = $zendAdapter;
34 6
        $this->connection = new ZendDb2Connection($this, $zendAdapter);
35 6
    }
36
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function quoteValue($value)
42
    {
43 1
        return $this->zendAdapter->getPlatform()->quoteValue($value);
44
    }
45
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 3
    public function query($query, $resultsetType = Resultset::TYPE_ARRAY)
51
    {
52
        try {
53
            //$r = $this->zendAdapter->query($query, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
54 3
            $r = $this->zendAdapter->query($query)->execute();
0 ignored issues
show
Bug introduced by
The method execute does only exist in Zend\Db\Adapter\Driver\StatementInterface, but not in Zend\Db\Adapter\Driver\R...tSet\ResultSetInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55 3
            $results = new Resultset($resultsetType);
56 3
            if ($r === false) {
57
                throw new Exception\InvalidArgumentException(
58
                    'Query could not be executed ([$query])'
59
                );
60 3
            } elseif ($r instanceof \Zend\Db\ResultSet\ResultSet) {
61
                foreach ($r as $row) {
62
                    $results->append((array) $row);
63
                }
64 3
            } elseif ($r instanceof \Zend\Db\Adapter\Driver\ResultInterface && $r->getFieldCount() > 0) {
65 2
                foreach ($r as $row) {
66 2
                    $results->append($row);
67 2
                }
68 2
            }
69 3
        } catch (\Exception $e) {
70 2
            $msg = "ZendDb2 adapter query error: {$e->getMessage()} [$query]";
71 2
            throw new Exception\InvalidArgumentException($msg);
72
        }
73 3
        return $results;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     * @return ZendDb2Connection
79
     */
80 1
    public function getConnection()
81
    {
82 1
        return $this->connection;
83
    }
84
}
85