Completed
Push — master ( 797c9e...b671da )
by Matteo
02:15
created

ExecuteQueries   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 17.31 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 0
dl 18
loc 104
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A affectedRows() 0 6 1
A query() 0 12 2
A selectDb() 0 6 1
A createDb() 9 9 1
A listTables() 9 9 1
A insertId() 0 6 1
A listDbs() 0 4 1
A dbName() 0 4 1
A dbQuery() 0 7 1
A dropDb() 0 4 1
A setCharset() 0 4 1
A listFields() 0 4 1
A listProcesses() 0 4 1
A tablename() 0 4 1
A unbufferedQuery() 0 4 1

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
namespace Mattbit\MysqlCompat\BridgeComponents;
4
5
use Mattbit\MysqlCompat\Exception\QueryException;
6
7
trait ExecuteQueries
8
{
9
    public function affectedRows(Connection $linkIdentifier = null)
10
    {
11
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
1 ignored issue
show
Bug introduced by
The property manager 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...
12
13
        return $connection->getRowCount();
14
    }
15
16
    public function query($query, Connection $linkIdentifier = null)
17
    {
18
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
19
20
        try {
21
            $result = $connection->query($query);
22
        } catch (QueryException $e) {
23
            return false;
24
        }
25
26
        return $result;
27
    }
28
29
    public function selectDb($databaseName, Connection $linkIdentifier = null)
30
    {
31
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
32
33
        return (bool) $connection->useDatabase($databaseName);
34
    }
35
36 View Code Duplication
    public function createDb($databaseName, Connection $linkIdentifier = null)
37
    {
38
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
39
40
        return $connection->parametrizedQuery(
41
            "CREATE DATABASE :databaseName",
42
            [':databaseName' => $databaseName]
43
        );
44
    }
45
46 View Code Duplication
    public function listTables($database, Connection $linkIdentifier = null)
47
    {
48
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
49
50
        return $connection->parametrizedQuery(
51
            "SHOW TABLES FROM :database",
52
            [':database' => $database]
53
        );
54
    }
55
56
    public function insertId(Connection $linkIdentifier = null)
57
    {
58
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
59
60
        return (int) $connection->getLastInsertId();
61
    }
62
63
    public function listDbs()
64
    {
65
        // @todo
66
    }
67
68
    public function dbName()
69
    {
70
        // @todo
71
    }
72
73
    public function dbQuery($database, $query, Connection $linkIdentifier = null)
74
    {
75
        $connection = $this->manager->getOpenConnectionOrFail($linkIdentifier);
76
        $connection->useDatabase($database);
77
78
        return $connection->query($query);
79
    }
80
81
    public function dropDb()
82
    {
83
        // @todo
84
    }
85
86
    public function setCharset()
87
    {
88
        // @todo
89
    }
90
91
    public function listFields()
92
    {
93
        // @todo
94
    }
95
96
    public function listProcesses()
97
    {
98
        // @todo
99
    }
100
101
    public function tablename()
102
    {
103
        // @todo
104
    }
105
106
    public function unbufferedQuery()
107
    {
108
        // @todo
109
    }
110
}
111