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 SQLiteDriver implements DriverInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $columns = []; |
||
22 | |||
23 | /** |
||
24 | * @var \PDO |
||
25 | */ |
||
26 | protected $pdo; |
||
27 | |||
28 | /** |
||
29 | * @param PDO $pdo |
||
30 | */ |
||
31 | 21 | public function __construct(\PDO $pdo) |
|
35 | |||
36 | /** |
||
37 | * Returns the result. |
||
38 | * |
||
39 | * @param string $tableName |
||
40 | * @return array |
||
41 | */ |
||
42 | 15 | View Code Duplication | public function getTable($tableName) |
61 | |||
62 | /** |
||
63 | * Prepares the defined columns. |
||
64 | * |
||
65 | * @param string $tableName |
||
66 | * @param mixed $row |
||
67 | * @return void |
||
68 | */ |
||
69 | 12 | protected function setColumn($tableName, $row) |
|
83 | |||
84 | /** |
||
85 | * Shows the list of tables. |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | 6 | public function showTables() |
|
106 | |||
107 | /** |
||
108 | * Sets the properties of the specified column if it does exists. |
||
109 | * |
||
110 | * @param string $tableName |
||
111 | * @param mixed $row |
||
112 | * @param \Rougin\Describe\Column &$column |
||
113 | * @return void |
||
114 | */ |
||
115 | 12 | protected function setForeignColumn($tableName, $row, Column &$column) |
|
131 | |||
132 | /** |
||
133 | * Prepares the query for getting the foreign columns. |
||
134 | * |
||
135 | * @param array $columns |
||
136 | * @param string $tableName |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function prepareForeignColumns(array $columns, $tableName) |
||
152 | |||
153 | /** |
||
154 | * Sets the properties of the specified column. |
||
155 | * |
||
156 | * @param mixed $row |
||
157 | * @param \Rougin\Describe\Column &$column |
||
158 | * @return void |
||
159 | */ |
||
160 | 12 | protected function setProperties($row, Column &$column) |
|
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.