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 |
||
7 | class SchemaParser implements Arrayable |
||
8 | { |
||
9 | /** |
||
10 | * The array of custom attributes. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $customAttributes = [ |
||
15 | 'remember_token' => 'rememberToken()', |
||
16 | 'soft_delete' => 'softDeletes()', |
||
17 | ]; |
||
18 | /** |
||
19 | * The migration schema. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $schema; |
||
24 | |||
25 | /** |
||
26 | * Create new instance. |
||
27 | * |
||
28 | * @param string|null $schema |
||
29 | */ |
||
30 | public function __construct($schema = null) |
||
34 | |||
35 | /** |
||
36 | * Render up migration fields. |
||
37 | * |
||
38 | * @return string |
||
39 | */ |
||
40 | public function up() |
||
44 | |||
45 | /** |
||
46 | * Render the migration to formatted script. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | View Code Duplication | public function render() |
|
59 | |||
60 | /** |
||
61 | * Convert string migration to array. |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | public function toArray() |
||
69 | |||
70 | /** |
||
71 | * Parse a string to array of formatted schema. |
||
72 | * |
||
73 | * @param string $schema |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | View Code Duplication | public function parse($schema) |
|
89 | |||
90 | /** |
||
91 | * Get array of schema. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getSchemas() |
||
103 | |||
104 | /** |
||
105 | * Get column name from schema. |
||
106 | * |
||
107 | * @param string $schema |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getColumn($schema) |
||
117 | |||
118 | /** |
||
119 | * Get column attributes. |
||
120 | * |
||
121 | * @param string $column |
||
122 | * @param string $schema |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getAttributes($column, $schema) |
||
132 | |||
133 | /** |
||
134 | * Determinte whether the given column is exist in customAttributes array. |
||
135 | * |
||
136 | * @param string $column |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function hasCustomAttribute($column) |
||
144 | |||
145 | /** |
||
146 | * Get custom attributes value. |
||
147 | * |
||
148 | * @param string $column |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getCustomAttribute($column) |
||
156 | |||
157 | /** |
||
158 | * Create field. |
||
159 | * |
||
160 | * @param string $column |
||
161 | * @param array $attributes |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function createField($column, $attributes, $type = 'add') |
||
174 | |||
175 | /** |
||
176 | * Render down migration fields. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | View Code Duplication | public function down() |
|
189 | |||
190 | /** |
||
191 | * Format field to script. |
||
192 | * |
||
193 | * @param int $key |
||
194 | * @param string $field |
||
195 | * @param string $column |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function addColumn($key, $field, $column) |
||
213 | |||
214 | /** |
||
215 | * Format field to script. |
||
216 | * |
||
217 | * @param int $key |
||
218 | * @param string $field |
||
219 | * @param string $column |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | protected function removeColumn($key, $field, $column) |
||
231 | } |
||
232 |
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.