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 declare(strict_types=1); |
||
9 | class TableMetadata |
||
10 | { |
||
11 | /** @var string Database table name */ |
||
12 | public $tableName; |
||
13 | |||
14 | /** @var string Entity class name */ |
||
15 | public $className; |
||
16 | |||
17 | /** @var array Collection of database table columns */ |
||
18 | public $columns = []; |
||
19 | |||
20 | /** @var array Collection of database table columns types */ |
||
21 | public $columnTypes = []; |
||
22 | |||
23 | /** @var array Collection of database table columns aliases to real column names */ |
||
24 | public $columnAliases = []; |
||
25 | |||
26 | /** @var array Collection of lower case table column aliases to real column names */ |
||
27 | public $lowerColumnAliases = []; |
||
28 | |||
29 | /** @var string Database table primary field */ |
||
30 | public $primaryField; |
||
31 | |||
32 | /** @var array Collection of database UNIQUE table columns */ |
||
33 | public $uniqueColumns = []; |
||
34 | |||
35 | /** @var array Collection of database INDEXED table columns */ |
||
36 | public $indexColumns = []; |
||
37 | |||
38 | /** @var array Collection of database table columns default values */ |
||
39 | public $columnDefaults = []; |
||
40 | |||
41 | /** @var array Collection of database table columns is nullable status */ |
||
42 | public $columnNullable = []; |
||
43 | |||
44 | /** |
||
45 | * Create metadata instance from entity class name |
||
46 | * @param string $className Entity class name |
||
47 | * @deprecated This is temporary old approach |
||
48 | * @return TableMetadata Metadata instance |
||
49 | * |
||
50 | * @throws \InvalidArgumentException If entity class not found |
||
51 | */ |
||
52 | public static function fromClassName(string $className) : TableMetadata |
||
80 | |||
81 | /** |
||
82 | * Get table column type by column name or alias. |
||
83 | * |
||
84 | * @param string $columnNameOrAlias Table column name or alias |
||
85 | * |
||
86 | * @return string Table column type |
||
87 | * @throws \InvalidArgumentException |
||
88 | */ |
||
89 | View Code Duplication | public function getTableColumnType(string $columnNameOrAlias) : string |
|
101 | |||
102 | /** |
||
103 | * Get table column default value by column name or alias. |
||
104 | * |
||
105 | * @param string $columnNameOrAlias Table column name or alias |
||
106 | * |
||
107 | * @return mixed Table column default value |
||
108 | * @throws \InvalidArgumentException |
||
109 | */ |
||
110 | View Code Duplication | public function getColumnDefaultValue(string $columnNameOrAlias) |
|
122 | |||
123 | /** |
||
124 | * Get table column name by column name or alias. |
||
125 | * |
||
126 | * @param string $columnNameOrAlias Table column name or alias |
||
127 | * |
||
128 | * @return string Table column name |
||
129 | * @throws \InvalidArgumentException |
||
130 | */ |
||
131 | public function getTableColumnName(string $columnNameOrAlias) : string |
||
147 | |||
148 | /** |
||
149 | * Get table column alias by column name or alias. |
||
150 | * |
||
151 | * @param string $columnNameOrAlias Table column name or alias |
||
152 | * |
||
153 | * @return string Table column alias |
||
154 | * @throws \InvalidArgumentException |
||
155 | */ |
||
156 | public function getTableColumnAlias(string $columnNameOrAlias) : string |
||
173 | |||
174 | /** |
||
175 | * Get table primary field name. |
||
176 | * |
||
177 | * @return string Table primary field name |
||
178 | * @throws \InvalidArgumentException |
||
179 | */ |
||
180 | public function getTablePrimaryField(): string |
||
184 | |||
185 | /** |
||
186 | * Get table column index by column name or alias. |
||
187 | * |
||
188 | * @param string $columnNameOrAlias Table column name or alias |
||
189 | * |
||
190 | * @return int Table column index |
||
191 | * @throws \InvalidArgumentException |
||
192 | */ |
||
193 | public function getTableColumnIndex(string $columnNameOrAlias) : int |
||
197 | |||
198 | /** |
||
199 | * Define if passed column name or alias exists. |
||
200 | * |
||
201 | * @param string $columnNameOrAlias Table column name or alias |
||
202 | * @return bool True if passed column name or alias exists |
||
203 | */ |
||
204 | public function isColumnExists(string $columnNameOrAlias): bool |
||
208 | |||
209 | /** |
||
210 | * Is column alias exists using case insensitive search. |
||
211 | * |
||
212 | * @param string $columnAlias Column name alias |
||
213 | * @return bool True if column alias exists otherwise false |
||
214 | */ |
||
215 | protected function isColumnAliasExists(string $columnAlias): bool |
||
220 | |||
221 | /** |
||
222 | * Is column name exists. |
||
223 | * |
||
224 | * @param string $columnName Column name |
||
225 | * @return bool True if column name exists otherwise false |
||
226 | */ |
||
227 | protected function isColumnNameExists(string $columnName): bool |
||
231 | |||
232 | /** |
||
233 | * Is column nullable. |
||
234 | * |
||
235 | * @param string $columnNameOrAlias Column name or alias |
||
236 | * @return bool True if column is nullable otherwise false |
||
237 | * @throws \InvalidArgumentException |
||
238 | */ |
||
239 | public function isColumnNullable(string $columnNameOrAlias): bool |
||
243 | } |
||
244 |
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.