Passed
Push — master ( a7f2b9...90017e )
by huang
03:13
created

Database   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 54.84%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 24
loc 88
ccs 17
cts 31
cp 0.5484
rs 10
wmc 11
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A reconnect() 0 4 1
A query() 12 13 3
A exec() 12 13 3
A has() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @var PDO
28
     */
29
    public $pdo;
30
31
    /**
32
     * Database constructor.
33
     *
34
     * @param array $options
35
     */
36 44
    public function __construct(array $options = null)
37
    {
38 44
        $this->config = $options;
39
40 44
        parent::__construct($this->config);
41
42 44
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43 44
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
44 44
        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
45 44
        $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
46 44
    }
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 3 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...
62
    {
63
        try {
64 3
            return parent::query($query);
65
        } catch (PDOException $e) {
66
            if ('HY000' !== $e->getCode()) {
67
                throw $e;
68
            }
69
            $this->reconnect();
70
71
            return parent::query($query);
72
        }
73
    }
74
75
    /**
76
     * @param $query
77
     *
78
     * @return bool|\PDOStatement
79
     */
80 1 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...
81
    {
82
        try {
83 1
            return parent::exec($query);
84
        } catch (PDOException $e) {
85
            if ('HY000' !== $e->getCode()) {
86
                throw $e;
87
            }
88
            $this->reconnect();
89
90
            return parent::exec($query);
91
        }
92
    }
93
94 1
    public function has($table, $join, $where = null)
95
    {
96 1
        $column = null;
97
98 1
        $query = $this->query('SELECT EXISTS('.$this->selectContext($table, $join, $column, $where, 1).')');
99
100 1
        if ($query && intval($query->fetchColumn()) === 1) {
101 1
            return true;
102
        }
103
104
        return false;
105
    }
106
}
107