Passed
Branch 3.1 (41cce1)
by huang
02:56
created

Database   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 32.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 34.78%

Importance

Changes 0
Metric Value
dl 24
loc 73
ccs 8
cts 23
cp 0.3478
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A reconnect() 0 4 1
A query() 12 13 3
A exec() 12 13 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 37
    public function __construct(array $options = null)
37
    {
38 37
        $this->config = $options;
39
40 37
        parent::__construct($this->config);
41
42 37
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43 37
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
44 37
    }
45
46
    /**
47
     * reconnect database.
48
     */
49
    public function reconnect()
50
    {
51
        $this->__construct($this->config);
52
    }
53
54
    /**
55
     * @param $query
56
     *
57
     * @return bool|\PDOStatement
58
     */
59 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...
60
    {
61
        try {
62 2
            return parent::query($query);
63
        } catch (PDOException $e) {
64
            if ('HY000' !== $e->getCode()) {
65
                throw $e;
66
            }
67
            $this->reconnect();
68
69
            return parent::query($query);
70
        }
71
    }
72
73
    /**
74
     * @param $query
75
     *
76
     * @return bool|int
77
     */
78 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...
79
    {
80
        try {
81
            return parent::exec($query);
82
        } catch (PDOException $e) {
83
            if ('HY000' !== $e->getCode()) {
84
                throw $e;
85
            }
86
            $this->reconnect();
87
88
            return parent::exec($query);
89
        }
90
    }
91
}
92