GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — integration (#2604)
by Brendan
04:24
created

migration_230::upgrade()   F

Complexity

Conditions 27
Paths 1768

Size

Total Lines 179
Code Lines 84

Duplication

Lines 15
Ratio 8.38 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 27
eloc 84
c 3
b 0
f 0
nc 1768
nop 0
dl 15
loc 179
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
    class migration_230 extends Migration
4
    {
5
        public static function getVersion()
6
        {
7
            return '2.3';
8
        }
9
10
        public static function getReleaseNotes()
11
        {
12
            return 'http://getsymphony.com/download/releases/version/2.3/';
13
        }
14
15
        public static function upgrade()
16
        {
17
            // 2.3dev
18
            if (version_compare(self::$existing_version, '2.3dev', '<=')) {
19
                Symphony::Configuration()->set('version', '2.3dev', 'symphony');
20
                Symphony::Configuration()->set('useragent', 'Symphony/2.3dev', 'general');
21
22
                // Add Publish Label to `tbl_fields`
23
                if (!Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) {
24
                    Symphony::Database()->query('ALTER TABLE `tbl_fields` ADD `publish_label` VARCHAR(255) DEFAULT NULL');
25
                }
26
27
                // Migrate any Checkbox's Long Description to Publish Label
28
                try {
29
                    $checkboxes = Symphony::Database()->fetch("SELECT `field_id`, `description` FROM `tbl_fields_checkbox`");
30
31
                    foreach ($checkboxes as $field) {
0 ignored issues
show
Bug introduced by
The expression $checkboxes of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
32
                        if (!isset($field['description'])) {
33
                            continue;
34
                        }
35
36
                        Symphony::Database()->query(sprintf("
37
                            UPDATE `tbl_fields`
38
                            SET `publish_label` = '%s'
39
                            WHERE `id` = %d
40
                            LIMIT 1;
41
                            ",
42
                            $field['description'],
43
                            $field['field_id']
44
                        ));
45
                    }
46
47
                    Symphony::Database()->query("ALTER TABLE `tbl_fields_checkbox` DROP `description`");
48
                } catch (Exception $ex) {
49
                }
50
51
                // Removing unused settings
52
                Symphony::Configuration()->remove('allow_page_subscription', 'symphony');
53
                Symphony::Configuration()->remove('strict_error_handling', 'symphony');
54
                Symphony::Configuration()->remove('character_set', 'database');
55
                Symphony::Configuration()->remove('character_encoding', 'database');
56
                Symphony::Configuration()->remove('runtime_character_set_alter', 'database');
57
58
                if (Symphony::Configuration()->get('pagination_maximum_rows', 'symphony') === '17') {
59
                    Symphony::Configuration()->set('pagination_maximum_rows', '20', 'symphony');
60
                }
61
62
                Symphony::Configuration()->write();
63
            }
64
65
            // 2.3 Beta 1
66
            if (version_compare(self::$existing_version, '2.3beta1', '<=')) {
67
                Symphony::Configuration()->set('version', '2.3beta1', 'symphony');
68
                Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 1', 'general');
69
70
                Symphony::Configuration()->write();
71
            }
72
73
            // 2.3 Beta 2
74
            if (version_compare(self::$existing_version, '2.3beta2', '<=')) {
75
                // Migrate Publish Labels (if created) to the Label field
76
                // Then drop Publish Label, we're going to use element_name and label
77
                // to take care of the same functionality!
78
                try {
79
                    if (Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) {
80
                        $fields = Symphony::Database()->fetch('SELECT `publish_label`, `label`, `id` FROM `tbl_fields`');
81
82
                        foreach ($fields as $field) {
0 ignored issues
show
Bug introduced by
The expression $fields of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
83
                            if (!$field['publish_label']) {
84
                                continue;
85
                            }
86
87
                            Symphony::Database()->query(sprintf("
88
                                UPDATE `tbl_fields`
89
                                SET `label` = '%s'
90
                                WHERE `id` = %d
91
                                LIMIT 1;
92
                                ",
93
                                $field['publish_label'],
94
                                $field['id']
95
                            ));
96
                        }
97
98
                        Symphony::Database()->query("ALTER TABLE `tbl_fields` DROP `publish_label`");
99
                    }
100
                } catch (Exception $ex) {
101
                    Symphony::Log()->notice($ex->getMessage());
102
                }
103
104
                // Add uniqueness constraint for the Authors table. #937
105
                try {
106
                    Symphony::Database()->query("ALTER TABLE `tbl_authors` ADD UNIQUE KEY `email` (`email`)");
107
                } catch (DatabaseException $ex) {
108
                    // 1061 will be 'duplicate key', which is fine (means key was added earlier)
109
                    // 1062 means the key failed to apply, which is bad.
110
                    // @see http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
111
                    if ($ex->getDatabaseErrorCode() === 1062) {
112
                        Symphony::Log()->error(
113
                            __("You have multiple Authors with the same email address, which can cause issues with password retrieval. Please ensure all Authors have unique email addresses before updating. " . $ex->getMessage())
114
                        );
115
116
                        return false;
117
                    }
118
                }
119
120
                // Update the version information
121
                Symphony::Configuration()->set('version', '2.3beta2', 'symphony');
122
                Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 2', 'general');
123
124
                Symphony::Configuration()->write();
125
            }
126
127
            // 2.3 Beta 3
128
            if (version_compare(self::$existing_version, '2.3beta3', '<=')) {
129
                // Refresh indexes on existing Author field tables
130
                $author_fields = Symphony::Database()->fetchCol("field_id",
131
                    "SELECT `field_id` FROM `tbl_fields_author`");
132
133 View Code Duplication
                foreach ($author_fields as $id) {
134
                    $table = 'tbl_entries_data_' . $id;
135
136
                    // MySQL doesn't support DROP IF EXISTS, so we'll try and catch.
137
                    try {
138
                        Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP INDEX `entry_id`");
139
                    } catch (Exception $ex) {
140
                    }
141
142
                    try {
143
                        Symphony::Database()->query("CREATE UNIQUE INDEX `author` ON `" . $table . "` (`entry_id`, `author_id`)");
144
                        Symphony::Database()->query("OPTIMIZE TABLE " . $table);
145
                    } catch (Exception $ex) {
146
                    }
147
                }
148
149
                // Move section sorting data from the database to the filesystem. #977
150
                if (Symphony::Database()->tableContainsField('tbl_sections', 'entry_order')) {
151
                    $sections = Symphony::Database()->fetch("SELECT `handle`, `entry_order`, `entry_order_direction` FROM `tbl_sections`");
152
                    foreach ($sections as $s) {
0 ignored issues
show
Bug introduced by
The expression $sections of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
153
                        Symphony::Configuration()->set('section_' . $s['handle'] . '_sortby', $s['entry_order'],
154
                            'sorting');
155
                        Symphony::Configuration()->set('section_' . $s['handle'] . '_order',
156
                            $s['entry_order_direction'], 'sorting');
157
                    }
158
                }
159
160
                // Drop `local`/`gmt` from Date fields, add `date` column. #693
161
                $date_fields = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_date`");
162
163
                foreach ($date_fields as $id) {
164
                    $table = 'tbl_entries_data_' . $id;
165
166
                    // Don't catch an Exception, we should halt updating if something goes wrong here
167
                    // Add the new `date` column for Date fields
168
                    if (!Symphony::Database()->tableContainsField($table, 'date')) {
169
                        Symphony::Database()->query("ALTER TABLE `" . $table . "` ADD `date` DATETIME DEFAULT NULL");
170
                        Symphony::Database()->query("CREATE INDEX `date` ON `" . $table . "` (`date`)");
171
                    }
172
173
                    if (Symphony::Database()->tableContainsField($table, 'date')) {
174
                        // Populate new Date column
175
                        if (Symphony::Database()->query("UPDATE `" . $table . "` SET date = CONVERT_TZ(SUBSTRING(value, 1, 19), SUBSTRING(value, -6), '+00:00')")) {
176
                            // Drop the `local`/`gmt` columns from Date fields
177
                            if (Symphony::Database()->tableContainsField($table, 'local')) {
178
                                Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `local`;");
179
                            }
180
181
                            if (Symphony::Database()->tableContainsField($table, 'gmt')) {
182
                                Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `gmt`;");
183
                            }
184
                        }
185
                    }
186
187
                    Symphony::Database()->query("OPTIMIZE TABLE " . $table);
188
                }
189
            }
190
191
            // Update the version information
192
            return parent::upgrade();
193
        }
194
195
        public static function preUpdateNotes()
196
        {
197
            return array(
198
                __("Symphony 2.3 is a major release that contains breaking changes from previous versions. It is highly recommended to review the releases notes and make a complete backup of your installation before updating as these changes may affect the functionality of your site."),
199
                __("This release enforces that Authors must have unique email addresses. If multiple Authors have the same email address, this update will fail.")
200
            );
201
        }
202
    }
203