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
Push — integration ( 45cc9f...98bc42 )
by Brendan
05:52
created

migration_230::upgrade()   F

Complexity

Conditions 27
Paths 1768

Size

Total Lines 176
Code Lines 81

Duplication

Lines 15
Ratio 8.52 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 176
rs 2
cc 27
eloc 81
nc 1768
nop 0

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", "SELECT `field_id` FROM `tbl_fields_author`");
131
132 View Code Duplication
                foreach ($author_fields as $id) {
133
                    $table = 'tbl_entries_data_' . $id;
134
135
                    // MySQL doesn't support DROP IF EXISTS, so we'll try and catch.
136
                    try {
137
                        Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP INDEX `entry_id`");
138
                    } catch (Exception $ex) {
139
                    }
140
141
                    try {
142
                        Symphony::Database()->query("CREATE UNIQUE INDEX `author` ON `" . $table . "` (`entry_id`, `author_id`)");
143
                        Symphony::Database()->query("OPTIMIZE TABLE " . $table);
144
                    } catch (Exception $ex) {
145
                    }
146
                }
147
148
                // Move section sorting data from the database to the filesystem. #977
149
                if (Symphony::Database()->tableContainsField('tbl_sections', 'entry_order')) {
150
                    $sections = Symphony::Database()->fetch("SELECT `handle`, `entry_order`, `entry_order_direction` FROM `tbl_sections`");
151
                    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...
152
                        Symphony::Configuration()->set('section_' . $s['handle'] . '_sortby', $s['entry_order'], 'sorting');
153
                        Symphony::Configuration()->set('section_' . $s['handle'] . '_order', $s['entry_order_direction'], 'sorting');
154
                    }
155
                }
156
157
                // Drop `local`/`gmt` from Date fields, add `date` column. #693
158
                $date_fields = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_date`");
159
160
                foreach ($date_fields as $id) {
161
                    $table = 'tbl_entries_data_' . $id;
162
163
                    // Don't catch an Exception, we should halt updating if something goes wrong here
164
                    // Add the new `date` column for Date fields
165
                    if (!Symphony::Database()->tableContainsField($table, 'date')) {
166
                        Symphony::Database()->query("ALTER TABLE `" . $table . "` ADD `date` DATETIME DEFAULT NULL");
167
                        Symphony::Database()->query("CREATE INDEX `date` ON `" . $table . "` (`date`)");
168
                    }
169
170
                    if (Symphony::Database()->tableContainsField($table, 'date')) {
171
                        // Populate new Date column
172
                        if (Symphony::Database()->query("UPDATE `" . $table . "` SET date = CONVERT_TZ(SUBSTRING(value, 1, 19), SUBSTRING(value, -6), '+00:00')")) {
173
                            // Drop the `local`/`gmt` columns from Date fields
174
                            if (Symphony::Database()->tableContainsField($table, 'local')) {
175
                                Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `local`;");
176
                            }
177
178
                            if (Symphony::Database()->tableContainsField($table, 'gmt')) {
179
                                Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `gmt`;");
180
                            }
181
                        }
182
                    }
183
184
                    Symphony::Database()->query("OPTIMIZE TABLE " . $table);
185
                }
186
            }
187
188
            // Update the version information
189
            return parent::upgrade();
190
        }
191
192
        public static function preUpdateNotes()
193
        {
194
            return array(
195
                __("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."),
196
                __("This release enforces that Authors must have unique email addresses. If multiple Authors have the same email address, this update will fail.")
197
            );
198
        }
199
    }
200