Passed
Push — main ( 667f12...2d9659 )
by Thierry
04:03 queued 02:06
created

Statement::rowCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db\MySqli;
4
5
use Lagdo\DbAdmin\Driver\Db\StatementInterface;
6
use Lagdo\DbAdmin\Driver\Entity\StatementFieldEntity;
7
8
use mysqli_result;
9
10
class Statement implements StatementInterface
11
{
12
    /**
13
     * The query result
14
     *
15
     * @var mysqli_result
16
     */
17
    protected $result = null;
18
19
    /**
20
     * The constructor
21
     *
22
     * @param mysqli_result|bool $result
23
     */
24
    public function __construct($result)
25
    {
26
        if (is_a($result, mysqli_result::class)) {
27
            $this->result = $result;
28
        }
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function rowCount()
35
    {
36
        return $this->result ? $this->result->num_rows : 0;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function fetchAssoc()
43
    {
44
        return ($this->result) ? $this->result->fetch_assoc() : null;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function fetchRow()
51
    {
52
        return ($this->result) ? $this->result->fetch_row() : null;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function fetchField()
59
    {
60
        if (!$this->result || !($field = $this->result->fetch_field())) {
61
            return null;
62
        }
63
        return new StatementFieldEntity($field->type, $field->type === 63, // 63 - binary
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Lagdo\DbAdmin...able, $field->orgtable) returns the type Lagdo\DbAdmin\Driver\Entity\StatementFieldEntity which is incompatible with the return type mandated by Lagdo\DbAdmin\Driver\Db\...Interface::fetchField() of Lagdo\DbAdmin\Driver\Db\StatementFieldEntity.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
64
            $field->name, $field->orgname, $field->table, $field->orgtable);
65
    }
66
67
    /**
68
     * The destructor
69
     */
70
    public function __destruct()
71
    {
72
        if (($this->result)) {
73
            $this->result->free();
74
        }
75
    }
76
}
77