Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php | ||
| 11 | abstract class DbDumper | ||
| 12 | { | ||
| 13 | /** @var string */ | ||
| 14 | protected $dbName; | ||
| 15 | |||
| 16 | /** @var string */ | ||
| 17 | protected $userName; | ||
| 18 | |||
| 19 | /** @var string */ | ||
| 20 | protected $password; | ||
| 21 | |||
| 22 | /** @var string */ | ||
| 23 | protected $host = 'localhost'; | ||
| 24 | |||
| 25 | /** @var int */ | ||
| 26 | protected $port = 5432; | ||
| 27 | |||
| 28 | /** @var string */ | ||
| 29 | protected $socket = ''; | ||
| 30 | |||
| 31 | /** @var int */ | ||
| 32 | protected $timeout = 0; | ||
| 33 | |||
| 34 | /** @var string */ | ||
| 35 | protected $dumpBinaryPath = ''; | ||
| 36 | |||
| 37 | /** @var array */ | ||
| 38 | protected $includeTables = []; | ||
| 39 | |||
| 40 | /** @var array */ | ||
| 41 | protected $excludeTables = []; | ||
| 42 | |||
| 43 | /** @var array */ | ||
| 44 | protected $extraOptions = []; | ||
| 45 | |||
| 46 | /** @var object */ | ||
| 47 | protected $compressor = null; | ||
| 48 | |||
| 49 | public static function create() | ||
| 50 |     { | ||
| 51 | return new static(); | ||
| 52 | } | ||
| 53 | |||
| 54 | public function getDbName(): string | ||
| 55 |     { | ||
| 56 | return $this->dbName; | ||
| 57 | } | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @param string $dbName | ||
| 61 | * | ||
| 62 | * @return $this | ||
| 63 | */ | ||
| 64 | public function setDbName(string $dbName) | ||
| 65 |     { | ||
| 66 | $this->dbName = $dbName; | ||
| 67 | |||
| 68 | return $this; | ||
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @param string $userName | ||
| 73 | * | ||
| 74 | * @return $this | ||
| 75 | */ | ||
| 76 | public function setUserName(string $userName) | ||
| 77 |     { | ||
| 78 | $this->userName = $userName; | ||
| 79 | |||
| 80 | return $this; | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * @param string $password | ||
| 85 | * | ||
| 86 | * @return $this | ||
| 87 | */ | ||
| 88 | public function setPassword(string $password) | ||
| 89 |     { | ||
| 90 | $this->password = $password; | ||
| 91 | |||
| 92 | return $this; | ||
| 93 | } | ||
| 94 | |||
| 95 | /** | ||
| 96 | * @param string $host | ||
| 97 | * | ||
| 98 | * @return $this | ||
| 99 | */ | ||
| 100 | public function setHost(string $host) | ||
| 101 |     { | ||
| 102 | $this->host = $host; | ||
| 103 | |||
| 104 | return $this; | ||
| 105 | } | ||
| 106 | |||
| 107 | public function getHost(): string | ||
| 108 |     { | ||
| 109 | return $this->host; | ||
| 110 | } | ||
| 111 | |||
| 112 | /** | ||
| 113 | * @param int $port | ||
| 114 | * | ||
| 115 | * @return $this | ||
| 116 | */ | ||
| 117 | public function setPort(int $port) | ||
| 118 |     { | ||
| 119 | $this->port = $port; | ||
| 120 | |||
| 121 | return $this; | ||
| 122 | } | ||
| 123 | |||
| 124 | /** | ||
| 125 | * @param string $socket | ||
| 126 | * | ||
| 127 | * @return $this | ||
| 128 | */ | ||
| 129 | public function setSocket(string $socket) | ||
| 130 |     { | ||
| 131 | $this->socket = $socket; | ||
| 132 | |||
| 133 | return $this; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * @param int $timeout | ||
| 138 | * | ||
| 139 | * @return $this | ||
| 140 | */ | ||
| 141 | public function setTimeout(int $timeout) | ||
| 142 |     { | ||
| 143 | $this->timeout = $timeout; | ||
| 144 | |||
| 145 | return $this; | ||
| 146 | } | ||
| 147 | |||
| 148 | public function setDumpBinaryPath(string $dumpBinaryPath) | ||
| 149 |     { | ||
| 150 |         if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') { | ||
| 151 | $dumpBinaryPath .= '/'; | ||
| 152 | } | ||
| 153 | |||
| 154 | $this->dumpBinaryPath = $dumpBinaryPath; | ||
| 155 | |||
| 156 | return $this; | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * @deprecated | ||
| 161 | * | ||
| 162 | * @return $this | ||
| 163 | */ | ||
| 164 | public function enableCompression() | ||
| 165 |     { | ||
| 166 | $this->compressor = new GzipCompressor(); | ||
| 167 | |||
| 168 | return $this; | ||
| 169 | } | ||
| 170 | |||
| 171 | public function getCompressorExtension(): string | ||
| 172 |     { | ||
| 173 | return $this->compressor->useExtension(); | ||
| 174 | } | ||
| 175 | |||
| 176 | public function useCompressor(Compressor $compressor) | ||
| 177 |     { | ||
| 178 | $this->compressor = $compressor; | ||
| 179 | |||
| 180 | return $this; | ||
| 181 | } | ||
| 182 | |||
| 183 | /** | ||
| 184 | * @param string|array $includeTables | ||
| 185 | * | ||
| 186 | * @return $this | ||
| 187 | * | ||
| 188 | * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter | ||
| 189 | */ | ||
| 190 | public function includeTables($includeTables) | ||
| 191 |     { | ||
| 192 |         if (! empty($this->excludeTables)) { | ||
| 193 |             throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables'); | ||
| 194 | } | ||
| 195 | |||
| 196 |         if (! is_array($includeTables)) { | ||
| 197 |             $includeTables = explode(', ', $includeTables); | ||
| 198 | } | ||
| 199 | |||
| 200 | $this->includeTables = $includeTables; | ||
| 201 | |||
| 202 | return $this; | ||
| 203 | } | ||
| 204 | |||
| 205 | /** | ||
| 206 | * @param string|array $excludeTables | ||
| 207 | * | ||
| 208 | * @return $this | ||
| 209 | * | ||
| 210 | * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter | ||
| 211 | */ | ||
| 212 | public function excludeTables($excludeTables) | ||
| 213 |     { | ||
| 214 |         if (! empty($this->includeTables)) { | ||
| 215 |             throw CannotSetParameter::conflictingParameters('excludeTables', 'includeTables'); | ||
| 216 | } | ||
| 217 | |||
| 218 |         if (! is_array($excludeTables)) { | ||
| 219 |             $excludeTables = explode(', ', $excludeTables); | ||
| 220 | } | ||
| 221 | |||
| 222 | $this->excludeTables = $excludeTables; | ||
| 223 | |||
| 224 | return $this; | ||
| 225 | } | ||
| 226 | |||
| 227 | /** | ||
| 228 | * @param string $extraOption | ||
| 229 | * | ||
| 230 | * @return $this | ||
| 231 | */ | ||
| 232 | public function addExtraOption(string $extraOption) | ||
| 233 |     { | ||
| 234 |         if (! empty($extraOption)) { | ||
| 235 | $this->extraOptions[] = $extraOption; | ||
| 236 | } | ||
| 237 | |||
| 238 | return $this; | ||
| 239 | } | ||
| 240 | |||
| 241 | abstract public function dumpToFile(string $dumpFile); | ||
| 242 | |||
| 243 | protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile) | ||
| 244 |     { | ||
| 245 |         if (! $process->isSuccessful()) { | ||
| 246 | throw DumpFailed::processDidNotEndSuccessfully($process); | ||
| 247 | } | ||
| 248 | |||
| 249 |         if (! file_exists($outputFile)) { | ||
| 250 | throw DumpFailed::dumpfileWasNotCreated(); | ||
| 251 | } | ||
| 252 | |||
| 253 |         if (filesize($outputFile) === 0) { | ||
| 254 | throw DumpFailed::dumpfileWasEmpty(); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | protected function echoToFile(string $command, string $dumpFile): string | ||
| 259 |     { | ||
| 260 | $dumpFile = '"'.addcslashes($dumpFile, '\\"').'"'; | ||
| 261 | |||
| 262 |         if ($this->compressor) { | ||
| 263 | $compressCommand = $this->compressor->useCommand(); | ||
| 264 | |||
| 265 | return <<<BASH | ||
| 266 |             if output=\$({$command}); | ||
| 267 | then | ||
| 268 | echo "\$output" | $compressCommand > $dumpFile | ||
| 269 | else | ||
| 279 |