Passed
Push — master ( 22f215...8d68fd )
by huang
03:36
created

Database::checkGoneAway()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 12
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      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Model;
11
12
use Medoo\Medoo;
13
use PDO;
14
use PDOException;
15
16
/**
17
 * Class Database.
18
 */
19
class Database extends Medoo
20
{
21
    /**
22
     * @var array|null
23
     */
24
    protected $config = [];
25
26
    /**
27
     * Database constructor.
28
     *
29
     * @param array $options
30
     */
31 37
    public function __construct(array $options = null)
32
    {
33 37
        $this->config = $options;
34
35 37
        parent::__construct($this->config);
36
37 37
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
0 ignored issues
show
Bug introduced by
The property pdo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38 37
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
39 37
    }
40
41
    /**
42
     * reconnect database.
43
     */
44
    public function reconnect()
45
    {
46
        $this->__construct($this->config);
47
    }
48
49
    /**
50
     * @param $query
51
     *
52
     * @return bool|\PDOStatement
53
     */
54 2 View Code Duplication
    public function query($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        try {
57 2
            return parent::query($query);
58
        } catch (PDOException $e) {
59
            if('HY000' !== $e->getCode()) {
60
                throw $e;
61
            }
62
            $this->reconnect();
63
            return parent::query($query);
64
        }
65
    }
66
67
    /**
68
     * @param $query
69
     *
70
     * @return bool|int
71
     */
72 View Code Duplication
    public function exec($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        try {
75
            return parent::exec($query);
76
        } catch (PDOException $e) {
77
            if('HY000' !== $e->getCode()) {
78
                throw $e;
79
            }
80
            $this->reconnect();
81
            return parent::exec($query);
82
        }
83
    }
84
}
85