Completed
Push — master ( d6fc27...dc9f7f )
by Hung
19s
created

MultiResultSetAdapter::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Foolz\SphinxQL\Drivers\Mysqli;
4
5
class MultiResultSetAdapter implements \Foolz\SphinxQL\Drivers\MultiResultSetAdapterInterface
6
{
7
    /**
8
     * @var bool
9
     */
10
    protected $valid = true;
11
12
    /**
13
     * @var Connection|null
14
     */
15
    protected $connection = null;
16
17
    public function __construct(Connection $connection)
18
    {
19
        $this->connection = $connection;
20
    }
21
22
    /**
23
     * @throws \Foolz\SphinxQL\Exception\ConnectionException
24
     */
25
    public function getNext()
26
    {
27
        if (
28
            !$this->valid() ||
29
            !$this->connection->getConnection()->more_results()
30
        ) {
31
            $this->valid = false;
32
        } else {
33
            $this->connection->getConnection()->next_result();
34
        }
35
    }
36
37
    /**
38
     * @return ResultSet
39
     * @throws \Foolz\SphinxQL\Exception\ConnectionException
40
     */
41
    public function current()
42
    {
43
        return ResultSet::make($this->connection, $this->connection->getConnection()->store_result());
0 ignored issues
show
Bug introduced by
It seems like $this->connection can be null; however, make() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
44
    }
45
46
    /**
47
     * @return bool
48
     * @throws \Foolz\SphinxQL\Exception\ConnectionException
49
     */
50
    public function valid()
51
    {
52
        return $this->connection->getConnection()->errno == 0 && $this->valid;
53
    }
54
}
55