Completed
Push — master ( 1caf4d...448bfe )
by Patrick
03:00
created

SQLDataSet   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 216
Duplicated Lines 41.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 90
loc 216
rs 8.3673
c 1
b 0
f 1
wmc 45
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A _get_row_count_for_query() 0 15 3
A _tableExistsNoPrefix() 12 12 3
A _tableExists() 12 12 3
A _viewExists() 12 12 3
A tableExists() 0 16 4
A getTable() 0 16 4
C read() 0 44 11
A update() 21 21 4
B create() 22 22 4
A raw_query() 0 10 2
A delete() 0 9 2
A __construct() 11 11 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like SQLDataSet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SQLDataSet, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Data;
3
4
class SQLDataSet extends DataSet
5
{
6
    protected $pdo;
7
8 View Code Duplication
    function __construct($params)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
9
    {
10
        if(isset($params['user']))
11
        {
12
            $this->pdo = new \PDO($params['dsn'], $params['user'], $params['pass']);
13
        }
14
        else
15
        {
16
            $this->pdo = new \PDO($params['dsn']);
17
        }
18
    }
19
20
    function _get_row_count_for_query($sql)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
    {
22
        $stmt = $this->pdo->query($sql);
23
        if($stmt === false)
24
        {
25
            return 0;
26
        }
27
        $count = $stmt->rowCount();
28
        if($count === 0)
29
        {
30
            $array = $stmt->fetchAll();
31
            $count = count($array);
32
        }
33
        return $count;
34
    }
35
36 View Code Duplication
    function _tableExistsNoPrefix($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
37
    {
38
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote($name)) > 0)
39
        {
40
            return true;
41
        }
42
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote($name)) > 0)
43
        {
44
            return true;
45
        }
46
        return false;
47
    }
48
49 View Code Duplication
    function _tableExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
50
    {
51
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('tbl'.$name)) > 0)
52
        {
53
            return true;
54
        }
55
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('tbl'.$name)) > 0)
56
        {
57
            return true;
58
        }
59
        return false;
60
    }
61
62 View Code Duplication
    function _viewExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
63
    {
64
        if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('v'.$name)) > 0)
65
        {
66
            return true;
67
        }
68
        else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('v'.$name)) > 0)
69
        {
70
            return true;
71
        }
72
        return false;
73
    }
74
75
    function tableExists($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
    {
77
        if($this->_tableExists($name))
78
        {
79
            return true;
80
        }
81
        if($this->_tableExistsNoPrefix($name))
82
        {
83
            return true;
84
        }
85
        if($this->_viewExists($name))
86
        {
87
            return true;
88
        }
89
        return false;
90
    }
91
92
    function getTable($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
    {
94
        if($this->_tableExists($name))
95
        {
96
            return new SQLDataTable($this, 'tbl'.$name);
97
        }
98
        if($this->_viewExists($name))
99
        {
100
            return new SQLDataTable($this, 'v'.$name);
101
        }
102
        if($this->_tableExistsNoPrefix($name))
103
        {
104
            return new SQLDataTable($this, $name);
105
        }
106
        throw new \Exception('No such table '.$name);
107
    }
108
109
    function read($tablename, $where=false, $select='*', $count=false, $skip=false, $sort=false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
    {
111
        if($select === false)
112
        {
113
            $select = '*';
114
        }
115
        $sql = "SELECT $select FROM $tablename";
116
        if($where !== false)
117
        {
118
            $sql.=' WHERE '.$where;
119
        }
120
        if($count !== false)
121
        {
122
            if($skip === false)
123
            {
124
                $sql.=' LIMIT '.(int)$count;
125
            }
126
            else
127
            {
128
                $sql.=" LIMIT $skip, $count";
129
            }
130
        }
131
        if($sort !== false)
132
        {
133
            $sql.=' ORDER BY ';
134
            $tmp = array();
135
            foreach($sort as $sort_col=>$dir)
0 ignored issues
show
Bug introduced by
The expression $sort of type boolean is not traversable.
Loading history...
136
            {
137
                array_push($tmp, $sort_col.' '.($dir === 1?'ASC':'DESC'));
138
            }
139
            $sql.=implode($tmp,',');
140
        }
141
        $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC);
142
        if($stmt === false)
143
        {
144
            return false;
145
        }
146
        $ret = $stmt->fetchAll();
147
        if($ret === false || empty($ret))
148
        {
149
            return false;
150
        }
151
        return $ret;
152
    }
153
154 View Code Duplication
    function update($tablename, $where, $data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
155
    {
156
        $set = array();
157
        if(is_object($data))
158
        {
159
            $data = (array)$data;
160
        }
161
        $cols = array_keys($data);
162
        $count = count($cols);
163
        for($i = 0; $i < $count; $i++)
164
        {
165
            array_push($set, $cols[$i].'='.$this->pdo->quote($data[$cols[$i]]));
166
        }
167
        $set = implode(',', $set);
168
        $sql = "UPDATE $tablename SET $set WHERE $where";
169
        if($this->pdo->exec($sql) === false)
170
        {
171
            return false;
172
        }
173
        return true;
174
    }
175
176 View Code Duplication
    function create($tablename, $data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
177
    {
178
        $set = array();
179
        if(is_object($data))
180
        {
181
            $data = (array)$data;
182
        }
183
        $cols = array_keys($data);
184
        $count = count($cols);
185
        for($i = 0; $i < $count; $i++)
186
        {
187
            array_push($set, $this->pdo->quote($data[$cols[$i]]));
188
        }
189
        $cols = implode(',', $cols);
190
        $set = implode(',', $set);
191
        $sql = "INSERT INTO $tablename ($cols) VALUES ($set);";
192
        if($this->pdo->exec($sql) === false)
193
        {
194
            return false;
195
        }
196
        return true;
197
    }
198
199
    function delete($tablename, $where)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
200
    {
201
        $sql = "DELETE FROM $tablename WHERE $where";
202
        if($this->pdo->exec($sql) === false)
203
        {
204
            return false;
205
        }
206
        return true;
207
    }
208
209
    function raw_query($sql)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
210
    {
211
        $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC);
212
        if($stmt === false)
213
        {
214
            return false;
215
        }
216
        $ret = $stmt->fetchAll();
217
        return $ret;
218
    }
219
}
220
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
221