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 |
||
| 21 | class PostgresDriver extends Driver |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Driver type. |
||
| 25 | */ |
||
| 26 | const TYPE = DatabaseInterface::POSTGRES; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Driver schemas. |
||
| 30 | */ |
||
| 31 | //const SCHEMA_TABLE = TableSchema::class; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Query compiler class. |
||
| 35 | */ |
||
| 36 | const QUERY_COMPILER = PostgresCompiler::class; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Default timestamp expression. |
||
| 40 | */ |
||
| 41 | const DATETIME_NOW = 'now()'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Cached list of primary keys associated with their table names. Used by InsertBuilder to |
||
| 45 | * emulate last insert id. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $primaryKeys = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function hasTable(string $name): bool |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | public function truncateData(string $table) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function tableNames(): array |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get singular primary key associated with desired table. Used to emulate last insert id. |
||
| 86 | * |
||
| 87 | * @param string $prefix Database prefix if any. |
||
| 88 | * @param string $table Fully specified table name, including postfix. |
||
| 89 | * |
||
| 90 | * @return string|null |
||
| 91 | * |
||
| 92 | * @throws DriverException |
||
| 93 | */ |
||
| 94 | public function getPrimary(string $prefix, string $table): string |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | * |
||
| 129 | * Postgres uses custom insert query builder in order to return value of inserted row. |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function insertBuilder(string $prefix, array $parameters = []): InsertQuery |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | protected function createPDO(): \PDO |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function getHandler(LoggerInterface $logger = null): AbstractHandler |
||
| 158 | } |
||
| 159 |
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.