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 MySQLDriver implements DriverInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $columns = []; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $database; |
||
27 | |||
28 | /** |
||
29 | * @var \PDO |
||
30 | */ |
||
31 | protected $pdo; |
||
32 | |||
33 | /** |
||
34 | * @param PDO $pdo |
||
35 | * @param string $database |
||
36 | */ |
||
37 | 60 | public function __construct(\PDO $pdo, $database) |
|
42 | |||
43 | /** |
||
44 | * Returns the result. |
||
45 | * |
||
46 | * @param string $tableName |
||
47 | * @return array |
||
48 | */ |
||
49 | 51 | View Code Duplication | public function getTable($tableName) |
|
|||
50 | { |
||
51 | 51 | $this->columns = []; |
|
52 | |||
53 | try { |
||
54 | 51 | $information = $this->pdo->prepare('DESCRIBE ' . $tableName); |
|
55 | |||
56 | 51 | $information->execute(); |
|
57 | 51 | $information->setFetchMode(\PDO::FETCH_OBJ); |
|
58 | 51 | } catch (\PDOException $e) { |
|
59 | // Table not found |
||
60 | } |
||
61 | |||
62 | 51 | while ($row = $information->fetch()) { |
|
63 | 48 | $this->setColumn($tableName, $row); |
|
64 | 48 | } |
|
65 | |||
66 | 51 | return $this->columns; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Shows the list of tables. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 9 | public function showTables() |
|
87 | |||
88 | /** |
||
89 | * Prepares the defined columns. |
||
90 | * |
||
91 | * @param string $tableName |
||
92 | * @param mixed $row |
||
93 | * @return void |
||
94 | */ |
||
95 | 48 | protected function setColumn($tableName, $row) |
|
96 | { |
||
97 | 48 | preg_match('/(.*?)\((.*?)\)/', $row->Type, $match); |
|
98 | |||
99 | 48 | $column = new Column; |
|
100 | |||
101 | 48 | $this->setProperties($row, $column); |
|
102 | 48 | $this->setKey($row, $column); |
|
103 | |||
104 | 48 | $column->setDataType($row->Type); |
|
105 | 48 | $column->setDefaultValue($row->Default); |
|
106 | 48 | $column->setField($row->Field); |
|
107 | |||
108 | 48 | if (isset($match[1])) { |
|
109 | 48 | $column->setDataType($match[1]); |
|
110 | 48 | $column->setLength($match[2]); |
|
111 | 48 | } |
|
112 | |||
113 | 48 | $this->setForeignColumn($tableName, $row, $column); |
|
114 | |||
115 | 48 | array_push($this->columns, $column); |
|
116 | 48 | } |
|
117 | |||
118 | /** |
||
119 | * Sets the key of the specified column. |
||
120 | * |
||
121 | * @param mixed $row |
||
122 | * @param \Rougin\Describe\Column &$column |
||
123 | * @return void |
||
124 | */ |
||
125 | 48 | protected function setKey($row, Column &$column) |
|
144 | |||
145 | /** |
||
146 | * Sets the properties of the specified column if it does exists. |
||
147 | * |
||
148 | * @param string $tableName |
||
149 | * @param mixed $row |
||
150 | * @param \Rougin\Describe\Column &$column |
||
151 | * @return void |
||
152 | */ |
||
153 | 48 | protected function setForeignColumn($tableName, $row, Column &$column) |
|
176 | |||
177 | /** |
||
178 | * Sets the properties of the specified column. |
||
179 | * |
||
180 | * @param mixed $row |
||
181 | * @param \Rougin\Describe\Column &$column |
||
182 | * @return void |
||
183 | */ |
||
184 | 48 | protected function setProperties($row, Column &$column) |
|
196 | |||
197 | /** |
||
198 | * Strips the table schema from the table name. |
||
199 | * |
||
200 | * @param string $table |
||
201 | * @return string |
||
202 | */ |
||
203 | 45 | protected function stripTableSchema($table) |
|
207 | } |
||
208 |
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.