Test Failed
Push — master ( 6d1358...8db816 )
by huang
02:59
created

Database::reconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
14
/**
15
 * Class Database.
16
 */
17
class Database extends Medoo
18
{
19
    /**
20
     * @var array|null
21
     */
22
    protected $config = [];
23
24
    /**
25
     * Database constructor.
26
     *
27
     * @param null $options
28
     */
29
    public function __construct($options = null)
30
    {
31
        $this->config = $options;
32
33
        parent::__construct($this->config);
34
    }
35
36
    /**
37
     * reconnect database.
38
     */
39
    public function reconnect()
40
    {
41
        $this->__construct($this->config);
0 ignored issues
show
Bug introduced by
It seems like $this->config can also be of type array; however, FastD\Model\Database::__construct() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
    }
43
44
    /**
45
     * check database gone away.
46
     */
47
    public function checkGoneAway()
48
    {
49
        list(, $code) = $this->pdo->errorInfo();
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...
50
        if (2006 === $code || 2013 === $code) {
51
            $this->reconnect();
52
        }
53
    }
54
55
    /**
56
     * @param $query
57
     *
58
     * @return bool|\PDOStatement
59
     */
60 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...
61
    {
62
        $result = parent::query($query);
63
        if (false === $result) {
64
            $this->checkGoneAway();
65
            $result = parent::query($query);
66
        }
67
68
        return $result;
69
    }
70
71
    /**
72
     * @param $query
73
     *
74
     * @return bool|int
75
     */
76 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...
77
    {
78
        $result = parent::exec($query);
79
        if (false === $result) {
80
            $this->checkGoneAway();
81
            $result = parent::exec($query);
82
        }
83
84
        return $result;
85
    }
86
}
87