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 |
||
| 12 | abstract class DbDumper |
||
| 13 | { |
||
| 14 | /** @var string */ |
||
| 15 | protected $dbName; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | protected $userName; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $password; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $host = 'localhost'; |
||
| 25 | |||
| 26 | /** @var int */ |
||
| 27 | protected $port = 5432; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | protected $socket = ''; |
||
| 31 | |||
| 32 | /** @var int */ |
||
| 33 | protected $timeout = 0; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | protected $dumpBinaryPath = ''; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | protected $includeTables = []; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | protected $excludeTables = []; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | protected $extraOptions = []; |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | protected $compressor = null; |
||
| 49 | |||
| 50 | public static function create() |
||
| 51 | { |
||
| 52 | return new static(); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getDbName(): string |
||
| 56 | { |
||
| 57 | return $this->dbName; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $dbName |
||
| 62 | * |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | public function setDbName(string $dbName) |
||
| 66 | { |
||
| 67 | $this->dbName = $dbName; |
||
| 68 | |||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $userName |
||
| 74 | * |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | public function setUserName(string $userName) |
||
| 78 | { |
||
| 79 | $this->userName = $userName; |
||
| 80 | |||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $password |
||
| 86 | * |
||
| 87 | * @return $this |
||
| 88 | */ |
||
| 89 | public function setPassword(string $password) |
||
| 90 | { |
||
| 91 | $this->password = $password; |
||
| 92 | |||
| 93 | return $this; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $host |
||
| 98 | * |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function setHost(string $host) |
||
| 102 | { |
||
| 103 | $this->host = $host; |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getHost(): string |
||
| 109 | { |
||
| 110 | return $this->host; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param int $port |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function setPort(int $port) |
||
| 119 | { |
||
| 120 | $this->port = $port; |
||
| 121 | |||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $socket |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function setSocket(string $socket) |
||
| 131 | { |
||
| 132 | $this->socket = $socket; |
||
| 133 | |||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param int $timeout |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function setTimeout(int $timeout) |
||
| 143 | { |
||
| 144 | $this->timeout = $timeout; |
||
| 145 | |||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $dumpBinaryPath |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function setDumpBinaryPath(string $dumpBinaryPath) |
||
| 155 | { |
||
| 156 | if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') { |
||
| 157 | $dumpBinaryPath .= '/'; |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->dumpBinaryPath = $dumpBinaryPath; |
||
| 161 | |||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function enableCompression() |
||
| 166 | { |
||
| 167 | return $this->GzipCompression(); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $level |
||
| 172 | * |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function GzipCompression(string $level = '9') |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $level |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function Bzip2Compression(string $level = '9') |
||
| 190 | { |
||
| 191 | $this->compressor = new Bzip2Compressor; |
||
| 192 | |||
| 193 | $this->compressor->setLevel($level); |
||
| 194 | |||
| 195 | return $this; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $level |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function LzmaCompression(string $level = 'e') |
||
| 211 | |||
| 212 | public function getCompressorExtension() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string|array $includeTables |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | * |
||
| 222 | * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter |
||
| 223 | */ |
||
| 224 | View Code Duplication | public function includeTables($includeTables) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param string|array $excludeTables |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | * |
||
| 244 | * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter |
||
| 245 | */ |
||
| 246 | View Code Duplication | public function excludeTables($excludeTables) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $extraOption |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function addExtraOption(string $extraOption) |
||
| 274 | |||
| 275 | abstract public function dumpToFile(string $dumpFile); |
||
| 276 | |||
| 277 | protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile) |
||
| 291 | |||
| 292 | protected function echoToFile(string $command, string $dumpFile): string |
||
| 299 | } |
||
| 300 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..