Conditions | 6 |
Paths | 10 |
Total Lines | 54 |
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); |
||
74 | public function backupData($exclude=[]): string |
||
75 | { |
||
76 | $tables = $this->getDriver()->getTables(); |
||
77 | |||
78 | // Filter out the tables you don't want |
||
79 | if( ! empty($exclude)) |
||
80 | { |
||
81 | $tables = array_diff($tables, $exclude); |
||
82 | } |
||
83 | |||
84 | $outputSql = ''; |
||
85 | |||
86 | // Select the rows from each Table |
||
87 | foreach($tables as $t) |
||
88 | { |
||
89 | $sql = "SELECT * FROM `{$t}`"; |
||
90 | $res = $this->getDriver()->query($sql); |
||
91 | $rows = $res->fetchAll(PDO::FETCH_ASSOC); |
||
92 | |||
93 | // Skip empty tables |
||
94 | if (count($rows) < 1) |
||
95 | { |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | // Nab the column names by getting the keys of the first row |
||
100 | $columns = @array_keys($rows[0]); |
||
101 | |||
102 | $insertRows = []; |
||
103 | |||
104 | // Create the insert statements |
||
105 | foreach($rows as $row) |
||
106 | { |
||
107 | $row = array_values($row); |
||
108 | |||
109 | // Workaround for Quercus |
||
110 | foreach($row as &$r) |
||
111 | { |
||
112 | $r = $this->getDriver()->quote($r); |
||
113 | } |
||
114 | $row = array_map('trim', $row); |
||
115 | |||
116 | $rowString = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');'; |
||
117 | |||
118 | $row = NULL; |
||
119 | |||
120 | $insertRows[] = $rowString; |
||
121 | } |
||
122 | |||
123 | $outputSql .= "\n\n".implode("\n", $insertRows)."\n"; |
||
124 | } |
||
125 | |||
126 | return $outputSql; |
||
127 | } |
||
128 | } |