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

Database   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 36.36 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 34.78%

Importance

Changes 0
Metric Value
dl 24
loc 66
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 12 3
A exec() 12 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
     * 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