Database::query()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.864

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 2
cts 5
cp 0.4
crap 2.864
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Model;
11
12
use Exception;
13
use Medoo\Medoo;
14
use PDO;
15
16
/**
17
 * Class Database.
18
 */
19
class Database extends Medoo
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $config = [];
25
26
    /**
27
     * @var PDO
28
     */
29
    public $pdo;
30
31
    /**
32
     * Database constructor.
33
     *
34
     * @param array $options
35
     */
36 34
    public function __construct(array $options = null)
37
    {
38 34
        $this->config = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options can be null. However, the property $config is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
39
40 34
        parent::__construct($this->config);
41
42 34
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43 34
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
44 34
        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
45 34
        $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
46 34
    }
47
48
    /**
49
     * reconnect database.
50
     */
51
    public function reconnect()
52
    {
53
        $this->__construct($this->config);
54
    }
55
56
    /**
57
     * @param $query
58
     *
59
     * @return bool|\PDOStatement
60
     */
61 2
    public function query($query)
62
    {
63
        try {
64 2
            return parent::query($query);
65
        } catch (Exception $e) {
66
            $this->reconnect();
67
68
            return parent::query($query);
69
        }
70
    }
71
72
    /**
73
     * @param $query
74
     *
75
     * @return bool|int
76
     *
77
     * @throws \ErrorException
78
     */
79 1
    public function exec($query)
80
    {
81
        try {
82 1
            return parent::exec($query);
83
        } catch (Exception $e) {
84
            $this->reconnect();
85
86
            return parent::exec($query);
87
        }
88
    }
89
90
    /**
91
     * @param $table
92
     * @param $join
93
     * @param null $where
94
     *
95
     * @return bool
96
     */
97 1
    public function has($table, $join, $where = null)
98
    {
99 1
        $column = null;
100
101 1
        $query = $this->query('SELECT EXISTS('.$this->selectContext($table, $join, $column, $where, 1).')');
102
103 1
        if ($query && 1 === intval($query->fetchColumn())) {
104 1
            return true;
105
        }
106
107
        return false;
108
    }
109
}
110