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
05:23
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 0
Metric Value
cc 27
eloc 84
c 0
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
    namespace SymphonyCms\Installer\Migrations;
3
4
    use SymphonyCms\Installer\Lib\Migration;
5
    use Symphony;
6
    use Exception;
7
    use DatabaseException;
8
9
    class migration_230 extends Migration
10
    {
11
        public static function getVersion()
12
        {
13
            return '2.3';
14
        }
15
16
        public static function getReleaseNotes()
17
        {
18
            return 'http://getsymphony.com/download/releases/version/2.3/';
19
        }
20
21
        public static function upgrade()
22
        {
23
            // 2.3dev
24
            if (version_compare(self::$existing_version, '2.3dev', '<=')) {
25
                Symphony::Configuration()->set('version', '2.3dev', 'symphony');
26
                Symphony::Configuration()->set('useragent', 'Symphony/2.3dev', 'general');
27
28
                // Add Publish Label to `tbl_fields`
29
                if (!Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) {
30
                    Symphony::Database()->query('ALTER TABLE `tbl_fields` ADD `publish_label` VARCHAR(255) DEFAULT NULL');
31
                }
32
33
                // Migrate any Checkbox's Long Description to Publish Label
34
                try {
35
                    $checkboxes = Symphony::Database()->fetch("SELECT `field_id`, `description` FROM `tbl_fields_checkbox`");
36
37
                    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...
38
                        if (!isset($field['description'])) {
39
                            continue;
40
                        }
41
42
                        Symphony::Database()->query(sprintf("
43
                            UPDATE `tbl_fields`
44
                            SET `publish_label` = '%s'
45
                            WHERE `id` = %d
46
                            LIMIT 1;
47
                            ",
48
                            $field['description'],
49
                            $field['field_id']
50
                        ));
51
                    }
52
53
                    Symphony::Database()->query("ALTER TABLE `tbl_fields_checkbox` DROP `description`");
54
                } catch (Exception $ex) {
55
                }
56
57
                // Removing unused settings
58
                Symphony::Configuration()->remove('allow_page_subscription', 'symphony');
59
                Symphony::Configuration()->remove('strict_error_handling', 'symphony');
60
                Symphony::Configuration()->remove('character_set', 'database');
61
                Symphony::Configuration()->remove('character_encoding', 'database');
62
                Symphony::Configuration()->remove('runtime_character_set_alter', 'database');
63
64
                if (Symphony::Configuration()->get('pagination_maximum_rows', 'symphony') === '17') {
65
                    Symphony::Configuration()->set('pagination_maximum_rows', '20', 'symphony');
66
                }
67
68
                Symphony::Configuration()->write();
69
            }
70
71
            // 2.3 Beta 1
72
            if (version_compare(self::$existing_version, '2.3beta1', '<=')) {
73
                Symphony::Configuration()->set('version', '2.3beta1', 'symphony');
74
                Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 1', 'general');
75
76
                Symphony::Configuration()->write();
77
            }
78
79
            // 2.3 Beta 2
80
            if (version_compare(self::$existing_version, '2.3beta2', '<=')) {
81
                // Migrate Publish Labels (if created) to the Label field
82
                // Then drop Publish Label, we're going to use element_name and label
83
                // to take care of the same functionality!
84
                try {
85
                    if (Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) {
86
                        $fields = Symphony::Database()->fetch('SELECT `publish_label`, `label`, `id` FROM `tbl_fields`');
87
88
                        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...
89
                            if (!$field['publish_label']) {
90
                                continue;
91
                            }
92
93
                            Symphony::Database()->query(sprintf("
94
                                UPDATE `tbl_fields`
95
                                SET `label` = '%s'
96
                                WHERE `id` = %d
97
                                LIMIT 1;
98
                                ",
99
                                $field['publish_label'],
100
                                $field['id']
101
                            ));
102
                        }
103
104
                        Symphony::Database()->query("ALTER TABLE `tbl_fields` DROP `publish_label`");
105
                    }
106
                } catch (Exception $ex) {
107
                    Symphony::Log()->notice($ex->getMessage());
108
                }
109
110
                // Add uniqueness constraint for the Authors table. #937
111
                try {
112
                    Symphony::Database()->query("ALTER TABLE `tbl_authors` ADD UNIQUE KEY `email` (`email`)");
113
                } catch (DatabaseException $ex) {
114
                    // 1061 will be 'duplicate key', which is fine (means key was added earlier)
115
                    // 1062 means the key failed to apply, which is bad.
116
                    // @see http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
117
                    if ($ex->getDatabaseErrorCode() === 1062) {
118
                        Symphony::Log()->error(
119
                            __("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())
120
                        );
121
122
                        return false;
123
                    }
124
                }
125
126
                // Update the version information
127
                Symphony::Configuration()->set('version', '2.3beta2', 'symphony');
128
                Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 2', 'general');
129
130
                Symphony::Configuration()->write();
131
            }
132
133
            // 2.3 Beta 3
134
            if (version_compare(self::$existing_version, '2.3beta3', '<=')) {
135
                // Refresh indexes on existing Author field tables
136
                $author_fields = Symphony::Database()->fetchCol("field_id",
137
                    "SELECT `field_id` FROM `tbl_fields_author`");
138
139 View Code Duplication
                foreach ($author_fields as $id) {
140
                    $table = 'tbl_entries_data_' . $id;
141
142
                    // MySQL doesn't support DROP IF EXISTS, so we'll try and catch.
143
                    try {
144
                        Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP INDEX `entry_id`");
145
                    } catch (Exception $ex) {
146
                    }
147
148
                    try {
149
                        Symphony::Database()->query("CREATE UNIQUE INDEX `author` ON `" . $table . "` (`entry_id`, `author_id`)");
150
                        Symphony::Database()->query("OPTIMIZE TABLE " . $table);
151
                    } catch (Exception $ex) {
152
                    }
153
                }
154
155
                // Move section sorting data from the database to the filesystem. #977
156
                if (Symphony::Database()->tableContainsField('tbl_sections', 'entry_order')) {
157
                    $sections = Symphony::Database()->fetch("SELECT `handle`, `entry_order`, `entry_order_direction` FROM `tbl_sections`");
158
                    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...
159
                        Symphony::Configuration()->set('section_' . $s['handle'] . '_sortby', $s['entry_order'],
160
                            'sorting');
161
                        Symphony::Configuration()->set('section_' . $s['handle'] . '_order',
162
                            $s['entry_order_direction'], 'sorting');
163
                    }
164
                }
165
166
                // Drop `local`/`gmt` from Date fields, add `date` column. #693
167
                $date_fields = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_date`");
168
169
                foreach ($date_fields as $id) {
170
                    $table = 'tbl_entries_data_' . $id;
171
172
                    // Don't catch an Exception, we should halt updating if something goes wrong here
173
                    // Add the new `date` column for Date fields
174
                    if (!Symphony::Database()->tableContainsField($table, 'date')) {
175
                        Symphony::Database()->query("ALTER TABLE `" . $table . "` ADD `date` DATETIME DEFAULT NULL");
176
                        Symphony::Database()->query("CREATE INDEX `date` ON `" . $table . "` (`date`)");
177
                    }
178
179
                    if (Symphony::Database()->tableContainsField($table, 'date')) {
180
                        // Populate new Date column
181
                        if (Symphony::Database()->query("UPDATE `" . $table . "` SET date = CONVERT_TZ(SUBSTRING(value, 1, 19), SUBSTRING(value, -6), '+00:00')")) {
182
                            // Drop the `local`/`gmt` columns from Date fields
183
                            if (Symphony::Database()->tableContainsField($table, 'local')) {
184
                                Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `local`;");
185
                            }
186
187
                            if (Symphony::Database()->tableContainsField($table, 'gmt')) {
188
                                Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `gmt`;");
189
                            }
190
                        }
191
                    }
192
193
                    Symphony::Database()->query("OPTIMIZE TABLE " . $table);
194
                }
195
            }
196
197
            // Update the version information
198
            return parent::upgrade();
199
        }
200
201
        public static function preUpdateNotes()
202
        {
203
            return array(
204
                __("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."),
205
                __("This release enforces that Authors must have unique email addresses. If multiple Authors have the same email address, this update will fail.")
206
            );
207
        }
208
    }
209