| Conditions | 15 |
| Paths | 18 |
| Total Lines | 72 |
| Code Lines | 49 |
| 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 |
||
| 105 | public function dumpTable(string $name, int $mode, bool $isView = false): string |
||
| 106 | { |
||
| 107 | $quotedTable = $this->quoteName($name); |
||
| 108 | $query = $this->connection->query(sprintf('SHOW CREATE TABLE %s', $quotedTable)); |
||
| 109 | $row = $query->fetchAssoc()->get(); |
||
| 110 | |||
| 111 | $result = '-- --------------------------------------------------------' . "\n\n"; |
||
| 112 | if ($mode & DatabaseDump::DROP) { |
||
| 113 | $result .= sprintf('DROP %s IF EXISTS %s;', $isView ? 'VIEW' : 'TABLE', $quotedTable) . "\n\n"; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($mode & DatabaseDump::CREATE) { |
||
| 117 | $result .= ($row[$isView ? 'Create View' : 'Create Table']) . ";\n\n"; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($isView === false && ($mode & DatabaseDump::DATA)) { |
||
| 121 | $result .= sprintf('ALTER TABLE %s DISABLE KEYS;', $quotedTable) . "\n\n"; |
||
| 122 | $numerics = []; |
||
| 123 | $query = $this->connection->query(sprintf('SHOW COLUMNS FROM %s', $quotedTable)); |
||
| 124 | |||
| 125 | $queryResult = $query->fetchAssoc()->all(); |
||
| 126 | $columns = []; |
||
| 127 | foreach ($queryResult as $res) { |
||
| 128 | $column = $res['Field']; |
||
| 129 | $columns[] = $this->quoteName($column); |
||
| 130 | $numerics[$column] = (bool) preg_match( |
||
| 131 | '#^[^(]*(BYTE|COUNTER|SERIAL|INT|LONG$|CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER)#i', |
||
| 132 | $res['Type'] |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | $columns = '(' . implode(', ', $columns) . ')'; |
||
| 137 | $size = 0; |
||
| 138 | $queryData = $this->connection->query(sprintf('SELECT * FROM %s', $quotedTable)); |
||
| 139 | $rows = $queryData->fetchAssoc()->all(); |
||
| 140 | foreach ($rows as $row) { |
||
| 141 | $str = '('; |
||
| 142 | foreach ($row as $key => $value) { |
||
| 143 | if ($value === null) { |
||
| 144 | $str .= "NULL,\t"; |
||
| 145 | } elseif ($numerics[$key]) { |
||
| 146 | $str .= NumberHelper::numberToString($value) . ",\t"; |
||
| 147 | } else { |
||
| 148 | $str .= $this->connection->getDriver()->quote($value) . ",\t"; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($size === 0) { |
||
| 153 | $str = sprintf("INSERT INTO %s %s VALUES\n%s", $quotedTable, $columns, $str); |
||
| 154 | } else { |
||
| 155 | $str = ',' . "\n" . $str; |
||
| 156 | } |
||
| 157 | |||
| 158 | $length = strlen($str) - 1; |
||
| 159 | $str[$length - 1] = ')'; |
||
| 160 | |||
| 161 | $result .= $str; |
||
| 162 | $size += $length; |
||
| 163 | if ($size > DatabaseDump::MAX_SQL_SIZE) { |
||
| 164 | $result .= ";\n"; |
||
| 165 | $size = 0; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if ($size > 0) { |
||
| 170 | $result .= ";\n\n"; |
||
| 171 | } |
||
| 172 | $result .= sprintf('ALTER TABLE %s ENABLE KEYS;', $quotedTable) . "\n\n"; |
||
| 173 | $result .= "\n"; |
||
| 174 | } |
||
| 175 | |||
| 176 | return $result; |
||
| 177 | } |
||
| 239 |