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 |
||
3 | namespace SymphonyCms\Installer\Migrations; |
||
4 | |||
5 | use SymphonyCms\Installer\Lib\Migration; |
||
6 | use Symphony; |
||
7 | use Exception; |
||
8 | |||
9 | class migration_220 extends Migration |
||
10 | { |
||
11 | public static function getVersion() |
||
15 | |||
16 | public static function getReleaseNotes() |
||
20 | |||
21 | public static function upgrade() |
||
22 | { |
||
23 | // 2.2.0dev |
||
24 | if (version_compare(self::$existing_version, '2.2.0dev', '<=')) { |
||
25 | Symphony::Configuration()->set('version', '2.2dev', 'symphony'); |
||
26 | if (Symphony::Database()->tableContainsField('tbl_sections_association', 'cascading_deletion')) { |
||
27 | Symphony::Database()->query( |
||
28 | 'ALTER TABLE `tbl_sections_association` CHANGE `cascading_deletion` `hide_association` ENUM("yes","no") COLLATE utf8_unicode_ci NOT NULL DEFAULT "no";' |
||
29 | ); |
||
30 | } |
||
31 | |||
32 | // Update Select table to include the new association field |
||
33 | Symphony::Database()->query('ALTER TABLE `tbl_fields_select` ADD `show_association` ENUM( "yes", "no" ) COLLATE utf8_unicode_ci NOT NULL DEFAULT "yes"'); |
||
34 | |||
35 | if (Symphony::Database()->tableContainsField('tbl_authors', 'default_section')) { |
||
36 | // Allow Authors to be set to any area in the backend. |
||
37 | Symphony::Database()->query( |
||
38 | 'ALTER TABLE `tbl_authors` CHANGE `default_section` `default_area` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL;' |
||
39 | ); |
||
40 | } |
||
41 | Symphony::Configuration()->write(); |
||
42 | } |
||
43 | |||
44 | // 2.2.0 |
||
45 | if (version_compare(self::$existing_version, '2.2', '<=')) { |
||
46 | Symphony::Configuration()->set('version', '2.2', 'symphony'); |
||
47 | Symphony::Configuration()->set('datetime_separator', ' ', 'region'); |
||
48 | Symphony::Configuration()->set('strict_error_handling', 'yes', 'symphony'); |
||
49 | |||
50 | // We've added UNIQUE KEY indexes to the Author, Checkbox, Date, Input, Textarea and Upload Fields |
||
51 | // Time to go through the entry tables and make this change as well. |
||
52 | $author = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_author`"); |
||
53 | $checkbox = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_checkbox`"); |
||
54 | $date = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_date`"); |
||
55 | $input = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_input`"); |
||
56 | $textarea = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_textarea`"); |
||
57 | $upload = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_upload`"); |
||
58 | |||
59 | $field_ids = array_merge($author, $checkbox, $date, $input, $textarea, $upload); |
||
60 | |||
61 | View Code Duplication | foreach ($field_ids as $id) { |
|
62 | $table = '`tbl_entries_data_' . $id . '`'; |
||
63 | |||
64 | try { |
||
65 | Symphony::Database()->query("ALTER TABLE " . $table . " DROP INDEX `entry_id`"); |
||
66 | } catch (Exception $ex) { |
||
67 | } |
||
68 | |||
69 | try { |
||
70 | Symphony::Database()->query("CREATE UNIQUE INDEX `entry_id` ON " . $table . " (`entry_id`)"); |
||
71 | Symphony::Database()->query("OPTIMIZE TABLE " . $table); |
||
72 | } catch (Exception $ex) { |
||
73 | } |
||
74 | } |
||
75 | } |
||
81 |