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 |
||
11 | abstract class MigrationApi |
||
12 | { |
||
13 | private $actions = null; |
||
14 | private $manipulation; |
||
15 | private $info; |
||
16 | |||
17 | /** |
||
18 | * Constructor. |
||
19 | * |
||
20 | * @param Manipulation $manipulation Manipulation instance. |
||
21 | * @param Info $info Info instance. |
||
22 | */ |
||
23 | public function __construct(Manipulation $manipulation, Info $info) |
||
29 | |||
30 | /** |
||
31 | * Create new table. |
||
32 | * |
||
33 | * @param string $name Table name. |
||
34 | * @param array $options Optional options. |
||
35 | * |
||
36 | * @return CreationTableApi CreationTableApi instance. |
||
37 | */ |
||
38 | public function createTable($name, array $options = []) |
||
61 | |||
62 | /** |
||
63 | * Drop table. |
||
64 | * |
||
65 | * @param string $name Table name. |
||
66 | * @param array $options Optional options. |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function dropTable($name, array $options = []) |
||
80 | |||
81 | /** |
||
82 | * Alter table. |
||
83 | * |
||
84 | * @param string $name Table name. |
||
85 | * @param array $options Optional options. |
||
86 | * |
||
87 | * @return AlterationTableApi |
||
88 | */ |
||
89 | public function alterTable($name, array $options = []) |
||
95 | |||
96 | /** |
||
97 | * Create new schema. |
||
98 | * |
||
99 | * @param string $name Schema name. |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | View Code Duplication | public function createSchema($name) |
|
110 | |||
111 | /** |
||
112 | * Drop schema. |
||
113 | * |
||
114 | * @param string $name Schema name. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | View Code Duplication | public function dropSchema($name) |
|
125 | |||
126 | /** |
||
127 | * Execute SQL. |
||
128 | * |
||
129 | * @param string $sql SQL to execute. |
||
130 | * |
||
131 | * @return integer |
||
132 | */ |
||
133 | public function execute($sql) |
||
137 | |||
138 | /** |
||
139 | * Get actions to execute. |
||
140 | * |
||
141 | * @return array Actions to execute. |
||
142 | */ |
||
143 | public function getActions() |
||
147 | } |
||
148 |
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.