Passed
Push — master ( 331e8c...9d3685 )
by y
01:19
created

Statement::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\DB;
4
5
use ArgumentCountError;
6
use Helix\DB;
7
use PDOStatement;
8
9
/**
10
 * Extends `PDOStatement` for fluency and logging.
11
 */
12
class Statement extends PDOStatement {
13
14
    /**
15
     * @var DB
16
     */
17
    protected $db;
18
19
    /**
20
     * PDO requires this to be protected.
21
     *
22
     * @param DB $db
23
     */
24
    protected function __construct (DB $db) {
25
        $this->db = $db;
26
    }
27
28
    /**
29
     * The query string.
30
     *
31
     * @return string
32
     */
33
    public function __toString () {
34
        return $this->queryString;
35
    }
36
37
    /**
38
     * Logs and returns for fluency.
39
     *
40
     * PHP returns `false` instead of throwing if too many arguments were given.
41
     * This checks for that and throws.
42
     *
43
     * @param array $args
44
     * @return $this
45
     * @throws ArgumentCountError
46
     */
47
    public function execute ($args = null) {
48
        $this->db->getLogger()->__invoke($this->queryString);
49
        if (!parent::execute($args)) {
50
            $info = $this->errorInfo();
51
            if ($info[0] == 0) {
52
                $argc = count($args);
53
                throw new ArgumentCountError("Too many arguments given ({$argc})");
54
            }
55
        }
56
        return $this;
57
    }
58
59
    /**
60
     * `lastInsertId()`
61
     *
62
     * @return int
63
     */
64
    public function getId (): int {
65
        return $this->db->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->lastInsertId() returns the type string which is incompatible with the type-hinted return integer.
Loading history...
66
    }
67
}
68