FetchModeTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 40
ccs 8
cts 10
cp 0.8
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFetchArgs() 0 2 1
A setFetchMode() 0 8 2
A getFetchMode() 0 2 1
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Db\Utils;
9
10
use PDO;
11
12
trait FetchModeTrait {
13
    /**
14
     * @var array The fetch arguments (if any).
15
     */
16
    private $fetchArgs = [PDO::FETCH_ASSOC];
17
18
    /**
19
     * Get the default fetch mode.
20
     *
21
     * @return int Returns the current fetch mode.
22
     */
23 17
    public function getFetchMode() {
24 17
        return reset($this->fetchArgs);
25
    }
26
27
    /**
28
     * Get the fetch arguments.
29
     *
30
     * @return array Returns an array of arguments including the fetch mode.
31
     */
32 107
    public function getFetchArgs() {
33 107
        return $this->fetchArgs;
34
    }
35
36
    /**
37
     * Set the default fetch mode.
38
     *
39
     * @param int|string $mode One of the **PDO::FETCH_*** constants or a class name.
40
     * @param mixed ...$args Additional arguments for {@link \PDOStatement::fetchAll()}.
41
     * @return $this
42
     * @see http://php.net/manual/en/pdostatement.fetchall.php
43
     */
44 17
    public function setFetchMode($mode, ...$args) {
45 17
        if (is_string($mode)) {
46
            array_unshift($args, PDO::$mode);
0 ignored issues
show
Bug introduced by
The property mode does not seem to exist on PDO.
Loading history...
47
            $mode = PDO::FETCH_CLASS;
48
        }
49 17
        $this->fetchArgs = array_merge([$mode], $args);
50
51 17
        return $this;
52
    }
53
}
54