| Conditions | 22 | 
| Paths | 4188 | 
| Total Lines | 137 | 
| Code Lines | 88 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | 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 | ||
| 11 | function backup_tables($DBH, $tables) | ||
| 12 | { | ||
| 13 | $DBH->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); | ||
| 14 | |||
| 15 | //Script Variables | ||
| 16 | $compression = false; | ||
| 17 | $BACKUP_PATH = ''; | ||
| 18 | $nowtimename = time(); | ||
| 19 | |||
| 20 | //create/open files | ||
| 21 |   if ($compression) { | ||
|  | |||
| 22 | $zp = gzopen($BACKUP_PATH . $nowtimename . '.sql.gz', 'a9'); | ||
| 23 |   } else { | ||
| 24 | $handle = fopen($BACKUP_PATH . $nowtimename . '.sql', 'a+'); | ||
| 25 | } | ||
| 26 | |||
| 27 | //array of all database field types which just take numbers | ||
| 28 | $numtypes = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'real']; | ||
| 29 | |||
| 30 | //get all of the tables | ||
| 31 |   if (empty($tables)) { | ||
| 32 |     $pstm1 = $DBH->query('SHOW TABLES'); | ||
| 33 |     while ($row = $pstm1->fetch(PDO::FETCH_NUM)) { | ||
| 34 | $tables[] = $row[0]; | ||
| 35 | } | ||
| 36 |   } else { | ||
| 37 |     $tables = is_array($tables) ? $tables : explode(',', $tables); | ||
| 38 | } | ||
| 39 | |||
| 40 | //cycle through the table(s) | ||
| 41 | |||
| 42 |   foreach ($tables as $table) { | ||
| 43 |     $result = $DBH->query("SELECT * FROM $table"); | ||
| 44 | $num_fields = $result->columnCount(); | ||
| 45 | $num_rows = $result->rowCount(); | ||
| 46 | |||
| 47 | $return = ''; | ||
| 48 | //uncomment below if you want 'DROP TABLE IF EXISTS' displayed | ||
| 49 | //$return.= 'DROP TABLE IF EXISTS `'.$table.'`;'; | ||
| 50 | |||
| 51 | //table structure | ||
| 52 |     $pstm2 = $DBH->query("SHOW CREATE TABLE $table"); | ||
| 53 | $row2 = $pstm2->fetch(PDO::FETCH_NUM); | ||
| 54 |     $ifnotexists = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $row2[1]); | ||
| 55 | $return .= "\n\n" . $ifnotexists . ";\n\n"; | ||
| 56 | |||
| 57 |     if ($compression) { | ||
| 58 | gzwrite($zp, $return); | ||
| 59 |     } else { | ||
| 60 | fwrite($handle, $return); | ||
| 61 | } | ||
| 62 | $return = ''; | ||
| 63 | |||
| 64 | //insert values | ||
| 65 |     if ($num_rows) { | ||
| 66 |       $return = 'INSERT INTO `' . "$table" . '` ('; | ||
| 67 |       $pstm3 = $DBH->query("SHOW COLUMNS FROM $table"); | ||
| 68 | $count = 0; | ||
| 69 | $type = []; | ||
| 70 | |||
| 71 |       while ($rows = $pstm3->fetch(PDO::FETCH_NUM)) { | ||
| 72 |         if (stripos($rows[1], '(')) { | ||
| 73 |           $type[$table][] = stristr($rows[1], '(', true); | ||
| 74 |         } else { | ||
| 75 | $type[$table][] = $rows[1]; | ||
| 76 | } | ||
| 77 | |||
| 78 | $return .= '`' . $rows[0] . '`'; | ||
| 79 | ++$count; | ||
| 80 |         if ($count < ($pstm3->rowCount())) { | ||
| 81 | $return .= ', '; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | $return .= ')' . ' VALUES'; | ||
| 86 | |||
| 87 |       if ($compression) { | ||
| 88 | gzwrite($zp, $return); | ||
| 89 |       } else { | ||
| 90 | fwrite($handle, $return); | ||
| 91 | } | ||
| 92 | $return = ''; | ||
| 93 | } | ||
| 94 | $count = 0; | ||
| 95 |     while ($row = $result->fetch(PDO::FETCH_NUM)) { | ||
| 96 |       $return = "\n\t("; | ||
| 97 | |||
| 98 |       for ($j = 0; $j < $num_fields; ++$j) { | ||
| 99 |         //$row[$j] = preg_replace("\n","\\n",$row[$j]); | ||
| 100 | |||
| 101 |         if (isset($row[$j])) { | ||
| 102 | //if number, take away "". else leave as string | ||
| 103 |           if ((in_array($type[$table][$j], $numtypes)) && (!empty($row[$j]))) { | ||
| 104 | $return .= $row[$j]; | ||
| 105 |           } else { | ||
| 106 | $return .= $DBH->quote($row[$j]); | ||
| 107 | } | ||
| 108 |         } else { | ||
| 109 | $return .= 'NULL'; | ||
| 110 | } | ||
| 111 |         if ($j < ($num_fields - 1)) { | ||
| 112 | $return .= ','; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | ++$count; | ||
| 116 |       if ($count < ($result->rowCount())) { | ||
| 117 | $return .= '),'; | ||
| 118 |       } else { | ||
| 119 | $return .= ');'; | ||
| 120 | } | ||
| 121 |       if ($compression) { | ||
| 122 | gzwrite($zp, $return); | ||
| 123 |       } else { | ||
| 124 | fwrite($handle, $return); | ||
| 125 | } | ||
| 126 | $return = ''; | ||
| 127 | } | ||
| 128 | $return = "\n\n-- ------------------------------------------------ \n\n"; | ||
| 129 |     if ($compression) { | ||
| 130 | gzwrite($zp, $return); | ||
| 131 |     } else { | ||
| 132 | fwrite($handle, $return); | ||
| 133 | } | ||
| 134 | $return = ''; | ||
| 135 | } | ||
| 136 | |||
| 137 | $error1 = $pstm2->errorInfo(); | ||
| 138 | $error2 = $pstm3->errorInfo(); | ||
| 139 | $error3 = $result->errorInfo(); | ||
| 140 | echo $error1[2]; | ||
| 141 | echo $error2[2]; | ||
| 142 | echo $error3[2]; | ||
| 143 | |||
| 144 |   if ($compression) { | ||
| 145 | gzclose($zp); | ||
| 146 |   } else { | ||
| 147 | fclose($handle); | ||
| 148 | } | ||
| 150 |