| Conditions | 9 |
| Paths | 56 |
| Total Lines | 62 |
| Code Lines | 39 |
| 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 |
||
| 89 | public function export(TableNodeInterface $table) |
||
| 90 | { |
||
| 91 | if (is_null($this->file)) { |
||
| 92 | $this->file = $this->getTemporaryFile(); |
||
| 93 | } |
||
| 94 | |||
| 95 | $targetFormat = $this->format; |
||
| 96 | |||
| 97 | $helper = $this->getBuilder()->build(MysqlHelper::class); |
||
| 98 | |||
| 99 | if (is_null($this->format) |
||
| 100 | || !$helper->isValidExportFormat($this->format) |
||
| 101 | ) { |
||
| 102 | $this->format = $helper->getDefaultExportFormat(); |
||
| 103 | } |
||
| 104 | $targetFormat = $targetFormat ?: $this->format; |
||
| 105 | |||
| 106 | $this->log(LogLevel::INFO, "Exporting mysql table: {table} to file: {file}", [ |
||
| 107 | 'table' => $table, |
||
| 108 | 'file' => $this->file, |
||
| 109 | ]); |
||
| 110 | |||
| 111 | if (count($table->getColumns()) > 0) { |
||
| 112 | $exporter = $this->getBuilder()->build(QueryExporter::class, $this->file, $targetFormat); |
||
| 113 | list ($sql, $bind) = $table->getAdapter()->getDialect()->getSelectSyntax($table); |
||
| 114 | return $exporter->export($this->getBuilder()->build(QueryNode::class, $table->getAdapter(), $sql, $bind)); |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($table instanceof SourceTableNodeInterface) { |
||
| 118 | $where = ($table->getWhere() ? "--where=" . escapeshellarg($table->getWhere()) . " " : ''); |
||
| 119 | } else { |
||
| 120 | $where = ''; |
||
| 121 | } |
||
| 122 | |||
| 123 | $cmd = "set -e; set -o pipefail; " . |
||
| 124 | "mysqldump -h{$this->host} -u{$this->user} -p{$this->pass} -P{$this->port} " . |
||
| 125 | "--no-create-info --compact --compress --quick --skip-extended-insert --single-transaction " . |
||
| 126 | "--skip-tz-utc --order-by-primary " . |
||
| 127 | $where . |
||
| 128 | "{$table->getSchema()} {$table->getTable()} " . |
||
| 129 | "| grep '^INSERT INTO' " . |
||
| 130 | "| perl -p -e 's/^INSERT INTO `[^`]+` VALUES \\((.+)\\)\\;\\s?$/\\1\\n/' " . |
||
| 131 | "| perl -p -e 's/(?<!\\\\)((?:\\\\{2})*)\\\\n/\\1\\\\\\n/g;s/(?<!\\\\)((?:\\\\{2})*)\\\\r/\\1\\\\\\r/g' " . // convert fake new lines into actual new lines again |
||
| 132 | ">> " . escapeshellarg($this->file->getPath()); |
||
| 133 | |||
| 134 | $this->log(LogLevel::DEBUG, "Executing Command:\n{cmd}", ['cmd' => $cmd]); |
||
| 135 | |||
| 136 | // add bash -c here to allow set -e and set -o pipefail to handle when a mid pipe process fails |
||
| 137 | $cmd = sprintf("bash -c %s", escapeshellarg($cmd)); |
||
| 138 | |||
| 139 | $process = $this->getProcess($cmd); |
||
| 140 | $process->setTimeout(null); // no timeout |
||
| 141 | $process->mustRun(); |
||
| 142 | |||
| 143 | if ($targetFormat !== $this->format) { |
||
| 144 | /** @var ReFormat $reFormat */ |
||
| 145 | $reFormat = $this->getBuilder()->build(ReFormat::class); |
||
| 146 | return $reFormat->reFormat($this->file, $targetFormat, null, $this->format, ['keepOldFile' => false]); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $this->file; |
||
| 150 | } |
||
| 151 | } |
||
| 152 |