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 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 |
||
| 4 | class SQLDataSet extends DataSet |
||
| 5 | { |
||
| 6 | protected $pdo; |
||
| 7 | |||
| 8 | public function __construct($params) |
||
| 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 | /** |
||
| 21 | * Get the number of rows affected by the query |
||
| 22 | * |
||
| 23 | * @param string $sql The SQL string |
||
| 24 | * |
||
| 25 | * @return integer The number of rows affected by the query |
||
| 26 | */ |
||
| 27 | private function _get_row_count_for_query($sql) |
||
| 28 | { |
||
| 29 | $stmt = $this->pdo->query($sql); |
||
| 30 | if($stmt === false) |
||
| 31 | { |
||
| 32 | return 0; |
||
| 33 | } |
||
| 34 | $count = $stmt->rowCount(); |
||
| 35 | if($count === 0) |
||
| 36 | { |
||
| 37 | $array = $stmt->fetchAll(); |
||
| 38 | $count = count($array); |
||
| 39 | } |
||
| 40 | return $count; |
||
| 41 | } |
||
| 42 | |||
| 43 | function _tableExistsNoPrefix($name) |
||
|
|
|||
| 44 | { |
||
| 45 | if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote($name)) > 0) |
||
| 46 | { |
||
| 47 | return true; |
||
| 48 | } |
||
| 49 | else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote($name)) > 0) |
||
| 50 | { |
||
| 51 | return true; |
||
| 52 | } |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | function _tableExists($name) |
||
| 57 | { |
||
| 58 | if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('tbl'.$name)) > 0) |
||
| 59 | { |
||
| 60 | return true; |
||
| 61 | } |
||
| 62 | else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('tbl'.$name)) > 0) |
||
| 63 | { |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | function _viewExists($name) |
||
| 70 | { |
||
| 71 | if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('v'.$name)) > 0) |
||
| 72 | { |
||
| 73 | return true; |
||
| 74 | } |
||
| 75 | else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('v'.$name)) > 0) |
||
| 76 | { |
||
| 77 | return true; |
||
| 78 | } |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | function tableExists($name) |
||
| 83 | { |
||
| 84 | if($this->_tableExists($name)) |
||
| 85 | { |
||
| 86 | return true; |
||
| 87 | } |
||
| 88 | if($this->_tableExistsNoPrefix($name)) |
||
| 89 | { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | if($this->_viewExists($name)) |
||
| 93 | { |
||
| 94 | return true; |
||
| 95 | } |
||
| 96 | return false; |
||
| 97 | } |
||
| 98 | |||
| 99 | function getTable($name) |
||
| 100 | { |
||
| 101 | if($this->_tableExists($name)) |
||
| 102 | { |
||
| 103 | return new SQLDataTable($this, 'tbl'.$name); |
||
| 104 | } |
||
| 105 | if($this->_viewExists($name)) |
||
| 106 | { |
||
| 107 | return new SQLDataTable($this, 'v'.$name); |
||
| 108 | } |
||
| 109 | if($this->_tableExistsNoPrefix($name)) |
||
| 110 | { |
||
| 111 | return new SQLDataTable($this, $name); |
||
| 112 | } |
||
| 113 | throw new \Exception('No such table '.$name); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param array $sort The array to sort by or false to not sort |
||
| 118 | */ |
||
| 119 | private function getOrderByClause($sort) |
||
| 120 | { |
||
| 121 | if(empty($sort)) |
||
| 122 | { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | $sql = ' ORDER BY '; |
||
| 126 | $tmp = array(); |
||
| 127 | foreach($sort as $sort_col=>$dir) |
||
| 128 | { |
||
| 129 | array_push($tmp, $sort_col.' '.($dir === 1 ? 'ASC' : 'DESC')); |
||
| 130 | } |
||
| 131 | $sql .= implode($tmp, ','); |
||
| 132 | return $sql; |
||
| 133 | } |
||
| 134 | |||
| 135 | private function getLimitClause($count, $skip) |
||
| 136 | { |
||
| 137 | if($count === false) |
||
| 138 | { |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | $count = intval($count); |
||
| 142 | if($skip !== false) |
||
| 143 | { |
||
| 144 | $skip = intval($count); |
||
| 145 | return " LIMIT $skip, $count"; |
||
| 146 | } |
||
| 147 | return ' LIMIT '.$count; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Read data from the specified SQL table |
||
| 152 | * |
||
| 153 | * @param string $tablename The name of the table to read from |
||
| 154 | * @param string $where The where caluse of the SQL statement |
||
| 155 | * @param string $select The colums to read |
||
| 156 | * @param string $count The number of rows to read |
||
| 157 | * @param string $skip The number of rows to skip over |
||
| 158 | * @param array $sort The array to sort by or false to not sort |
||
| 159 | * |
||
| 160 | * @return array An array of all the returned records |
||
| 161 | */ |
||
| 162 | public function read($tablename, $where = false, $select = '*', $count = false, $skip = false, $sort = false) |
||
| 163 | { |
||
| 164 | if($select === false) |
||
| 165 | { |
||
| 166 | $select = '*'; |
||
| 167 | } |
||
| 168 | $sql = "SELECT $select FROM $tablename"; |
||
| 169 | if($where !== false) |
||
| 170 | { |
||
| 171 | $sql .= ' WHERE '.$where; |
||
| 172 | } |
||
| 173 | $tmp = $this->getLimitClause($count, $skip) |
||
| 174 | if($tmp !== false) |
||
| 175 | { |
||
| 176 | $sql .= $tmp; |
||
| 177 | } |
||
| 178 | $tmp = $this->getOrderByClause($sort); |
||
| 179 | if($tmp !== false) |
||
| 180 | { |
||
| 181 | $sql .= $tmp; |
||
| 182 | } |
||
| 183 | $stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC); |
||
| 184 | if($stmt === false) |
||
| 185 | { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | $ret = $stmt->fetchAll(); |
||
| 189 | if($ret === false || empty($ret)) |
||
| 190 | { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | return $ret; |
||
| 194 | } |
||
| 195 | |||
| 196 | function update($tablename, $where, $data) |
||
| 197 | { |
||
| 198 | $set = array(); |
||
| 199 | if(is_object($data)) |
||
| 200 | { |
||
| 201 | $data = (array)$data; |
||
| 202 | } |
||
| 203 | $cols = array_keys($data); |
||
| 204 | $count = count($cols); |
||
| 205 | for($i = 0; $i < $count; $i++) |
||
| 206 | { |
||
| 207 | array_push($set, $cols[$i].'='.$this->pdo->quote($data[$cols[$i]])); |
||
| 208 | } |
||
| 209 | $set = implode(',', $set); |
||
| 210 | $sql = "UPDATE $tablename SET $set WHERE $where"; |
||
| 211 | if($this->pdo->exec($sql) === false) |
||
| 212 | { |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | function create($tablename, $data) |
||
| 219 | { |
||
| 220 | $set = array(); |
||
| 221 | if(is_object($data)) |
||
| 222 | { |
||
| 223 | $data = (array)$data; |
||
| 224 | } |
||
| 225 | $cols = array_keys($data); |
||
| 226 | $count = count($cols); |
||
| 227 | for($i = 0; $i < $count; $i++) |
||
| 228 | { |
||
| 229 | array_push($set, $this->pdo->quote($data[$cols[$i]])); |
||
| 230 | } |
||
| 231 | $cols = implode(',', $cols); |
||
| 232 | $set = implode(',', $set); |
||
| 233 | $sql = "INSERT INTO $tablename ($cols) VALUES ($set);"; |
||
| 234 | if($this->pdo->exec($sql) === false) |
||
| 235 | { |
||
| 236 | return false; |
||
| 237 | } |
||
| 238 | return true; |
||
| 239 | } |
||
| 240 | |||
| 241 | function delete($tablename, $where) |
||
| 263 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.