Conditions | 7 |
Paths | 12 |
Total Lines | 64 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function backup_data($excluded=array()) |
||
35 | { |
||
36 | // Get a list of all the objects |
||
37 | $sql = 'SELECT DISTINCT "name" |
||
38 | FROM "sqlite_master" |
||
39 | WHERE "type"=\'table\''; |
||
40 | |||
41 | if( ! empty($excluded)) |
||
42 | { |
||
43 | $sql .= " AND \"name\" NOT IN('".implode("','", $excluded)."')"; |
||
44 | } |
||
45 | |||
46 | $res = $this->get_driver()->query($sql); |
||
47 | $result = $res->fetchAll(\PDO::FETCH_ASSOC); |
||
48 | |||
49 | unset($res); |
||
50 | |||
51 | $output_sql = ''; |
||
52 | |||
53 | // Get the data for each object |
||
54 | foreach($result as $r) |
||
55 | { |
||
56 | $sql = 'SELECT * FROM "'.$r['name'].'"'; |
||
57 | $res = $this->get_driver()->query($sql); |
||
58 | $obj_res = $res->fetchAll(\PDO::FETCH_ASSOC); |
||
59 | |||
60 | unset($res); |
||
61 | |||
62 | // If the row is empty, continue; |
||
63 | if (empty($obj_res)) |
||
64 | { |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | // Nab the column names by getting the keys of the first row |
||
69 | $columns = array_keys(current($obj_res)); |
||
70 | |||
71 | $insert_rows = array(); |
||
72 | |||
73 | // Create the insert statements |
||
74 | foreach($obj_res as $row) |
||
75 | { |
||
76 | $row = array_values($row); |
||
77 | |||
78 | // Quote values as needed by type |
||
79 | for($i=0, $icount=count($row); $i<$icount; $i++) |
||
80 | { |
||
81 | $row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->get_driver()->quote($row[$i]); |
||
82 | } |
||
83 | |||
84 | $row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
||
85 | |||
86 | unset($row); |
||
87 | |||
88 | $insert_rows[] = $row_string; |
||
89 | } |
||
90 | |||
91 | unset($obj_res); |
||
92 | |||
93 | $output_sql .= "\n\n".implode("\n", $insert_rows); |
||
94 | } |
||
95 | |||
96 | return $output_sql; |
||
97 | } |
||
98 | |||
123 | // End of sqlite_util.php |