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

SQLDataSet::read()   C

Complexity

Conditions 11
Paths 72

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 11
eloc 24
nc 72
nop 6
dl 0
loc 44
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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