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 |
||
| 46 | abstract class AbstractMigration implements MigrationInterface |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $environment; |
||
| 52 | /** |
||
| 53 | * @var float |
||
| 54 | */ |
||
| 55 | protected $version; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
| 59 | */ |
||
| 60 | protected $adapter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 64 | */ |
||
| 65 | protected $output; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 69 | */ |
||
| 70 | protected $input; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Whether this migration is being applied or reverted |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $isMigratingUp = true; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Class Constructor. |
||
| 81 | * |
||
| 82 | 406 | * @param string $environment Environment Detected |
|
| 83 | * @param int $version Migration Version |
||
| 84 | 406 | * @param \Symfony\Component\Console\Input\InputInterface|null $input |
|
| 85 | 406 | * @param \Symfony\Component\Console\Output\OutputInterface|null $output |
|
| 86 | 384 | */ |
|
| 87 | 384 | final public function __construct($environment, $version, InputInterface $input = null, OutputInterface $output = null) |
|
| 102 | 406 | ||
| 103 | /** |
||
| 104 | * Initialize method. |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | 1 | */ |
|
| 108 | protected function init() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | 1 | */ |
|
| 115 | public function up() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | 21 | */ |
|
| 122 | public function down() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function setAdapter(AdapterInterface $adapter) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | 384 | */ |
|
| 139 | public function getAdapter() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | 2 | public function setInput(InputInterface $input) |
|
| 148 | { |
||
| 149 | 2 | $this->input = $input; |
|
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | 385 | * {@inheritdoc} |
|
| 156 | */ |
||
| 157 | 385 | public function getInput() |
|
| 158 | 385 | { |
|
| 159 | return $this->input; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | 3 | */ |
|
| 165 | public function setOutput(OutputInterface $output) |
||
| 171 | |||
| 172 | 140 | /** |
|
| 173 | * {@inheritdoc} |
||
| 174 | 140 | */ |
|
| 175 | public function getOutput() |
||
| 179 | |||
| 180 | 1 | /** |
|
| 181 | * {@inheritdoc} |
||
| 182 | 1 | */ |
|
| 183 | 1 | public function getName() |
|
| 187 | |||
| 188 | /** |
||
| 189 | 279 | * {@inheritdoc} |
|
| 190 | */ |
||
| 191 | 279 | public function getEnvironment() |
|
| 195 | |||
| 196 | /** |
||
| 197 | 10 | * {@inheritdoc} |
|
| 198 | */ |
||
| 199 | 10 | public function setVersion($version) |
|
| 200 | 10 | { |
|
| 201 | $this->version = $version; |
||
| 202 | |||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | 2 | /** |
|
| 207 | * {@inheritdoc} |
||
| 208 | 2 | */ |
|
| 209 | public function getVersion() |
||
| 210 | { |
||
| 211 | return $this->version; |
||
| 212 | } |
||
| 213 | |||
| 214 | 3 | /** |
|
| 215 | * {@inheritdoc} |
||
| 216 | 3 | */ |
|
| 217 | public function setMigratingUp($isMigratingUp) |
||
| 218 | { |
||
| 219 | $this->isMigratingUp = $isMigratingUp; |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | 1 | } |
|
| 223 | |||
| 224 | 1 | /** |
|
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function isMigratingUp() |
||
| 228 | { |
||
| 229 | return $this->isMigratingUp; |
||
| 230 | 1 | } |
|
| 231 | |||
| 232 | 1 | /** |
|
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function execute($sql) |
||
| 236 | { |
||
| 237 | return $this->getAdapter()->execute($sql); |
||
| 238 | 1 | } |
|
| 239 | |||
| 240 | 1 | /** |
|
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function query($sql) |
||
| 244 | { |
||
| 245 | return $this->getAdapter()->query($sql); |
||
| 246 | 3 | } |
|
| 247 | |||
| 248 | /** |
||
| 249 | 3 | * {@inheritdoc} |
|
| 250 | 2 | */ |
|
| 251 | 2 | public function getQueryBuilder() |
|
| 252 | 3 | { |
|
| 253 | 3 | return $this->getAdapter()->getQueryBuilder(); |
|
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | 1 | */ |
|
| 259 | public function fetchRow($sql) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | 1 | */ |
|
| 267 | public function fetchAll($sql) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | 1 | */ |
|
| 275 | View Code Duplication | public function insert($table, $data) |
|
| 276 | 1 | { |
|
| 277 | // convert to table object |
||
| 278 | if (is_string($table)) { |
||
| 279 | $table = new Table($table, [], $this->getAdapter()); |
||
| 280 | } |
||
| 281 | $table->insert($data)->save(); |
||
| 282 | 5 | } |
|
| 283 | |||
| 284 | 5 | /** |
|
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function createDatabase($name, $options) |
||
| 291 | |||
| 292 | /** |
||
| 293 | 1 | * {@inheritdoc} |
|
| 294 | */ |
||
| 295 | 1 | public function dropDatabase($name) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function hasTable($tableName) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function table($tableName, $options = []) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * A short-hand method to drop the given database table. |
||
| 318 | * |
||
| 319 | * @param string $tableName Table Name |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | public function dropTable($tableName) |
||
| 326 | } |
||
| 327 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.