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 |
||
| 16 | class MySQLTable extends AbstractTable |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * List of most common MySQL table engines. |
||
| 20 | */ |
||
| 21 | const ENGINE_INNODB = 'InnoDB'; |
||
| 22 | const ENGINE_MYISAM = 'MyISAM'; |
||
| 23 | const ENGINE_MEMORY = 'Memory'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * MySQL table engine. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $engine = self::ENGINE_INNODB; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Populate table schema with values from database. |
||
| 34 | * |
||
| 35 | * @param TableState $state |
||
| 36 | */ |
||
| 37 | protected function initSchema(TableState $state) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Change table engine. Such operation will be applied only at moment of table creation. |
||
| 49 | * |
||
| 50 | * @param string $engine |
||
| 51 | * |
||
| 52 | * @return $this |
||
| 53 | * |
||
| 54 | * @throws SchemaException |
||
| 55 | */ |
||
| 56 | public function setEngine($engine) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getEngine() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | View Code Duplication | protected function fetchColumns(): array |
|
|
|
|||
| 79 | { |
||
| 80 | $query = "SHOW FULL COLUMNS FROM {$this->driver->identifier($this->getName())}"; |
||
| 81 | |||
| 82 | $result = []; |
||
| 83 | foreach ($this->driver->query($query) as $schema) { |
||
| 84 | $result[] = MySQLColumn::createInstance($this->getName(), $schema); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $result; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | protected function fetchIndexes(): array |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | protected function fetchReferences(): array |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Fetching primary keys from table. |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | protected function fetchPrimaryKeys(): array |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | protected function createColumn(string $name): AbstractColumn |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | protected function createIndex(string $name): AbstractIndex |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | protected function createForeign(string $column): AbstractReference |
||
| 185 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.