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 |
||
22 | class PostgresDriver extends Driver |
||
23 | { |
||
24 | /** |
||
25 | * Driver type. |
||
26 | */ |
||
27 | const TYPE = DatabaseInterface::POSTGRES; |
||
28 | |||
29 | /** |
||
30 | * Driver schemas. |
||
31 | */ |
||
32 | const SCHEMA_TABLE = TableSchema::class; |
||
33 | |||
34 | /** |
||
35 | * Commander used to execute commands. :) |
||
36 | */ |
||
37 | const COMMANDER = Commander::class; |
||
38 | |||
39 | /** |
||
40 | * Query compiler class. |
||
41 | */ |
||
42 | const QUERY_COMPILER = QueryCompiler::class; |
||
43 | |||
44 | /** |
||
45 | * Default timestamp expression. |
||
46 | */ |
||
47 | const TIMESTAMP_NOW = 'now()'; |
||
48 | |||
49 | /** |
||
50 | * Cached list of primary keys associated with their table names. Used by InsertBuilder to |
||
51 | * emulate last insert id. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private $primaryKeys = []; |
||
56 | |||
57 | /** |
||
58 | * Needed to remember table primary keys. |
||
59 | * |
||
60 | * @invisible |
||
61 | * @var HippocampusInterface |
||
62 | */ |
||
63 | protected $memory = null; |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | * @param string $name |
||
68 | * @param array $config |
||
69 | * @param FactoryInterface $factory |
||
70 | * @param HippocampusInterface $memory |
||
71 | */ |
||
72 | public function __construct( |
||
73 | $name, |
||
74 | array $config, |
||
75 | FactoryInterface $factory = null, |
||
76 | HippocampusInterface $memory = null |
||
77 | ) { |
||
78 | parent::__construct($name, $config, $factory); |
||
79 | |||
80 | $this->memory = $memory; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function hasTable($name) |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | View Code Duplication | public function tableNames() |
|
109 | |||
110 | /** |
||
111 | * Get singular primary key associated with desired table. Used to emulate last insert id. |
||
112 | * |
||
113 | * @param string $table Fully specified table name, including postfix. |
||
114 | * @return string |
||
115 | * @throws DriverException |
||
116 | */ |
||
117 | public function getPrimary($table) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | View Code Duplication | public function insertBuilder(Database $database, array $parameters = []) |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | protected function createPDO() |
||
171 | } |
||
172 |
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.