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