Completed
Push — master ( b242bc...563f05 )
by Sébastien
03:29
created

ZendDb2Adapter::query()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.3229

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 16
cp 0.8125
rs 7.551
c 0
b 0
f 0
cc 7
eloc 14
nc 10
nop 2
crap 7.3229
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
     * @var \Zend\Db\Adapter\Adapter
14
     */
15
    protected $zendAdapter;
16
17
    /**
18
     * @var ZendDb2Connection
19
     */
20
    protected $connection;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param \Zend\Db\Adapter\Adapter $zendAdapter
26
     */
27 6
    public function __construct(\Zend\Db\Adapter\Adapter $zendAdapter)
28
    {
29 6
        $this->zendAdapter = $zendAdapter;
30 6
        $this->connection = new ZendDb2Connection($this, $zendAdapter);
31 6
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function quoteValue($value)
37
    {
38 1
        return $this->zendAdapter->getPlatform()->quoteValue($value);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function query($query, $resultsetType = Resultset::TYPE_ARRAY)
45
    {
46
        try {
47 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...
48 3
            $results = new Resultset($resultsetType);
49 3
            if ($r instanceof \Zend\Db\ResultSet\ResultSet) {
50
                foreach ($r as $row) {
51
                    $results->append((array) $row);
52
                }
53 3
            } elseif ($r instanceof \Zend\Db\Adapter\Driver\ResultInterface && $r->getFieldCount() > 0) {
54 2
                foreach ($r as $row) {
55 2
                    $results->append($row);
56 2
                }
57 2
            }
58 3
        } catch (\Exception $e) {
59 2
            $msg = "ZendDb2 adapter query error: {$e->getMessage()} [$query]";
60 2
            throw new Exception\InvalidArgumentException($msg);
61
        }
62
63 3
        return $results;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @return ZendDb2Connection
70
     */
71 1
    public function getConnection()
72
    {
73 1
        return $this->connection;
74
    }
75
}
76